From fb389143bc48918a35502dcd01d129392b158801 Mon Sep 17 00:00:00 2001 From: Matthew Caruana Galizia Date: Wed, 25 Feb 2015 13:30:02 -0600 Subject: [PATCH] Update example on how to use with Request, with respect to #9 --- README.md | 15 ++++++----- example/tor-request.js | 17 +++++++++++++ example/tor.js | 56 +++++++++++++++++------------------------- index.js | 4 +-- lib/Agent.js | 7 +++--- 5 files changed, 55 insertions(+), 44 deletions(-) create mode 100644 example/tor-request.js diff --git a/README.md b/README.md index c31f4df..09ae013 100644 --- a/README.md +++ b/README.md @@ -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); }); ``` diff --git a/example/tor-request.js b/example/tor-request.js new file mode 100644 index 0000000..3077b77 --- /dev/null +++ b/example/tor-request.js @@ -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); +}); diff --git a/example/tor.js b/example/tor.js index 0a8ac3c..5ece1a0 100644 --- a/example/tor.js +++ b/example/tor.js @@ -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(); diff --git a/index.js b/index.js index 63ba100..caff8aa 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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); diff --git a/lib/Agent.js b/lib/Agent.js index a48d4f6..b7f6346 100644 --- a/lib/Agent.js +++ b/lib/Agent.js @@ -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;