Skip to content

Instantly share code, notes, and snippets.

@qerub
qerub / HttpsFilter.java
Last active November 6, 2022 00:12
Servlet filter for forcing HTTPS when behind a SSL termination proxy that sends X-Forwarded-Proto
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import static java.lang.String.format;
public class HttpsFilter implements Filter {
@qerub
qerub / spiped-ssh.plist
Created December 19, 2013 01:30
Piping sshd through spiped running as an OS X launch daemon
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>spiped-ssh</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/spiped</string>
<string>-d</string>
@qerub
qerub / index.md
Last active November 21, 2021 20:02
Useful macOS commands

How to list non-Apple kernel extensions

kextstat | egrep -v '\bcom.apple.'

How to restart the computer without having to manually unlock FileVault

sudo fdesetup authrestart
@qerub
qerub / gist:96104c9844aa399b679e
Created July 28, 2014 13:24
Integrating Thrift's JSON serializer with Jackson
class ThriftSerializer extends JsonSerializer<TBase> {
@Override
public void serialize(TBase value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
try {
TProtocolFactory f = new TSimpleJSONProtocol.Factory();
String s = new TSerializer(f).toString(value);
jgen.writeRawValue(s);
}
catch (TException e) {
throw new IOException(e);
@qerub
qerub / x_forwarded_for.clj
Created June 12, 2012 15:06
[Clojure] Ring middleware for `X-Forwarded-For` [:remote-addr rewrite]
(ns ring.middleware.x-forwarded-for
(:use [clojure.string :only (split)]))
(defn wrap-x-forwarded-for [handler]
(fn [request]
(if-let [xff (get-in request [:headers "x-forwarded-for"])]
(handler (assoc request :remote-addr (last (split xff #"\s*,\s*"))))
(handler request))))
@qerub
qerub / gist:3219854
Last active April 21, 2021 09:46
Representing [Clojure] code in JSON
; Context:
; http://stackoverflow.com/questions/3436216/how-to-map-clojure-code-to-and-from-json
(defn escape-string [x]
(clojure.string/replace x #"^[':\\]" "\\\\$0"))
(defn code-to-json [x]
(condp #(%1 %2) x
number? x
symbol? (str \' (name x))
@qerub
qerub / lifx-sunrise-simulator.rb
Last active September 9, 2020 11:44
LIFX Sunrise Simulator
require "lifx" # http://www.rubydoc.info/gems/lifx
def calculate_color(i) # 0 <= i <= 1
h = [40 * 2 * i, 40].min # will be 40 (yellow) at i=1/2 and stay there
s = 1.0 - [(i - 0.5) * 2, 0].max # will be 1 until i=1/2 and then go down to 0
b = i
LIFX::Color.hsbk(h, s, b, LIFX::Color::KELVIN_MIN)
end
duration = 10 * 60 # seconds
@qerub
qerub / vm_sync_shutdown.rb
Last active January 18, 2020 17:57
Script to shutdown a libvirt VM/domain and wait for completion
#!/usr/bin/env ruby
require "libvirt"
def vm_sync_shutdown(name)
conn = Libvirt::open("qemu:///system")
dom = conn.lookup_domain_by_name(name)
# TODO: Use dom.state instead of dom.info.state when it gets available
@qerub
qerub / httpsExchange.ts
Created December 18, 2016 00:24
[TypeScript] Dependency-free Promise-based Node.js HTTP client
// Based on https://www.tomas-dvorak.cz/posts/nodejs-request-without-dependencies/
import * as https from "https";
async function httpsExchange(requestOptions: https.RequestOptions): Promise<string> {
return new Promise<string>((resolve, reject) => {
const request = https.request(requestOptions, (response) => {
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error("Non-2xx status code: " + response.statusCode));
}
@qerub
qerub / lifx.yaml
Created June 25, 2016 23:00
First stab at a Swagger/OpenAPI specification for the LIFX API
swagger: "2.0"
info:
title: LIFX HTTP Remote Control API
version: v1
description: https://api.developer.lifx.com/
host: api.lifx.com
basePath: /v1
schemes:
- https
produces: