Skip to content

Commit

Permalink
Update example on how to use with Request, with respect to #9
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcg committed Feb 25, 2015
1 parent 6e75d32 commit fb38914
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 44 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ Specify the `socksHost` and `socksPort` options if your SOCKS server isn't runni

## Using with Tor ##

Works great for making HTTP requests through [Tor](https://www.torproject.org/) (see bundled example).
Works great for making HTTPS requests through [Tor](https://www.torproject.org/).

Make sure a Tor server is running locally and run `node example/tor http://en.wikipedia.org/wiki/SOCKS` to test.

## Using with Request ##

To use with [Request](https://github.com/mikeal/request), just pass an agent instance.

```js
var Socks5ClientHttpAgent = require('socks5-http-client/lib/Agent');
var Agent = require('socks5-http-client/lib/Agent');

request({
url: 'http://www.google.com/',
agent: new Socks5ClientHttpAgent({
url: 'http://en.wikipedia.org/wiki/SOCKS',
agentClass: Agent,
agentOptions: {
socksHost: 'my-tor-proxy-host', // Defaults to 'localhost'.
socksPort: 9050 // Defaults to 1080.
})
}
}, function(err, res) {
console.log(res);
console.log(err || res.body);
});
```

Expand Down
17 changes: 17 additions & 0 deletions example/tor-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

/*jshint node:true*/

var request = require('request');

var Agent = require('../lib/Agent');

request({
url: process.argv[2],
agentClass: Agent,
agentOptions: {
socksPort: 9050 // Defaults to 1080.
}
}, function(err, res) {
console.log(res.body);
});
56 changes: 23 additions & 33 deletions example/tor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@

/*jshint node:true*/

var http = require('../');

var options = {
socksPort: 9050, // Tor official port
hostname: 'en.wikipedia.org',
port: 80,
path: '/wiki/SOCKS'
};

var req = http.get(options, function(res) {
var version;

console.log('----------- STATUS -----------');
console.log(res.statusCode);
console.log('----------- HEADERS ----------');
console.log(JSON.stringify(res.headers));
var url = require('url');
var shttp = require('../');

var options = url.parse(process.argv[2]);

options.socksPort = 9050; // Tor default port.

var req = shttp.get(options, function(res) {
res.setEncoding('utf8');

version = process.version.substr(1).split('.');
if (version[0] > 0 || version[1] > 8) {

// The new way, using the readable stream interface (Node >= 0.10.0):
res.on('readable', function() {
console.log('----------- CHUNK ------------');
console.log(res.read());
});
} else {

// The old way, using 'data' listeners (Node <= 0.8.22):
res.on('data', function(chunk) {
console.log('----------- CHUNK ------------');
console.log(chunk);
});
}
res.on('readable', function() {
var data = res.read();

// Check for the end of stream signal.
if (null === data) {
process.stdout.write('\n');
return;
}

process.stdout.write(data);
});
});

req.on('error', function(e) {
console.log('problem with request: ' + e.message);
console.error('Problem with request: ' + e.message);
});

// GET request, so end without sending any data.
req.end();
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
var http = require('http');
var url = require('url');

var Socks5ClientHttpAgent = require('./lib/Agent');
var Agent = require('./lib/Agent');

exports.request = function(options, cb) {
var agent;
Expand All @@ -26,7 +26,7 @@ exports.request = function(options, cb) {
options.port = 80;
}

agent = new Socks5ClientHttpAgent(options);
agent = new Agent(options);
options.agent = agent;

return http.request(options, cb);
Expand Down
7 changes: 4 additions & 3 deletions lib/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ var http = require('http');
var inherits = require('util').inherits;
var socksClient = require('socks5-client');

function Socks5ClientHttpAgent(options) {
function Agent(options) {
http.Agent.call(this, options);

this.socksHost = options.socksHost || 'localhost';
this.socksPort = options.socksPort || 1080;

this.createConnection = socksClient.createConnection;
}

inherits(Socks5ClientHttpAgent, http.Agent);
inherits(Agent, http.Agent);

module.exports = Socks5ClientHttpAgent;
module.exports = Agent;

0 comments on commit fb38914

Please sign in to comment.