Skip to content

Instantly share code, notes, and snippets.

@amn
amn / cygadmin.sh
Last active August 4, 2016 12:39
Download fresh copy of Cygwin installer if current copy is stale, and invoke it without administration rights (user-local install)
#!/bin/sh -ue
# Usage: /path/to/cygadmin.sh [preferred/path/to/setup-x86_64.exe]
# If first argument is omitted, this script behaves as if the value was ".", i.e. current working directory.
# If setup file exists, its extended attributes are checked to see if a property called 'etag' is attached. If it is, its value is used in HTTP request to Cygwin website to download the installer. The website will reply with "not modified, use your own copy" if etag values match, and in this case, the existing setup file will be used. This is in effect local caching of Web resources, the same thing your browser does, but through command line. The setup file is invoked with '--no-admin' option, because my Windows install is managed by crazy paranoid system administrators of our organization where even professionals are not trusted with Administrator role.
# When a fresh installer file is downloaded, an extended attribute with the name 'etag' is attached to it, value being as present in HTTP response. All this so that the c
@amn
amn / auth-w-tokens-ex-1.py
Last active June 1, 2017 21:10
Authentication with tokens, example
import datetime
""" Secrets, must not be shared. """
token_encrypt_decrypt_key = "<YOUR-KEY-TEXT>";
good_token_salt = "<YOUR-SALT-TEXT>";
# Placeholders: encrypt, decrypt, current_date, get_client_ipv4_addr, client_send, Error,
def make_auth_token(username, ipv4_addr, expires):
""" Compose and return an authentication token that can be given to the client (only over private [secure] channel). """
@amn
amn / IntersectionObserver.js
Created December 10, 2018 20:27
An implementation of IntersectionObserver class, after http://w3c.github.io/IntersectionObserver, with some deviations
/** An implementation of the IntersectionObserver class, as specified by http://w3c.github.io/IntersectionObserver. There are some open issues with the later specification, but an attempt is made here to follow it to the best of ability, unless it is found too difficult to interpret -- the motivation is, after all, to have a working implementation, and as certain elements of specification weren't clarifying in themselves, these parts of implementation may turn out to be lacking or even incorrect.
This was initially written to plug in in user agents that do not provide their own implementation, like Safari on iOS 9.3.5. Hence, the used syntax flavour is such as to work in said user agent. */
IntersectionObserver = (function() {
/** Find intersection product of two rectangles `a` and `b`, itself a rectangle. Essentially, this function is the logical AND operation for rectangles. A rectangle with zero width and/or height is returned if `a` and `b` are adjacent but do not overlap in that axis. No rectangle is re
@amn
amn / set-security-info-by-sddl.c
Last active March 15, 2019 09:19
Demonstrates how using SetFileSecurity does not result in a [file] ACL with ACEs inherited from parent [folder], while using SetNamedSecurityInfo does, as is proper. Disable (comment) the `SetNamedSecurityInfo` call along with its parent `if` statement and enable (uncomment) the following `SetFileSecurity` call (and its parent `if` statement, ob…
/*
Demonstrates how using SetFileSecurity does not result in a [file] ACL with ACEs inherited from parent [folder], while using SetNamedSecurityInfo does, as is proper.
Disable (comment) the `SetNamedSecurityInfo` call along with its parent `if` statement and enable (uncomment) the following `SetFileSecurity` call (and its parent `if` statement, obviously) to switch the behavior and observe different resultant ACL on the file.
The Windows application entry point in this snippet expects two command line arguments -- the file path of the file you want to set security information on, and the actual security (specified in SDDL format) information desired for the file.
*/
#include <windows.h>
#include <shellapi.h>
@amn
amn / solarized.vim
Last active September 19, 2021 13:33
An alternative, "austere" implementation of the Solarized color scheme for Vim
" URL: https://gist.github.com/amn/f77f67df54ad4471f2fa12262d7addf5
hi clear
if exists("syntax_on")
syntax reset
endif
let g:colors_name = "Solarized"
@amn
amn / uuid.js
Last active September 24, 2021 10:08
A [`crypto.getRandomValues` based,] "copy avoiding" UUID-4 generator for the Web
export const uuid4 = () => {
const ho = (n, p) => n.toString(16).padStart(p, 0); /// Return the hexadecimal text representation of number `n`, padded with zeroes to be of length `p`
crypto.getRandomValues(new Uint8Array(16)); /// Fill a data buffer with random data
data[6] = (data[6] & 0xf) | 0x40; /// Patch the 6th byte to reflect a version 4 UUID
data[8] = (data[8] & 0x3f) | 0x80; /// Patch the 8th byte to reflect a variant 1 UUID (version 4 UUIDs are)
const view = new DataView(data.buffer); /// Create a view on the data buffer
return `${ho(view.getUint32(0), 8)}-${ho(view.getUint16(4), 4)}-${ho(view.getUint16(6), 4)}-${ho(view.getUint16(8), 4)}-${ho(view.getUint32(10), 8)}${ho(view.getUint16(14), 4)}`; /// Compile the canonical representation of the data
};
@amn
amn / MediaRecorderDataStream.js
Created September 23, 2021 19:17
A class of readable streams that vend data chunks generated by a media recorder
export default class MediaRecorderDataStream extends ReadableStream {
constructor(recorder) {
const signaler = new AbortController(), { signal } = signaler;
super({
start: controller => {
recorder.addEventListener("dataavailable", ev => {
controller.enqueue(ev.data);
}, { signal });
recorder.addEventListener("stop", ev => {
controller.close();
@amn
amn / toggle-windows-theme.ps1
Created April 19, 2023 20:39
A rather quick-and-dirty but functioning script for toggling Windows theme against calculated times of sunset and sunrise at current location
Add-Type -AssemblyName System.Device
$gcw = New-Object System.Device.Location.GeoCoordinateWatcher
$gcw.Start()
$date = Get-Date
$DaysInYear = if ($date.Year % 4) { 366 } else { 365 }
$timezone = Get-TimeZone