Skip to content

Instantly share code, notes, and snippets.

@horus
horus / printer.go
Last active December 18, 2023 02:31
fix: send on closed channel
type printer struct {
sync.Mutex
queue chan api.ContainerEvent
consumer api.LogConsumer
stopped bool
}
// newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
func newLogPrinter(consumer api.LogConsumer) logPrinter {
queue := make(chan api.ContainerEvent)
@horus
horus / 401.pl
Last active July 27, 2023 09:22
Challenge: Private Access Tokens
#!/usr/bin/perl
use strict;
use warnings;
use Digest::SHA qw(sha256_hex sha256);
use MIME::Base64 qw(encode_base64 decode_base64url);
sub pack_issuer { pack('n2A*', 0x0002, length $_[0], $_[0]) }
sub pack_origin { pack('xnA*', length $_[0], $_[0]) }
@horus
horus / main.c
Last active January 27, 2021 12:20
main.c:7:12: warning: 'main' is usually a function [-Wmain]
/*
> gcc48 main.c -o test 01/27 18:45
> ./test 01/27 18:46
hello world!
> cat main.c
*/
/* FreeBSD, GCC 4.8 */
const char main[] = {
0xb8,0x04,0x00,0x00,0x00, // mov $0x4,%eax ; syscall write
0xbf,0x01,0x00,0x00,0x00, // mov $0x1,%edi ; stdout, 1st arg
@horus
horus / TuplePlus.hs
Created January 15, 2021 03:33
tuple arithmetic via generics
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
module TuplePlus where
import GHC.Generics
class GTuplePlus f where
gplus :: f p -> f p -> f p
package main
import (
"fmt"
"runtime"
)
func main0() {
var a = []int{1, 2, 3, 4, 5}
for i := range a {
@horus
horus / Screen Locker.applescript
Created August 29, 2017 06:39
Messages.app Screen Locker: Forget to lock your Mac? Text yourself!
using terms from application "Messages"
on message sent theMessage for theChat
end message sent
on message received theMessage from theBuddy for theChat
if name of theBuddy contains "_phone_#_" and theMessage = "lock" then
tell application "System Events"
start current screen saver
end tell
@horus
horus / pam_script.c.patch
Created January 31, 2015 02:26
avoiding ECHILD
--- pam_script/pam_script.c 2014-06-27 03:39:02.000000000 +0800
+++ pam_script/pam_script.c 2015-01-31 10:15:21.000000000 +0800
@@ -24,6 +24,9 @@
#include <sys/wait.h> /* wait */
#include <unistd.h> /* stat, fork, execve, **environ */
#include <stdlib.h> /* calloc, setenv, putenv */
+#include <signal.h> /* signal, SIGCHLD, SIG_DFL, SIG_ERR */
+
+typedef void (*sighandler_t)(int);
@horus
horus / gist:52e2c81ee364e69c26e8
Created January 16, 2015 11:12
java-bridge JNI/Haskell example
import Foreign.Java
main :: IO ()
main = do
initJava ["-Djava.class.path=/home/horus/test"]
runJava $ do
Just testClass <- getClass "Test"
Just systemClass <- getClass "java.lang.System"
Just printStreamClass <- getClass "java.io.PrintStream"
Just outField <- getStaticField systemClass "out" (object "java.io.PrintStream")
import java.util.function.*;
import java.util.stream.*;
public class Test {
public static void main(String[] args) {
Supplier<Integer> times = () -> 3;
Function<Integer, String> dotimes = x -> {
String val;
switch(x) {
case 1: val = "once";
@horus
horus / pmctemplate.hs
Created November 28, 2014 05:59
Lab 05 - Poor Man's Concurrency Monad
module Lab5 where
import Control.Monad
data Concurrent a = Concurrent ((a -> Action) -> Action)
data Action
= Atom (IO Action)
| Fork Action Action
| Stop