public
Last active

Node April Fools Proxy

  • Download Gist
README.md
Markdown

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!

index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.