Skip to content

Instantly share code, notes, and snippets.

@bjorne
Last active December 16, 2015 16:28
Show Gist options
  • Save bjorne/5462925 to your computer and use it in GitHub Desktop.
Save bjorne/5462925 to your computer and use it in GitHub Desktop.
Node April Fools Proxy

April Fools Proxy

A vanilla node proxy that gives webpages a treat.

Clone this gist and simply run node index.js. Then configure 127.0.0.1:8484 as a proxy and you're good to go.

Have fun and be sure to report any pranks!

var http = require('http'),
spawn = require('child_process').spawn,
fs = require('fs'),
parseUrl = require('url').parse;
// read lolcat.jpg in current directory
var lolcat = fs.readFileSync('lolcat.jpg');
var sendLolcat = function(req, res, pres) {
/*
* LOLCATS!!!!!!!!
*/
pres.headers['content-type'] = 'image/jpg';
delete pres.headers['content-length'];
res.writeHead(pres.statusCode, pres.headers);
var identify = spawn("identify", ['-']);
identify.stdout.on('data', function(data) {
var dimensions = data.toString().match(/ (\d+x\d+) /);
if (dimensions) {
dimensions = dimensions[1];
console.log('dimensions', dimensions, 'url', req.url);
var convert = spawn('convert', ['-', '-resize', '' + dimensions + '\!', 'jpeg:-']);
convert.stdout.pipe(res);
convert.stderr.on('data', function(err) {
console.log('err', err.toString());
});
convert.stdin.write(lolcat);
convert.stdin.end();
} else {
res.end();
}
});
pres.pipe(identify.stdin);
};
var rotateAndBlurHtml = function(req, res, pres) {
console.log('body replace!');
res.writeHead(pres.statusCode, pres.headers);
pres.setEncoding('utf-8');
pres.on('data', function(chunk) {
// dont do this at home, kids
chunk = chunk.replace(/<\/body>/, '<script>document.body.style.MozTransform="rotate(-3deg)"; document.body.style.webkitFilter="blur(3px)";</script></body>');
res.write(chunk);
});
pres.on('end', function() {
res.end();
});
};
var forwardResponse = function(req, res, pres) {
res.writeHead(pres.statusCode, pres.headers);
pres.on('data', function(chunk) {
res.write(chunk);
});
pres.on('end', function() {
res.end();
});
};
http.createServer(function(req, res) {
var u = parseUrl(req.url);
u.method = req.method;
// proxy request
var preq = http.request(u, function(pres) {
if (pres.headers['content-type'] && pres.headers['content-type'].match(/^image\//)) {
sendLolcat(req, res, pres);
} else if (pres.headers['content-type'] && pres.headers['content-type'].match(/^text\/html/)) {
rotateAndBlurHtml(req, res, pres);
} else {
forwardResponse(req, res, pres);
}
});
preq.on('error', function(err) {
console.log(err);
});
preq.end();
}).listen(8484);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment