Permalink
Browse files

Support for arbitrary ports.

  • Loading branch information...
tsyd committed Oct 11, 2012
1 parent 14de239 commit 2cb3486e24e7900317b0bce339a8bcebb3135386
Showing with 89 additions and 65 deletions.
  1. +89 −65 lib/proxy-tamper.js
View
@@ -8,91 +8,115 @@ var ProxyTamper = function (options) {
var proxyReq = null;
var tamperBody = null;
var buffers = [];
+ var chunks = [];
+ var body = '';
- _patterns.forEach(function (p) {
- if (req.url.search(p.pattern) != -1) {
- switch (p.tamper.constructor.name) {
- case 'Function':
- switch (parseInt(p.tamper.length)) {
- case 1:
- var reqProxyObj = {
- method: req.method, url: req.url, headers: req.headers,
- onResponse: function (thisOnResultHandler) {
- resp._onResultHandler = thisOnResultHandler;
- }
- };
+ var interceptRequest = function () {
+ _patterns.forEach(function (p) {
+ if (req.url.search(p.pattern) != -1) {
+ switch (p.tamper.constructor.name) {
+ case 'Function':
+ switch (parseInt(p.tamper.length)) {
+ case 1:
+ var reqProxyObj = {
+ method: req.method,
+ url: req.url,
+ headers: req.headers,
+ body: body,
+ onResponse: function (thisOnResultHandler) {
+ resp._onResultHandler = thisOnResultHandler;
+ }
+ };
- p.tamper.call(null, reqProxyObj);
- req.url = reqProxyObj.url;
- req.headers = reqProxyObj.headers;
- req.method = reqProxyObj.method;
+ p.tamper.call(null, reqProxyObj);
+ req.url = reqProxyObj.url;
+ req.headers = reqProxyObj.headers;
+ req.method = reqProxyObj.method;
+ break;
+ case 0:
+ default:
+ tamperBody = p.tamper.call(null, null);
+ break;
+ }
break;
- case 0:
+ case 'String':
+ tamperBody = p.tamper; break;
default:
- tamperBody = p.tamper.call(null, null);
- break;
+ throw new Error('Tamper object must be a function or string but was a '
+ + p.tamper.constructor.name + '.'); break;
}
- break;
- case 'String':
- tamperBody = p.tamper; break;
- default:
- throw new Error('Tamper object must be a function or string but was a '
- + p.tamper.constructor.name + '.'); break;
}
+ });
+
+ if (!resp._onResultHandler && tamperBody) {
+ resp.writeHead(200, {});
+ resp.write(tamperBody, 'utf8');
+ resp.end();
+ return;
}
- });
- if (!resp._onResultHandler && tamperBody) {
- resp.writeHead(200, {});
- resp.write(tamperBody, 'utf8');
- resp.end();
- return;
- }
+ var options = {
+ 'host': req.headers['host'].split(':')[0] || req.headers['host'],
+ 'port': parseInt(req.headers['host'].split(':')[1] || 80),
+ 'path': req.url,
+ 'method': req.method,
+ 'headers': req.headers
+ };
- proxyReq = http.request({'host': req.headers['host'], 'port': 80, 'path': req.url, 'method': req.method});
+ proxyReq = http.request(options, function (proxyResp) {
+ proxyResp.on('data', function (chunk) { buffers.push(chunk); });
+ proxyResp.on('error', function (e) {});
+ proxyResp.on('end', function () {
+ var strBody = '';
+ buffers.forEach(function (buf) { strBody += buf.toString(); });
+ var originalStrBody = strBody;
- proxyReq.addListener('error', function (e) {});
- proxyReq.addListener('response', function (proxyResp) {
- proxyResp.addListener('data', function (chunk) { buffers.push(chunk); });
- proxyResp.addListener('error', function (e) {});
- proxyResp.addListener('end', function () {
- var strBody = '';
- buffers.forEach(function (buf) { strBody += buf.toString(); });
- var originalStrBody = strBody;
+ var proxyRespProxyObj = {
+ statusCode: proxyResp.statusCode, headers: proxyResp.headers, body: strBody,
+ complete: function () {
+ resp.writeHead(this.statusCode, this.headers);
- var proxyRespProxyObj = {
- statusCode: proxyResp.statusCode, headers: proxyResp.headers, body: strBody,
- complete: function () {
- resp.writeHead(this.statusCode, this.headers);
+ if ((strBody != this.body) && (this.body != undefined)) {
+ resp.write(this.body);
+ }
+ else {
+ buffers.forEach(function (buf) {
+ resp.write(buf, 'utf8');
+ });
+ }
- if ((strBody != this.body) && (this.body != undefined)) {
- resp.write(this.body);
- }
- else {
- buffers.forEach(function (buf) {
- resp.write(buf, 'utf8');
- });
+ resp.end();
}
+ };
- resp.end();
+ if (resp._onResultHandler) {
+ resp._onResultHandler.call(null, proxyRespProxyObj);
+ }
+ else {
+ proxyRespProxyObj.complete.call(proxyRespProxyObj);
}
- };
+ });
+ });
- if (resp._onResultHandler) {
- resp._onResultHandler.call(null, proxyRespProxyObj);
- }
- else {
- proxyRespProxyObj.complete.call(proxyRespProxyObj);
- }
+ proxyReq.on('error', function (e) {
+ console.log('error: ' + e.message);
});
- });
- req.addListener('data', function (chunk) {
- proxyReq.write(chunk, 'utf8');
- });
+ chunks.forEach(function (chunk) { proxyReq.write(chunk); });
- req.addListener('end', function () {
proxyReq.end();
+ }
+
+
+ req.on('data', function (chunk) {
+ chunks.push(chunk);
+ });
+
+ req.on('end', function () {
+ chunks.forEach(function (chunk) {
+ body += chunk.toString();
+ });
+ interceptRequest();
});
}).listen(options.port);

0 comments on commit 2cb3486

Please sign in to comment.