Skip to content

Instantly share code, notes, and snippets.

@AlbinoGeek
Created June 25, 2014 14:01
Show Gist options
  • Save AlbinoGeek/cebc1af63ddfb6829b46 to your computer and use it in GitHub Desktop.
Save AlbinoGeek/cebc1af63ddfb6829b46 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// Starts a small HTTP server on all IP addresses, port 8000
// Server will resolve all /example.com style requests with jwhois.
(function() {
var Router, HashTable;
try {
Router = require('node-simple-router');
} catch (e) {
console.log("I used node-simple-router because I'm lazy.");
console.log("You can install it with: npm -g node-simple-router");
process.exit(-1);
}
try {
HashTable = require('hashtable');
} catch (e) {
console.log("I used hashtable because I'm lazy.");
console.log("You can install it with: npm -g hashtable");
process.exit(-1);
}
var Sys, Exe, child;
var internal_cache, http, router, server, clean_up;
// Create a new hashtable object to store results
internal_cache = new HashTable();
// Pre-hash table to hold approximately 4K records.
internal_cache.rehash(4096);
Sys = require('sys');
Exe = require('child_process').exec;
http = require('http');
router = router({
server_static: false,
list_dir: false,
serve_cgi: false,
serve_php: false,
});
// Accepts any /example.com => { query: example.com }
router.any("/:query", function(request, response) {
var query = request.params.query;
// Basic length checking, you should implement real checking here.
if (query.length > 255 || query.length < 3)
return response.end("-1");
Sys.print("Updating WHOIS record for " + query);
// This is very bad security, especially without cleansed input.
// Yet again however, this is just a proof of concept example.
child = Exe('jwhois ' + query, function (error, stdout, stderr) {
// Cut off the trailing \n to sanitize return value
stdout = stdout.slice(0, stdout.length - 1);
internal_cache.put(query, stdout);
}); // THIS HAPPENS ASYNC, FIRST REQUEST WILL RETURN undefined
// Return what we have in our cache.
return response.end(internal_cache(query));
});
server = http.createServer(router);
server.on('listening', function() {
var addr;
addr = server.address() || {
address: '0.0.0.0',
port: 8000
};
return router.log("Serving web content at " + addr.address + ":" + addr.port + " - PID: " + process.pid);
});
clean_up = function() {
router.log(" ");
router.log("Server shutting up...");
router.log(" ");
server.close();
return process.exit(0);
};
process.on('SIGINT', clean_up);
process.on('SIGHUP', clean_up);
process.on('SIGQUIT', clean_up);
process.on('SIGTERM', clean_up);
server.listen(8000);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment