Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Fixed for latest version of Node and updated example.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsyd committed Aug 18, 2013
1 parent f5db9d3 commit 36069e8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 56 deletions.
67 changes: 47 additions & 20 deletions README.md
Expand Up @@ -4,44 +4,72 @@ A HTTP proxy library for node.js that allows for selective requests to be tamper

## Installation

Either install via npm:
Either install via `npm`:

npm install proxy-tamper

Or via a git clone:
Or via a `git clone`:

cd node_modules
git clone git://github.com/tsyd/proxy-tamper.git
npm link

## Examples
## Example

To mock HTTP requests with a string or result of a function call, specify a regular expression for the URL and a string or function:
The following example demonstrates `proxy-tamper` abilities:

var proxy = require('./lib/proxy-tamper').start({ port: 8080 });
var proxy = require('proxy-tamper').start({port: 8080});

proxy.tamper(/test/, 'tampered');
// block all URLs that contain 'block' in them
proxy.tamper(/block/, 'This content is blocked!');

// disallow Google
proxy.tamper(/google/, function (request) {
request.url = request.url.replace(/google/g, 'bing');
});

// replace all instances of 'Apple' with 'Orange' in Techcrunch articles
proxy.tamper(/techcrunch.com.*\/$/, function (request) {
// gzip encoding is not supported when tampering the body
delete request.headers['accept-encoding'];

request.onResponse(function (response) {
// tamper the body
response.body = response.body.replace(/Apple/g, 'Orange');
response.headers['server'] = 'proxy-tamper 1337';

// complete the response
response.complete();
});
});

To tamper HTTP requests with a string or result of a function call, specify a regular expression for the URL and a string or function:

proxy.tamper(/block/, 'This content is blocked!');

The response body of all URLs that contain `block` in them will be `This content is blocked!'.

It is possible to manipulate the original request before it's executed over the proxy. The request object has access to `request.url`, `request.headers`, and `request.method`:

proxy.tamper(/translate\.google\..*?\/translate_a\/t/, function (request) {
// disallow translations
request.url = request.url.replace(/hl=../, 'hl=en').replace(/tl=../, 'tl=en')
.replace(/sl=../, 'sl=en').replace(/text=.*/, 'text=No+translation+for+you!');
proxy.tamper(/google/, function (request) {
request.url = request.url.replace(/google/g, 'bing');
});

It is also possible to modify the response before proxying it back to the original request by specifying an `onResponse` handler:

// replace all instances of 'Apple' with 'Orange' in Techcrunch articles
proxy.tamper(/techcrunch.com.*\/$/, function (request) {
// gzip encoding is not supported when tampering the body
delete request.headers['accept-encoding'];

proxy.tamper(/tsyd\.net\/$/, function (request) {
request.onResponse(function (response) {
// called when we have the response from the tampered url
response.body = reverseHeadings(response.body); // reverseHeadings defined elsewhere
// tamper the body
response.body = response.body.replace(/Apple/g, 'Orange');
response.headers['server'] = 'proxy-tamper 1337';
// the onResponse handler must complete the response

// complete the response
response.complete();
});
});
});

The `onResponse` handler has read and write access to a subset of the `http.ClientResponse` response object, namely: `response.headers`, `response.statusCode`, and `response.url`. The `onResponse` handler may also modfiy the string representation of the response body by accessing `response.body`.
Expand All @@ -54,9 +82,8 @@ To test the example application, simply run:

Then set your browser to use `127.0.0.1:8080` as the HTTP proxy, and visit the following URLs:

* <http://stackoverflow.com/test>
* <http://tsyd.net>
* <http://translate.google.com> (Attempt to translate something.)
* <http://google.com/block/>
* <http://techcrunch.com/tag/apple/>

## License

Expand Down
52 changes: 20 additions & 32 deletions example.js
@@ -1,38 +1,26 @@
var proxy = require('./lib/proxy-tamper');
var proxy = require('./lib/proxy-tamper').start({port: 8080});

proxy.start({ port: 8080 }, function (p) {
p.tamper(/\/test/, 'tampered');
// block all URLs that contain 'block' in them
proxy.tamper(/block/, 'This content is blocked!');

p.tamper(/translate\.google\..*?\/translate_a\/t/, function (request) {
// disallow translations
request.url = request.url.replace(/hl=../, 'hl=en').replace(/tl=../, 'tl=en')
.replace(/sl=../, 'sl=en').replace(/text=.*/, 'text=No+translation+for+you!');
});
// disallow Google
proxy.tamper(/google/, function (request) {
request.url = request.url.replace(/google/g, 'bing');
});

p.tamper(/tsyd\.net\/$/, function (request) {
request.onResponse(function (response) {
// called when we have the response from the tampered url

response.body = reverseHeadings(response.body);
response.headers['server'] = 'proxy-tamper 1337';

// the onResponse handler must complete the response
response.complete();
});
});
});
// replace all instances of 'Apple' with 'Orange' in Techcrunch articles
proxy.tamper(/techcrunch.com.*\/$/, function (request) {
console.log('tampering ' + request.url);

function reverseHeadings (str) {
var matches = str.match(/(<h\d>.*?<\/h\d>)/mg);
// gzip encoding is not supported when tampering the body
delete request.headers['accept-encoding'];

if (matches) {
// reverse the text within all header tags
matches.forEach(function (match) {
var parts = match.match(/(<h\d>)(.*?)(<\/h\d>)/);
str = str.replace(parts[0],
parts[1] + parts[2].split('').reverse().join('') + parts[3]);
});
}
request.onResponse(function (response) {
// tamper the body
response.body = response.body.replace(/Apple/g, 'Orange');
response.headers['server'] = 'proxy-tamper 1337';

return str;
};
// complete the response
response.complete();
});
});
3 changes: 1 addition & 2 deletions lib/proxy-tamper.js
Expand Up @@ -127,8 +127,7 @@ var ProxyTamper = function (options) {
};
}

exports.start = function (options, block) {
exports.start = function (options) {
var proxyTamper = new ProxyTamper(options);
block.call(proxyTamper, proxyTamper);
return proxyTamper;
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -8,15 +8,15 @@
"author": {
"name": "Thomas Sydorowski"
},
"version": "0.1.1",
"version": "0.1.3",
"licenses": [
{
"type": "MIT",
"url": "https://raw.github.com/tsyd/proxy-tamper/master/LICENSE"
}
],
"engines": {
"node": ">=0.8.0"
"node": ">=0.10.0"
},
"main": "./lib/proxy-tamper.js",
"homepage": "http://github.com/tsyd/proxy-tamper",
Expand Down

0 comments on commit 36069e8

Please sign in to comment.