js-proxy
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -6,3 +6,3 @@ var join = require('path').join; | ||
function instrument_html(str, options) { | ||
if (str.length == 0) { | ||
if (str.length === 0) { | ||
return ""; | ||
@@ -15,9 +15,9 @@ } | ||
return str; //not instrumenting JSON | ||
} catch (err) { | ||
} catch (ignore) { | ||
} | ||
//second checking if its JS | ||
var checking = instrument.instrument(str, options).toString(); | ||
var $; | ||
if (checking == str) { | ||
var checking = instrument.instrument(str, options).toString(), | ||
$; | ||
if (checking === str) { | ||
$ = cheerio.load(str, { | ||
@@ -31,8 +31,7 @@ xmlMode: false, | ||
// $.root().find('body').prepend("<script src='//128.199.221.233/customVarsIni.js'></script><script src='//128.199.221.233/stringIni.js'></script>"); | ||
$('script').each(function(index, elem) { | ||
var type = $(this).attr('type'); | ||
var src = $(this).attr('src'); | ||
var jsText = $(this).text(); | ||
var temp = jsText; | ||
if (type === undefined || type.toLowerCase() == 'text/javascript') { | ||
$('script').each(function () { | ||
var type = $(this).attr('type'), | ||
jsText = $(this).text(), | ||
temp = jsText; | ||
if (type === undefined || type.toLowerCase() === 'text/javascript') { | ||
temp = instrument.instrument(jsText, options).toString(); | ||
@@ -44,10 +43,10 @@ } | ||
//instrumenting other sources of js | ||
var eventAttributes = ["onafterprint", "onbeforeprint", "onbeforeunload", "onerror", "onhaschange", "onload", "onmessage", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onredo", "onresize", "onstorage", "onundo", "onunload", "onblur", "onchange", "oncontextmenu", "onfocus", "onformchange", "onforminput", "oninput", "oninvalid", "onreset", "onselect", "onsubmit", "onkeydown", "onkeypress", "onkeyup", "onclick", "ondbclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "onmousedown", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onscroll", "onabort", "oncanplay", "oncanplaythrough", "ondurationchange", "onemptied", "onended", "onerror", "onloadeddata", "onloadedmetadata", "onloadstart", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreadystatechange", "onseeked", "onseeking", "onstalled", "onsuspend", "ontimeupdate", "onvolumechange", "onwaiting"]; | ||
var eventAttributes = ["onafterprint", "onbeforeprint", "onbeforeunload", "onerror", "onhaschange", "onload", "onmessage", "onoffline", "ononline", "onpagehide", "onpageshow", "onpopstate", "onredo", "onresize", "onstorage", "onundo", "onunload", "onblur", "onchange", "oncontextmenu", "onfocus", "onformchange", "onforminput", "oninput", "oninvalid", "onreset", "onselect", "onsubmit", "onkeydown", "onkeypress", "onkeyup", "onclick", "ondbclick", "ondrag", "ondragend", "ondragenter", "ondragleave", "ondragover", "ondragstart", "ondrop", "onmousedown", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onscroll", "onabort", "oncanplay", "oncanplaythrough", "ondurationchange", "onemptied", "onended", "onerror", "onloadeddata", "onloadedmetadata", "onloadstart", "onpause", "onplay", "onplaying", "onprogress", "onratechange", "onreadystatechange", "onseeked", "onseeking", "onstalled", "onsuspend", "ontimeupdate", "onvolumechange", "onwaiting"], | ||
text, | ||
prepend = "function I(){", | ||
append = "};", | ||
output = ""; | ||
var text; | ||
var prepend = "function I(){"; | ||
var append = "};"; | ||
var output = ""; | ||
for (var i = 0; i < eventAttributes.length; i++) { | ||
$("[" + eventAttributes[i] + "]").each(function() { | ||
$("[" + eventAttributes[i] + "]").each(function () { | ||
text = prepend + $(this).attr(eventAttributes[i]) + append; | ||
@@ -54,0 +53,0 @@ //instrumenting each attribute only once |
@@ -20,3 +20,3 @@ var falafel = require("falafel-turbo"); | ||
if(output2 == undefined) { | ||
if (output2 === undefined) { | ||
//parse error | ||
@@ -23,0 +23,0 @@ return input; |
{ | ||
"name": "js-proxy", | ||
"description": "A multithreaded man-in-the-middle proxy to extract JavaScript from websites and modify it on the fly", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
104
proxy.js
@@ -1,14 +0,17 @@ | ||
var start = function(options) { | ||
var httpProxy = require('http-proxy'); | ||
var url = require('url'); | ||
var path = require("path"); | ||
var instrument = require(path.join(__dirname, './instrument.js')); | ||
var instrument_html = require(path.join(__dirname, './instrument_html.js')); | ||
var zlib = require('zlib'); | ||
var port = parseInt(options.port); | ||
var start = function (options) { | ||
var i, | ||
httpProxy = require('http-proxy'), | ||
url = require('url'), | ||
path = require("path"), | ||
instrument = require(path.join(__dirname, './instrument.js')), | ||
instrument_html = require(path.join(__dirname, './instrument_html.js')), | ||
zlib = require('zlib'), | ||
port = parseInt(options.port, 10), | ||
cluster = require('cluster'), | ||
threads = parseInt(options.threads, 10); | ||
var cluster = require('cluster'); | ||
var threads = process.argv.slice(2)[0]; | ||
if (!threads) | ||
if (!threads) { | ||
threads = 1; | ||
} | ||
if (cluster.isMaster) { | ||
@@ -19,15 +22,20 @@ for (i = 0; i < threads; i++) { | ||
} else { | ||
httpProxy.createServer(function(req, res, next) { | ||
httpProxy.createServer(function (req, res, next) { | ||
var _writeHead = res.writeHead; | ||
var _process = false; | ||
var _isJS = false; | ||
var _isHTML = false; | ||
var _code, _headers, _contentType; | ||
var _content; | ||
var _writeHead = res.writeHead, | ||
_process = false, | ||
_isJS = false, | ||
_isHTML = false, | ||
_code, | ||
_headers, | ||
_contentType, | ||
_content, | ||
_write = res.write, | ||
_end = res.end, | ||
processedContent = ''; | ||
delete req.headers['accept-encoding']; | ||
res.writeHead = function() { | ||
_code = arguments[0] + ''; | ||
res.writeHead = function (code) { | ||
_code = code.toString(); | ||
_headers = this._headers; | ||
@@ -52,4 +60,4 @@ if (this.getHeader('content-type')) { | ||
}; | ||
var _write = res.write; | ||
res.write = function(data) { | ||
res.write = function (data) { | ||
if (_process) { | ||
@@ -62,6 +70,20 @@ _content = Buffer.concat([_content, data]); | ||
var _end = res.end; | ||
res.end = function() { | ||
res.end = function () { | ||
function callback(err, buffer) { | ||
var _instrumentJS = function (str, options) { | ||
return instrument.instrument(str, options).toString(); | ||
}; | ||
var _instrumentHTML = function (str, options) { | ||
return instrument_html.instrument_html(str, options); | ||
}; | ||
var finish = function () { | ||
_headers['content-length'] = Buffer.byteLength(processedContent, 'utf8'); | ||
_writeHead.call(res, _code, _headers); | ||
_write.call(res, processedContent); | ||
_end.apply(res, arguments); | ||
}; | ||
var callback = function (err, buffer) { | ||
if (!err) { | ||
@@ -80,13 +102,5 @@ res.removeHeader("Content-Encoding"); | ||
} | ||
} | ||
}; | ||
function finish() { | ||
_headers['content-length'] = Buffer.byteLength(processedContent, 'utf8'); | ||
_writeHead.call(res, _code, _headers); | ||
_write.call(res, processedContent); | ||
_end.apply(res, arguments); | ||
} | ||
if (_process) { | ||
var processedContent = ''; | ||
if (this.getHeader("Content-Encoding") === "gzip") { | ||
@@ -101,4 +115,2 @@ zlib.unzip(_content, callback); | ||
processedContent = _instrumentHTML(_content.toString(), options); | ||
} else { | ||
processedContent = buffer.toString(); | ||
} | ||
@@ -112,6 +124,6 @@ finish(); | ||
next(); | ||
}, function(req, res) { | ||
var proxy = new httpProxy.RoutingProxy(); | ||
var buffer = httpProxy.buffer(req); | ||
var urlObj = url.parse(req.url); | ||
}, function (req, res) { | ||
var proxy = new httpProxy.RoutingProxy(), | ||
buffer = httpProxy.buffer(req), | ||
urlObj = url.parse(req.url); | ||
req.headers.host = urlObj.host; | ||
@@ -121,6 +133,6 @@ req.url = urlObj.path; | ||
host: urlObj.hostname, | ||
port: urlObj.port ? urlObj.port : 80, | ||
port: urlObj.port || 80, | ||
buffer: buffer | ||
}); | ||
}).listen(port, function() { | ||
}).listen(port, function () { | ||
console.log("Waiting for requests..."); | ||
@@ -130,12 +142,4 @@ }); | ||
} | ||
function _instrumentJS(str, options) { | ||
return instrument.instrument(str, options).toString(); | ||
} | ||
function _instrumentHTML(str, options) { | ||
return instrument_html.instrument_html(str, options); | ||
} | ||
}; | ||
module.exports.start = start; |
#JS-Proxy | ||
A man-in-the-middle proxy which captures JavaScript on the fly and gives lets you modify it using a callback. | ||
A multithreaded man-in-the-middle proxy which captures JavaScript on the fly and lets you modify it using a callback. | ||
##Installation | ||
``` | ||
npm install js-proxy | ||
``` | ||
The callback function is called for each node returned by [falafel](https://github.com/substack/node-falafel) | ||
var proxy = require('js-proxy'); | ||
var options = {}; | ||
options.port = 9003; | ||
options.preprocess = function(input){ | ||
// add in your preprocessing logic here | ||
return input; | ||
}; | ||
options.postprocess = function(input){ | ||
// add in your postprocessing logic here | ||
return input; | ||
}; | ||
options.instrument = function(node) { | ||
if(node.type == "Program") { | ||
node.update("'use strict;';\n" + node.source()); | ||
} | ||
} | ||
proxy.start(options); | ||
``` js | ||
var proxy = require('js-proxy'); | ||
var options = {}; | ||
options.port = 9003; | ||
options.threads = 3; | ||
options.preprocess = function(input){ | ||
// add in your preprocessing logic here | ||
return input; | ||
}; | ||
options.postprocess = function(input){ | ||
// add in your postprocessing logic here | ||
return input; | ||
}; | ||
options.instrument = function(node) { | ||
if(node.type == "Program") { | ||
node.update("'use strict;';\n" + node.source()); | ||
} | ||
} | ||
proxy.start(options); | ||
``` |
10170
200
33