问题描述:

I'm planning to do a series of HTTP requests in NodeJS though Tor.

Tor uses SOCKS5 so I went out and searched for a way to proxify HTTP requests in NodeJS.

I'm planning to the the default http.request() function to do the work.
However, I can't seem to find a way to use a proxy with that.
Someone suggested that I could do this:

var http = require("http");

var options = {

host: "localhost",

port: 9050,

path: "http://check.torproject.org",

method: 'GET',

headers: {

Host: "http://check.torproject.org",

}

};

var req = http.request(options, function(res) {

res.on('data', function (chunk) {

console.log('BODY: ' + chunk);

});

});

But it didn't work.

So, any suggestions?

网友答案:

I've just published two modules that should help you do this: socks5-http-client and socks5-https-client.

Just use those instead of the default http module. The API is the same. For example:

require('socks5-http-client').request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});
网友答案:

Not a complete answer, but you may want to keep your eye on these two modules.

https://github.com/Ayms/node-Tor

Support is being added into: https://github.com/Ayms/node-bot.

I sent him an email asking when he expected this to be complete, will update this post soon with that information.

网友答案:

Yo should try with polipo, that work for me; http://ccm.net/faq/805-installing-an-easy-http-proxy-cache-polipo

网友答案:

I had the same problem and used polipo as proxy between node and TOR

node (request) - polilp httproxy:8123 -  polipo - tor (socks5:9050).

For mac (osx with brew) it worked like this:

brew install polipo tor
tor # start top
polipo socksParentProxy=localhost:9050 # start polipo

Working example with request

var request = require('request');
var options = {'url':'https://check.torproject.org/', 'proxy':'http://localhost:8123'}

request(options,
            function (error, response, body) {
            if (error){
                console.log(error);
                return;
            }

            var usingTor = (body.indexOf('Congratulations. This browser is configured to use Tor.')  !== -1);
            expect(usingTor).to.equal(true);   

        });
相关阅读:
Top