Skip to content

Instantly share code, notes, and snippets.

View dannypsnl's full-sized avatar

Lîm Tsú-thuàn dannypsnl

View GitHub Profile
package singleton
import (
"sync"
)
type singleton struct {
mu sync.Mutex
counter int
}
@dannypsnl
dannypsnl / or.go
Last active February 13, 2018 09:41
var Or func(channels ...<-chan interface{}) <-chan interface{}
func init() {
Or = func(channels ...<-chan interface{}) <-chan interface{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
@dannypsnl
dannypsnl / main.go
Last active February 2, 2022 17:29
using return error in concurrency
func main() {
res := make(chan interface{})
defer close(res)
go wrap(res, func() (interface{}, error) {
return os.Open("./main.go")
})
r := <-res
if msg, err := r.(error); err {
panic(msg)
}
@dannypsnl
dannypsnl / error_wrapper.go
Created June 21, 2018 17:41
Go: Error is Value
package wrap
import (
"reflect"
)
type ErrorWrapper struct {
err error
prevReturns []reflect.Value
}
@dannypsnl
dannypsnl / tcp_flags.txt
Created June 21, 2019 06:34 — forked from tuxfight3r/tcp_flags.txt
tcpdump - reading tcp flags
##TCP FLAGS##
Unskilled Attackers Pester Real Security Folks
==============================================
TCPDUMP FLAGS
Unskilled = URG = (Not Displayed in Flag Field, Displayed elsewhere)
Attackers = ACK = (Not Displayed in Flag Field, Displayed elsewhere)
Pester = PSH = [P] (Push Data)
Real = RST = [R] (Reset Connection)
Security = SYN = [S] (Start Connection)
@dannypsnl
dannypsnl / ip-sysctl-rp_filter.txt
Created July 8, 2019 13:42
ip sysctl, section rp_filter
# part of https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt
rp_filter - INTEGER
0 - No source validation.
1 - Strict mode as defined in RFC3704 Strict Reverse Path
Each incoming packet is tested against the FIB and if the interface
is not the best reverse path the packet check will fail.
By default failed packets are discarded.
2 - Loose mode as defined in RFC3704 Loose Reverse Path
Each incoming packet's source address is also tested against the FIB
and if the source address is not reachable via any interface
@dannypsnl
dannypsnl / int_or_string_example.cc
Created July 25, 2019 15:31
int or string, multiple dispatching to avoid duplicate define
#include <iostream>
#include <sstream>
#include <string>
struct Int;
struct String;
struct Visitor {
void print_t(int i);
void print_t(std::string str);
@dannypsnl
dannypsnl / stalin-sort.lisp
Created August 12, 2019 03:19
stalin sort XD
(defun stalin-sort (list)
(let ((cur (car list))
(rest (cdr list)))
(cond
;; [] leads []
((null rest) list)
;; e.g [3, 2, 4] => cur: 3, rest [2, 4], since 3 > 2 = car([2, 4])
;; keep run the sort by current filter
((> cur (car rest))
(stalin-sort (cons cur (cdr rest))))
@dannypsnl
dannypsnl / mac_frameworks.nix
Last active August 20, 2019 06:46
Nix: Get Frameworks of MacOS
let
pkgs = import <stable> {};
inherit (pkgs.darwin.apple_sdk.frameworks) Cocoa CoreFoundation CoreServices;
in # ignore
@dannypsnl
dannypsnl / Lexer.hs
Created August 23, 2019 03:35 — forked from osa1/Lexer.hs
separated lexing and parsing stages in parsec
module Lexer
( Token(..)
, TokenPos
, tokenize
) where
import Text.ParserCombinators.Parsec hiding (token, tokens)
import Control.Applicative ((<*), (*>), (<$>), (<*>))
data Token = Ide String