Skip to content

Instantly share code, notes, and snippets.

@oskarhane
Created April 8, 2013 07:39
Show Gist options
  • Save oskarhane/5334944 to your computer and use it in GitHub Desktop.
Save oskarhane/5334944 to your computer and use it in GitHub Desktop.
Blocket Easter Eggs - Sista uppgiften
/**
Name: Node.js UDP server
Supported Platforms: Only tested on Linux
Date: 2013-03-28
Author: Oskar Hane <oh@oskarhane.com>
Website: http://oskarhane.com
Purpose: For Blocket's Easter Egg comptetition. A UDP server which replies all requests
with a binary representation of the string "$your_ip.$current_unix_timestamp"
Usage:
1. Install Node.js via Package: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager
or via Source: http://oodavid.tumblr.com/post/15090798307/how-to-install-node-js-on-linux
2. Install npm https://npmjs.org. "curl http://npmjs.org/install.sh | sh".
3. Install Forever to make it a deamon. "npm install forever -g"
4. Run "forever start udp.js" and connect with a UDP-client on port 2600.
*/
var dgram = require("dgram");
var Buffer = require('buffer').Buffer;
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
var time = Math.round(new Date().getTime() / 1000);
var bin = ascii2bin(rinfo.address + time);
var buf = new Buffer(bin + '\n');
server.send(buf, 0, buf.length, rinfo.port, rinfo.address);
});
server.bind(2600);
//Convert to binary representation
//Not the most "correct" way, but it works
function ascii2bin(str, index){
index = index || 0;
if(index > str.length-1)
return '';
return ('00000000' + str.charCodeAt(index).toString(2)).slice(-8) + ' ' + ascii2bin(str, index+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment