Skip to content

Instantly share code, notes, and snippets.

View koonix's full-sized avatar
🦆
honk

koonix

🦆
honk
View GitHub Profile
@koonix
koonix / openssl-ed25519-certs.md
Last active July 12, 2025 10:52
Creating Self-Signed ED25519 CA and Certificates

Creating Self-Signed ED25519 CA and Certificates

IMPORTANT: the Common Name (CN) of the servers should be different from that of the CA. Otherwise, things won't work on servers that use OpenSSL.

CA

openssl genpkey -algorithm ed25519 > ca-key.pem
@koonix
koonix / aria2ctrlfile.go
Last active December 29, 2024 22:46
aria2's control file format implementation in Go.
// Package aria2ctrlfile implements [aria2]'s [control file format] (version 1).
// See https://github.com/aria2/aria2/blob/release-1.37.0/src/DefaultBtProgressInfoFile.cc
// for implementation details.
//
// [aria2]: https://github.com/aria2/aria2
// [control file format]: https://aria2.github.io/manual/en/html/technical-notes.html#control-file-aria2-format
package aria2ctrlfile
import (
"encoding/binary"
@koonix
koonix / muldiv.go
Last active January 7, 2025 23:44
Go 64-bit multiplication and division without overflow.
import "math/bits"
func mulDiv(a, b, c int64) int64 {
hi, lo := bits.Mul64(uint64(a), uint64(b))
quo, _ := bits.Div64(hi, lo, uint64(c))
return int64(quo)
}
@koonix
koonix / ringbuf.go
Created December 25, 2024 06:23
Ring buffer.
package ringbuf
type RingBuf[T any] struct {
Buf []T
head int
tail int
}
func New[T any](size int) *RingBuf[T] {
return &RingBuf[T]{
@koonix
koonix / doh-servers.sh
Created December 21, 2024 21:02
Script that returns a list of DNS-over-HTTPS servers that are accessible directly via IP address.
#!/bin/bash
set -eu -o pipefail
# Script that returns a list of DNS-over-HTTPS servers that are accessible directly via IP address.
# https://dnscrypt.info/public-servers
# https://dnscrypt.info/stamps-specifications
main()
{
VERBOSE=false
@koonix
koonix / subsets.go
Last active October 6, 2024 17:35
Go function that returns the subsets of a slice.
func subsets[T any](v []T) [][]T {
switch len(v) {
case 0:
return [][]T{
{},
}
case 1:
return [][]T{
{v[0]},
{},
@koonix
koonix / permute.go
Last active October 6, 2024 17:32
Go function that returns the permutations of a slice.
func permute[T any](v []T) [][]T {
switch len(v) {
case 0:
return [][]T{
{},
}
case 1:
return [][]T{
{v[0]},
}
@koonix
koonix / nuclear.go
Last active January 8, 2025 00:14
Nuclear atomically stores values. The closest thing to this in the stdlib is atomic.Pointer.
import "sync"
type Nuclear[T any] struct {
mu sync.RWMutex
v T
}
func NewNuclear[T any](v T) *Nuclear[T] {
return &Nuclear{
v: v,
@koonix
koonix / once.go
Created September 27, 2024 23:04
Go object that stores a value only once
package once
import "sync"
type Once[T any] struct {
sync.Mutex
has bool
v T
}
@koonix
koonix / withcloser_reader.go
Last active September 18, 2024 17:30
Go package that adds Close to Reader/Writer
package withcloser
import "io"
type reader struct {
io.Reader
close func() error
}
func (r reader) Close() error {