Skip to content

Instantly share code, notes, and snippets.

@mhofman
mhofman / services-start
Last active July 13, 2017 08:18
syslog-ng on AsusWRT Merlin
#!/bin/sh
alias log="logger -p user.info -t services-start"
RC='/opt/etc/init.d/rc.unslung'
i=30
until [ -x "$RC" ] ; do
i=$(($i-1))
if [ "$i" -lt 1 ] ; then
log Could not start Entware
@mhofman
mhofman / get_lutron_cert.sh
Created October 13, 2017 09:48
Generate a signed certificate valid to connect locally to a Lutron Caseta Smart Bridge
#!/bin/sh
# Usage: get_lutron_cert.sh [bridge_ip] | tee cert.pem
function error() {
echo "Error: $1" >&2
exit 1
}
login_server="device-login.lutron.com"
@mhofman
mhofman / universal.py
Created December 21, 2017 04:26
Universal media_player for home-assistant with support for `active_child_template` and `default_first_child`
"""
Combination of multiple media players into one for a universal controller.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.universal/
"""
import asyncio
import logging
# pylint: disable=import-error
from copy import copy
@mhofman
mhofman / update
Last active July 18, 2018 21:21
Git update hook to deploy files of a pushed branch into a corresponding worktree if it exists. The id of the worktree should match the branch name (rename/move if necessary). See https://git-scm.com/docs/git-worktree
#!/bin/sh
# Exit if any unhandled command fails
set -e
# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
@mhofman
mhofman / SSH-Custom-Git.md
Last active September 5, 2018 03:48
Execute custom install of git from ssh

Execute custom install of git and/or force git-shell from ssh.

Relies on using a dedicated SSH key to access the server using git.

@mhofman
mhofman / HAProxy-transparent-web-services-routing.md
Last active June 17, 2024 13:51
Leverage HAProxy to transparently route requests to web services identified by host name.

Web Service Fronting

Multiple Web properties on a single IP address

Hosting multiple websites on a single public IP address on the standard HTTP(S) ports is relatively easy with popular web servers like Apache, Nginx and lighttpd all supporting Virtual Hosts.
For Web Services which bundle their own HTTP server, things get more complicated, unless their HTTP stack can be shared somehow. More often than not, the application's HTTP stack listens directly on a dedicated TCP port.

Hosting multiple services on a single IP then requires using a fronting server listening on the standard HTTP port, and routing to the right backend service based on the host name or the path sent by the client.
Path based routing is cumbersome, usually requiring either the service to be aware of the path prefix, or a rewrite by the HTTP fronting server of all absolute URLs in the requests and responses.
Hostname based routing is more straightforward. The fronting server can just look at the [HTTP/1.1 Host header](https://tools

@mhofman
mhofman / Dockerfile
Created January 20, 2019 04:32
DNSMasq docker without root nor capabilites
FROM alpine:edge
COPY files /
ARG DNSMASQ_UID=100
ARG DNSMASQ_GID=101
ARG user=dnsmasq
ARG capabilities=cap_net_raw,cap_net_bind_service
# Because Synology is dumb
ENV DNSMASQ_UID="${DNSMASQ_UID}"
ENV DNSMASQ_GID="${DNSMASQ_GID}"
@mhofman
mhofman / greeter.js
Last active March 25, 2024 18:04
ES Module Dynamic export
const greeters = {
'en': 'english',
'fr': 'french'
};
const promise = (async () => {
const greeter = greeters[navigator.language.split('-')[0]];
if (!greeter) {
throw new Error('No greeter for you!');
@mhofman
mhofman / html-console.js
Created March 2, 2019 09:39
Simple HTML Console library
(function (global) {
var output = null;
var buffer = [];
var target;
function log(msg) {
if (!output) {
buffer.push(msg);
return;
}
@mhofman
mhofman / demo-weak-ref.js
Last active March 8, 2019 23:38
WeakRef basic demo
let target = {};
let wr = new WeakRef(target);
let fg = new FinalizationGroup((iter) => {
for(const i of iter) {
log(i);
}
});
fg.register(target, "hello world");
target = null;
setTimeout(gc, 0);