Skip to content

Instantly share code, notes, and snippets.

Alba Mendez jmendeth

Block or report user

Report or block jmendeth

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View crc32.js
const table = new Int32Array(256).map((_,n) => [...Array(8)]
.reduce(c => (c>>>1) ^ ((c&1) * 0xEDB88320), n))
const fn = (C, b) => (C>>>8) ^ table[(C^b) & 0xFF]
const crc = (x, seed) => ~x.reduce(fn, ~seed)
// Example:
crc( Buffer.from('Hello') ) === 0xF7D18982
@jmendeth
jmendeth / README.md
Last active Jul 25, 2019
Helper program to add HTTP/SOCKS proxy support to SSH
View README.md

ssh-proxy-dialer

This program adds proxy support to ssh. Once installed, ssh will obey the ssh_proxy environment variable (or all_proxy as a fallback) and will try to connect to the server through that proxy. Example:

export ssh_proxy="socks5://10.139.2.1:8066"
ssh example.com  # will connect through SOCKS5 proxy
@jmendeth
jmendeth / diff.py
Created Jul 15, 2019
Quick & dirty uility for diffing filesystems
View diff.py
import os, re
# Ignores differences on folder mode / ownership
# Collapses "all contents are new" into "whole folder is new"
# Forces no type differences (i.e. link vs dir, etc.)
# Keeps in mind loaded passwd / group for comparison of UID or GID
with open("/etc/passwd") as f:
new_uid_table = { int(x.split(":")[2]): x.split(":")[0] for x in f.read().strip().splitlines() }
with open("/other/etc/passwd") as f:
@jmendeth
jmendeth / optimal-bifurcation.c
Last active Jun 14, 2019
Implementation of the optimal bifurcation algorithm
View optimal-bifurcation.c
/**
Implementation of the optimal bifurcation algorithm.
- bw: Amount of bandwidth to distribute
- N: Number of links
- Cs: Capacities of each link, in descending order
- hCs: Square roots of the capacities
- b [output]: Bandwidths to send through each link
Given a set of links whose capacities are the items of `Cs`, split the
@jmendeth
jmendeth / README.md
Last active Jun 10, 2019
Wrapper that simplifies SSH tunnels
View README.md

ssh-from

ssh-from simplifies common usage of SSH tunnels, and makes double (or triple) tunnels a piece of cake.
Here is an example, where I SSH to a computer that is behind two NATs, tunneling through each router:

Double tunnel example

Details coming to the blog soon.

Installation

@jmendeth
jmendeth / clear.py
Created Jul 17, 2012
"clear" command in Python
View clear.py
class clear:
def __call__(self):
import os
if os.name==('ce','nt','dos'): os.system('cls')
elif os.name=='posix': os.system('clear')
else: print('\n'*120)
def __neg__(self): self()
def __repr__(self):
self();return ''
@jmendeth
jmendeth / unpack-config.ts
Last active May 17, 2019
Quick script to write many config files in a single file
View unpack-config.ts
const util = require('util')
const path = require('path')
const fs = require('fs')
const mkdir = util.promisify(fs.mkdir)
const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile)
const trimLines = text => text.replace(/^[ \t]*\n?|\n?[ \t]*$/g, '')
const dedent = text => {
const lines = text.split('\n').map(line => line.trim() ? line : '')
@jmendeth
jmendeth / maildir.ini
Last active May 17, 2019
Haraka plugin to store mail in a fixed maildir
View maildir.ini
# This file must be placed in "config" directory of your Haraka server.
# Maildir directory. Can be relative to Haraka server directory, or absolute.
# Available tokens:
# %d -> domain
# %n -> username
path=/var/mails/%d/%n
# Permission of new directories.
dir_mode=0750
@jmendeth
jmendeth / dhondt.py
Last active Apr 28, 2019
Implementation of the D'Hondt method
View dhondt.py
""" Implements the D'Hondt method.
Given a chamber of `total_seats` seats, and a dictionary of
integer values (vote counts), one for each party, assigns seats
to each party using the D'Hondt method.
Returns a dictionary of integer values (assigned seats). Results
are undefined if two parties have the same number of non-zero
votes, or there are no votes.
Example (taken from Wikipedia):
@jmendeth
jmendeth / parser.py
Last active Apr 10, 2019
Parsing twitter IDs
View parser.py
"""
Simple parser for Twitter-generated IDs.
ID model taken from Snowflake, which is no longer used:
https://github.com/twitter-archive/snowflake/blob/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231/src/main/scala/com/twitter/service/snowflake/IdWorker.scala
Twitter IDs are 64-bit, and have the following structure:
- Bits 63 - 22: timestamp (in ms)
- Bits 21 - 17: datacenter ID
- Bits 16 - 12: worker ID
You can’t perform that action at this time.