Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
numberoverzero / _trace.py
Created August 3, 2017 16:35
add TRACE level to python logging
import logging
_trace_installed = False
def install_trace_logger():
global _trace_installed
if _trace_installed:
return
level = logging.TRACE = logging.DEBUG - 5
@numberoverzero
numberoverzero / 00. selinux.md
Last active June 17, 2022 16:31
hardened nginx conf, multiple subdomains under different certs using SNI

SELinux, oh god.

Context

You generated some certs, some dhparams, set up cloudflare origin certs, copied all the settings below, added some content to /var/www/ and set up your nginx.conf, and nginx -t says everything's fine.

Let's load some content! Nope, just kidding, nothing works.

@numberoverzero
numberoverzero / appendix.rst
Last active November 11, 2021 23:41
semver appendix A

Appendix: Backwards Compatible Bug Fixes

The last point of Semantic Versioning 2.0.0__ has caused much debate around what constitutes a breaking change__. There's no way to strictly define breaking without reducing it to uselessness. Instead, the following tests are applied to determining if a bug fix is backwards compatible. If either of the following holds, fixing the bug is backwards incompatible:

  1. A reasonable user would not notice the unintended behavior; even if it is not covered by, or is directly
@numberoverzero
numberoverzero / 00.bloop_multitable.py
Created November 10, 2021 23:31
Small class to map multiple Bloop models onto a shared table
# bloop 3.0.0
import copy
from typing import Union
from bloop import BaseModel, Column
from bloop.models import subclassof, instanceof
from bloop.types import Type
__all__ = ["Mapper"]
# fragments from evaluating some toolkit classes to build shared models as used
# in DAT401: Advanced Design Patterns for DynamoDB
# https://www.youtube.com/watch?v=HaEPXoXVf2k
import functools
from typing import Type
from bloop.conditions import Condition, iter_columns
from bloop.models import BaseModel, Column, IMeta, bind_column
@numberoverzero
numberoverzero / run_forked.rs
Created November 6, 2021 07:44
execute a function in a detached process and exit
// fork = "0.1"
use fork::{self, Fork};
use std::process;
/// no error handling, no logging, just one shot to run in a forked process
fn run_forked<R>(f: fn() -> R) -> bool
{
match fork::fork() {
Ok(Fork::Parent(_)) => {
// we're in the parent process, must have forked successfully
@numberoverzero
numberoverzero / async_value.py
Created July 6, 2015 02:50
Variable that can be `await`ed on to reach a certain value, without blocking an event loop
import asyncio
missing = object()
class Value:
'''
Simple class that enables `await value.wait_for(foo)` to wait until the
variable is set to the expected value, without blocking the event loop.
Loosely modeled after asyncio.Event and asyncio.Condition
@numberoverzero
numberoverzero / 00. costFactorForDuration.ts
Created March 15, 2021 09:18
compute an optimal bcrypt costfactor for a target hash time on the current machine
import assert from 'assert'
import bcrypt from 'bcrypt'
import crypto from 'crypto'
import { PerformanceObserver, performance } from 'perf_hooks'
const MINIMUM_BCRYPT_COST_FACTOR = 10
const RECOMMENDED_BCRYPT_COST_FACTOR = costFactorForDuration(250)
/**
* Calculate a cost factor based on a target duration (in milliseconds) for hashing.
@numberoverzero
numberoverzero / 00. 32bit key
Last active May 23, 2020 00:50
Monogame RenderQueue designs
MSB LSB
00 06 14 15 26 32
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━┫
┃pass 64┃layer 256┃ 0 ┃effect 512┃texture 256┃ BATCHED MATERIAL
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━┫
┃pass 64┃layer 256┃ 1 ┃data ~2 bytes┃ DYNAMIC MATERIAL
┣━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━╋━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
00 06 14 15 32
MSB LSB
@numberoverzero
numberoverzero / 00_hash_naming.cs
Created April 18, 2020 09:05
utility functions for filename nonces
byte[] Sha256(string s)
{
using (var hash = SHA256.Create())
{
return hash.ComputeHash(Encoding.UTF8.GetBytes(s));
}
}
byte[] Fold(byte[] input)
{