Skip to content

Instantly share code, notes, and snippets.

View paulo-raca's full-sized avatar

Paulo Costa paulo-raca

View GitHub Profile
@paulo-raca
paulo-raca / new_password.py
Created August 9, 2024 22:10
Generate secure random password
#!/usr/bin/env python3
import string
import secrets
n=16
charset = string.ascii_letters + string.digits + string.punctuation
print("".join([secrets.choice(charset) for i in range(n)]))
@paulo-raca
paulo-raca / README.md
Last active June 12, 2023 16:16
Support for `Forwarded`, `X-Forwarded` and `X-Real-IP` in Go ReverseProxy

This code ensures X-Forward (and friends) headers are updated correctly when using stacked proxies: the host, protocol and "for" should be inherited from the previous proxies.

Of course, there are 3 competing/similar ways to retrieve this information from the previous Proxy, so this tries to be compatible with them all:

  • Forwarded (new, nicer and standardized in rfc7239)
  • X-Forwarded-[For/Host/Proto], the legacy but de-facto standard
  • X-Real-IP, a less-common alternative to X-Forwarded-For[0].

Also, retrieving them from the incoming request requires caution: Malicious clients may try inject these headers, so these headers should only be used when we expect them.

@paulo-raca
paulo-raca / .gitconfig
Last active September 6, 2023 15:40
OS setup
[user]
email = Xxx Yzz
name = xxx
signingkey = /home/xxx/.ssh/id_rsa
[sendemail]
from = Xxx Yyy <[email protected]>
smtpuser = [email protected]
smtppass = xxx
smtpserver = smtp.googlemail.com
@paulo-raca
paulo-raca / NetworkStateCallback.kt
Created July 30, 2021 12:08
Handy NetworkCallback implementation that merges the bunch of distinct callbacks in a single state
package com.crowdstrike.android.vpn.util
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import android.util.Log
/**
* NetworkCallback is quite inconvenient in that data is split between
@paulo-raca
paulo-raca / Condition.kt
Last active October 1, 2023 22:16
Condition Variables for Kotlin Coroutines
package kotlinx.coroutines.sync
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.withTimeout
import java.util.function.Predicate
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.nanoseconds
@paulo-raca
paulo-raca / token_bucket.py
Last active November 8, 2019 22:05
TokenBucket in Python-Asyncio
import asyncio
import time
class Timer:
def now():
raise NotImplemented()
async def wait(time):
raise NotImplemented()
class WallTimer(Timer):
@paulo-raca
paulo-raca / trace_parser.py
Last active February 14, 2020 02:17
Quick and dirty parser for Android traces
#!/usr/bin/env python3
"""
Parser for Android Traces.
Based on documentation from https://rhye.org/post/android-profiling-flamegraphs/
"""
from dataclasses import dataclass
from enum import Enum
package com.crowdstrike.android.playstorecontroller.util;
import android.Manifest;
import android.app.ActivityThread;
import android.app.Instrumentation;
import android.app.UiAutomation;
import android.graphics.Bitmap;
import android.nfc.Tag;
import android.os.Build;
import android.os.Debug;
@paulo-raca
paulo-raca / scan.cpp
Created March 1, 2018 19:45
Seach for data on an array region with decreasing step sizes
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
void scan(uint64_t min, uint64_t max, uint64_t min_step = 1) {
uint64_t step = 1L<<63;
printf("%02" PRIu64 "\n", min);
while (step > min_step) {
for (uint64_t i = min + step / 2; i<max; i+=step) {
@paulo-raca
paulo-raca / AndroidManifest.xml
Last active April 13, 2022 01:02
getActiveNotifications() using NotificationListenerService
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="testapp.android.gradle.inutilfutil.com.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"