Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delaying proxy, xkcd-style http://wp.me/pfjwm-me
var oc = function(a) {
var o = {};
for(var i=0;i<a.length;i++) {
o[a[i]]='';
}
return o;
}
var http = require('http'),
url = require('url'),
fs = require('fs');
var delayed_sites_raw = fs.readFileSync('timekillers', 'ascii');
var delayed_sites = oc(delayed_sites_raw.split("\n"));
console.log('Delayed sites:' + delayed_sites);
http.createServer(function(request, response) {
var delay = 0;
if (host in delayed_sites) {
delay = 30000;
}
var proxy = http.createClient(80, request.headers['host'])
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.on('response', function (proxy_response) {
proxy_response.pause();
setTimeout(function() {
proxy_response.resume();
}, delay);
proxy_response.pipe(response);
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.pipe(proxy_request);
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.