Skip to content

Instantly share code, notes, and snippets.

View FergusInLondon's full-sized avatar
😑

Fergus FergusInLondon

😑
View GitHub Profile
@FergusInLondon
FergusInLondon / bearing.py
Last active March 13, 2024 16:24
bearing + angle of elevation calculations
from collections import namedtuple
from math import radians, sin, cos, atan2, sqrt, degrees
# Point is a convenience wrapper to hold the co-ordinates belonging to
# a particular "point" - i.e. the base station location, or the remote
# device location.
#
# Units: lat, long = radians. alt = meters.
Point = namedtuple('Point', ['lat', 'lon', 'alt'])
@FergusInLondon
FergusInLondon / DockerClient.php
Created April 23, 2018 23:41
Example Docker Engine API Client using PHP
<?php
class DockerClient {
/** @param resource */
private $curlClient;
/** @param string */
private $socketPath;
@FergusInLondon
FergusInLondon / snoopy.c
Created March 28, 2017 07:02
snoopy.c - process_vm_readv() demonstration.
/**
* snoopy.c - Snoop on another tasks memory.
* Fergus In London <fergus@fergus.london>
*
* This is a pretty basic demo of the process_vm_readv syscall, a syscall which
* provides a nicer interface than ptrace for accessing memory used by another
* task.
*
* To play with simply use `ps` to get a PID for the process you'd like to snoop
* on, and `pmap` for the relevant memory address.
@FergusInLondon
FergusInLondon / filesnooper.c
Last active September 24, 2021 16:20
LD_PRELOAD Code Injection and Hooking
/* filesnooper.c - Fergus In London <fergus@fergus.london>
*
* Similar to snoopy.c, wrote to scratch an itch and demonstrate how LD_PRELOAD
* can be used for injecting code and/or hooking/wrapping around function calls.
*
* This is actually a good example, because if you try and use it with different
* ways of writing files, you'll see that to actually do this effectively you'd
* need to examine the target binary with strace, and potentially implement
* signatures from other libraries. (i.e a binary calling fprintf() may not be
* be effected by this.)
@FergusInLondon
FergusInLondon / profiling.py
Last active February 5, 2021 06:56
Two Python contextmanagers for performance profiling - <100 line
"""
profiling.py - standard library backed context managers for performance profiling
To use these context managers, ensure that the appropriate environment variable is
set - i.e. 'PROFILING_ENABLED'. The default directory for outputting profiling data
is the *current directory* - i.e `.` - although this too can be overidden via the
environment - specifically `PROFILING_DIRECTORY`.
Due to the quirk in the design of `pathlib`, passing an absolute path to one of the
context managers will override the output directory associated with that trace.
@FergusInLondon
FergusInLondon / mpris.py
Last active September 21, 2020 04:36
Retrieve data from a Media Player in Linux, via dbus. (Uses Python)
import dbus
class MediaPlayer:
"""Recieves state from a MediaPlayer using dbus."""
player_properties = False
def __init__(self, player_name):
# Get an instance of the dbus session bus, and retrieve
# a proxy object for accessing the MediaPlayer
@FergusInLondon
FergusInLondon / gists.js
Created April 30, 2020 15:17
Get github gists for static site generation
async function getGithubGists(username) {
const gists = await require('axios').get(
`https://api.github.com/users/${username}/gists`
)
return gists.data.map(({html_url, description, created_at, files}) => {
return {
url: html_url,
files: Object.keys(files),
description, created_at
@FergusInLondon
FergusInLondon / DependencyTraverser.js
Last active April 29, 2020 15:57
Dependency Traversal
const _ = require("lodash");
const fixtures = require("./fixtures.js");
/**
* Simple object capable of demonstrating how to build a dependency tree
* from a POM-like structure; using BFS (queue based).
*/
function DependencyTraverser(targetObject = {})
{
/** Queue containing dependencies to process */
@FergusInLondon
FergusInLondon / gitstats.js
Created April 29, 2020 13:01
Get git stats for static site generation.
const run = require('util').promisify(require('child_process').exec)
async function getGitStats() {
const {stdout: refHash } = await run('git rev-parse HEAD')
const {stdout: message } = await run(`git log --format=%B -n 1 ${refHash}`)
const {stdout: branch } = await run('git rev-parse --abbrev-ref HEAD')
return { refHash, message, branch }
}
@FergusInLondon
FergusInLondon / 1.window.js
Last active April 28, 2019 23:38
Processing Sequential Data w/ "*Sliding Windows*"
/**
* A (very over-engineered, yet still impressively scruffy) implementation of a
* Sliding Window (adjacent, overlapping, and buffered.) for the processing of
* sequential data.
*
* Written very hurriedly one evening, to support a blog post.
*
* @param {array} data
* @param {int|Number} width
* @param {array?} accumulator