Skip to content

Instantly share code, notes, and snippets.

@mauricesvay
Created November 1, 2012 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauricesvay/3994090 to your computer and use it in GitHub Desktop.
Save mauricesvay/3994090 to your computer and use it in GitHub Desktop.
Plugin for hoxy proxy that dumps responses to disk
/**
* Plugin for hoxy proxy that dumps responses to disk
* https://github.com/greim/hoxy
*
* Saves to disk (almost) everything that goes to your browser (images, videos, etc.)
* Simply add to rules.txt:
* response:@dump()
*/
var fs = require('fs');
var color = {
'reset': '\033[0m',
'bold': '\033[1m',
'italic': '\033[3m',
'underline': '\033[4m',
'blink': '\033[5m',
'black': '\033[30m',
'red': '\033[31m',
'green': '\033[32m',
'yellow': '\033[33m',
'blue': '\033[34m',
'magenta': '\033[35m',
'cyan': '\033[36m',
'white': '\033[37m'
};
function codeColor(code) {
var code_colors = {
'100' : color.blue,
'200' : color.green,
'300' : color.blue,
'400' : color.red,
'500' : color.red
};
code = Math.floor(code / 100) * 100;
return code_colors[code];
}
exports.run = function(api) {
var reqInf = api.getRequestInfo();
var respInf = api.getResponseInfo();
var request_body = api.getRequestBody();
var response_body = '';
var url;
var dumpfile;
if (respInf) {
url = reqInf.host + reqInf.url;
console.log(color.yellow + reqInf.method + " " + url + color.reset);
console.log(reqInf.headers);
if (request_body) {
console.log(request_body);
}
console.log('...........................................................');
console.log('> ' + codeColor(respInf.statusCode) + respInf.statusCode + color.reset);
console.log(respInf.headers);
if (respInf.statusCode >= 200 && respInf.statusCode <= 299 ) {
dumpfile = 'dump/' + url.replace(/[^a-zA-Z0-9]/g,'_');
if (dumpfile.length > 50) {
dumpfile = dumpfile.substr(0,50);
}
switch (respInf.headers['content-type']) {
case 'image/jpg':
case 'image/jpeg':
dumpfile += ".jpg";
break;
case 'image/gif':
dumpfile += ".gif";
break;
case 'image/png':
dumpfile += ".png";
break;
case 'application/javascript':
dumpfile += ".js";
break;
case 'text/css':
dumpfile += ".css";
break;
default:
dumpfile += ".txt";
}
for (var i=0,l=respInf.body.length; i<l; i++) {
response_body += respInf.body[i].toString('binary');
}
fs.writeFile(dumpfile, response_body, 'binary', function (err) {
if (err) throw err;
});
}
console.log('==========================================================');
}
api.notify();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment