Skip to content

Instantly share code, notes, and snippets.

View whiler's full-sized avatar
🇨🇳

whiler whiler

🇨🇳
View GitHub Profile
@whiler
whiler / snowflake.c
Created December 29, 2016 06:47
A simple implementation of twitter snowflake in C
#include<time.h>
#include<unistd.h>
/*
* snowflake.c
*
* a simple implementation of twitter snowflake in C
*
* [email protected]
*/
@whiler
whiler / echo.lua
Created January 4, 2017 10:14
Simple TCP Echo Server in Lua
local signal = require("posix.signal")
local socket = require("socket")
local string = require("string")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("127.0.0.1", 0))
local ip, port = server:getsockname()
print(string.format("telnet %s %s", ip, port))
@whiler
whiler / sample.py
Created January 5, 2017 09:28
Chooses k unique random elements from a population sequence with odds like random.sample
# coding=utf-8
import random
__author__ = "[email protected]"
def sample(population, k, odds=lambda item: 1.0):
"""
Chooses k unique random elements from a population sequence with odds.
@whiler
whiler / astar.py
Last active November 24, 2022 06:41
A star algorithm implementation in Python language
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import bisect
class AStarPathfinding(object):
class Node(object):
# item must be hashable
@whiler
whiler / binheap.py
Last active April 23, 2018 10:13
Binary Heap implementation in Python language
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
class BaseBinaryHeap(object):
def __init__(self, key=lambda x: x, value=lambda x: x):
self.queue = list()
self.index = dict()
self.key = key
@whiler
whiler / binheap.js
Created April 24, 2018 08:32
Binary Heap implementation in JavaScript language
"use strict";
function BinaryHeap(keyFunction, scoreFunction) {
this.content = [];
this.map = {};
this.keyFunction = keyFunction || function (e) {
return e;
};
this.scoreFunction = scoreFunction || function (e) {
return e;
@whiler
whiler / ping-before-ssh.sh
Created May 28, 2018 07:20
ping before ssh
iptables --table filter --append INPUT --match conntrack --ctstate ESTABLISHED,RELATED --match comment --comment "Allow established and related connections" --jump ACCEPT
iptables --table filter --append INPUT --protocol icmp --icmp-type echo-request --match length --length 84 --match recent --set --name openSSH --rsource --match comment --comment "ping before ssh" --jump LOG --log-prefix "OpenDoor SSH: " --log-level 7
iptables --table filter --append INPUT --protocol tcp --dport ssh --syn --match recent ! --rcheck --seconds 30 --name openSSH --rsource --match comment --comment "Drop SSH connection not pinged recently" --jump DROP
@whiler
whiler / scanner.py
Created October 19, 2018 03:37
Python Tokenizer using re.Scanner
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import collections
import re
Token = collections.namedtuple('Token', ['tpe', 'value'])
Scanner = re.Scanner((
@whiler
whiler / limit.py
Last active June 8, 2022 21:19
simplest rate limiting algorithm in python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Ref. https://stackoverflow.com/questions/667508/whats-a-good-rate-limiting-algorithm#answer-668327
import time
def limit(rate, burst=0.0):
unit = 1.0
@whiler
whiler / traceroute.py
Created August 6, 2019 04:17
traceroute implementation in Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import random
import socket
import time
def traceroute(dest, hops=64, timeout=0.7):
empty = bytes()