Skip to content

Instantly share code, notes, and snippets.

@changtimwu
changtimwu / bufto0endstring.coffee
Created November 7, 2012 03:29
strip chars after '\0'
dataary = [65,66,67,68,69,70, 0, 0, 0, 71]
buf = new Buffer(dataary)
console.log 'buf=',buf
s= buf.toString('utf-8')
console.log "s='#{s}'. length=#{s.length}"
ahash =
m1:1
@changtimwu
changtimwu / keepcontext.coffee
Created November 9, 2012 02:09
How to keep context(aka this) for a setInterval callback in a coffeescript class
class ggp
constructor: (@nm)->
self = this
setInterval (->
self.shownm()
),1000
shownm: ->
console.log 'my name is ', @nm
g1 = new ggp( 'qq')
@changtimwu
changtimwu / multiinherit.coffee
Created November 9, 2012 09:09
multiple inheritance in coffeescript
class Mixin
# "Class method". Augment object or class `t` with new methods.
augment: (t) ->
(t[n] = m unless n == 'augment' or !this[n].prototype?) for n, m of this
t.setup()
# When an object is augmented with at least one mixin, call this method to
# remove `mixin`.
eject: (mixin) ->
(delete this[n] if m in (p for o, p of mixin::)) for n, m of this
# Implement in your mixin to act as a constructor for mixed-in properties
@changtimwu
changtimwu / nodewininst.md
Created November 20, 2012 09:47
install nodejs and git on windows

Please prepare the following stuff.

 git --version
@changtimwu
changtimwu / robot.js
Created December 7, 2012 01:55
Gnome 3310
//FightCode can only understand your robot
//if its class is called Robot
var Robot = function(robot) {
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
@changtimwu
changtimwu / robot.js
Created December 7, 2012 01:58 — forked from kosmodrom/robot.js
from some body
var Robot = function(robot){
robot.turnLeft(robot.angle % 90);
robot.turnGunRight(90);
robot.clone();
this.direction = 1;
};
Robot.prototype.onIdle = function(ev) {
var robot = ev.robot;
robot.ahead(1);
if (robot.parentId) {
@changtimwu
changtimwu / testhmac.c
Last active December 14, 2015 18:39
some testing code for SNMPV3 USM MD5
/*
build with
gcc -std=c99 testhmac.c -o testhmac -lssl -lcrypto
*/
#include <openssl/des.h>
#include <openssl/aes.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include <stdlib.h>
#include <stdio.h>
var fs =require( 'fs');
var devfn='/dev/ttyS1'
var txFd = fs.openSync( devfn, 'w');
var rxfd = fs.openSync( devfn, 'r');
var txBuf = new Buffer('hello');
var txBytesWritten = fs.writeSync(txFd, txBuf, 0, txBuf.length, -1);
console.log("written bytes=", txBytesWritten);
fs.closeSync(txFd);
# yon can connect to server with the following command
# socat -,raw,echo=0,escape=0x0f UNIX-CONNECT:/tmp/echo.socket
net = require('net')
readline = require('readline')
create_cli_session = (sr)->
rl = readline.createInterface sr, sr
rl.setPrompt('OHAI> ')
rl.prompt()
@changtimwu
changtimwu / iptmy.sh
Last active December 16, 2015 18:39
DROP everything except web & ssh.
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 443 -j ACCEPT
iptables -A INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT