Skip to content

Instantly share code, notes, and snippets.

@adamhooper
adamhooper / pyspawner_sandbox.py
Created December 7, 2019 19:12
Network-namespace a process so it can't access our internal network
import errno
import sys
from dataclasses import dataclass
import pyroute2
@dataclass(frozen=True)
class NetworkConfig:
"""
@adamhooper
adamhooper / setup-sandbox.sh
Created December 7, 2019 18:56
Build a 20GB-or-less chroot layer
#!/bin/bash
set -e
CHROOT=/var/lib/workbench/chroot # Empty
BASE_LAYER=/var/lib/workbench/chroot-base # already populated with /etc, /lib, /usr...
CHROOT_SIZE=20G # max size of user edits
VENV_PATH="/root/.local/share/virtualenvs" # only exits in dev
# /app/common (base layer)
@adamhooper
adamhooper / spawner.py
Last active November 27, 2019 19:24
Clone a process with new file descriptors
import os
import socket
from c_clone import libc_clone
# Primer on global variables: we set these before clone(), so they're set in
# both the spawner process and the child process. Set them to None when they're
# no longer needed, to make code easier to read.
#
# Primer on pipes: `os.pipe()` creates two file descriptors: a "read" and a
# "write". Data written to the "write" end can (and must) be read by the "read"
@adamhooper
adamhooper / c_clone.py
Last active December 16, 2023 23:27
Call Linux clone() from within Python
import ctypes
import os
import signal
from typing import Callable
libc = ctypes.CDLL("libc.so.6", use_errno=True)
# <linux/prctl.h>
PR_SET_NAME = 15
PR_SET_SECCOMP = 22
@adamhooper
adamhooper / fonts.conf
Last active August 6, 2018 14:12
Using font files directly within node-canvas
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir prefix="default">.</dir>
</fontconfig>
@adamhooper
adamhooper / micro-benchmark.js
Last active September 6, 2018 21:49
fs.readFileSync() reads hot tiny files much faster than fs.readFile()
#!/usr/bin/env node
'use strict'
const fs = require('fs')
const util = require('util')
const paths = []
for (let i = 1; i <= 20000; i++) {
paths.push(`small-files/${i}.txt`)
}
@adamhooper
adamhooper / mbox_to_overview_folder.py
Last active September 10, 2023 21:13
Convert an mbox file into a folder full of .txt and attachments. Good for uploading ~1,000 messages to Overview.
#!/usr/bin/env python3
import email.message
import mailbox
# Just ignore these lines.
# Python's mbox reader finds a way to return Messages that don't
# have these super-important methods. This hack adds them.
setattr(email.message.Message, '_find_body', email.message.MIMEPart._find_body)
setattr(email.message.Message, '_body_types', email.message.MIMEPart._body_types)
@adamhooper
adamhooper / fixed_code.rb
Last active July 31, 2020 14:01
2016-07-31 Code that avoids Timeout::timeout()
require 'net/http'
#require 'timeout'
require 'mysql2' # https://github.com/brianmario/mysql2
def download_to_database(url, sql_statement)
res = Net::HTTP.start(
url.host,
url.port,
use_ssl: url.scheme == 'https',
open_timeout: 5,
@adamhooper
adamhooper / buggy_code.rb
Last active July 31, 2016 17:29
2016-07-31 Timeout::timeout() buggy code
require 'net/http'
require 'timeout'
require 'mysql2' # https://github.com/brianmario/mysql2
def download_to_database(url, sql_statement)
Timeout::timeout(5) do # BUG. Never do this.
res = Net::HTTP.get_response(url)
sql_statement.execute(url.to_s, res.code, res.body)
end
end
class Supplier < ActiveRecord::Base
end