Doing http requests through a SOCKS5 proxy in Node

2019-03-08 02:12发布

问题:

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?

回答1:

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);
    });
});


回答2:

I know I'm answering an old question but there is a better solution available for this question, about how to use sock4 & sock5 proxy in Node.js. For the sake of simplicity, I will be using request-promise module however you can also use bare request module.

Requrement: socks-proxy-agent, request-promise

Example:

async function main() {


var proxy = "socks4://1.2.3.4:35618"

var agent = new SocksProxyAgent(proxy);

var options = {
    uri: 'http://targetUrl',
    agent: agent,
    headers: {
        'User-Agent': 'Request-Promise'
    }
}

try {
    var responce = await rp(options)
} catch(err) {
    console.log(err)
}

console.log(responce)  }


回答3:

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.



回答4:

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



回答5:

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);   

        });


回答6:

If you're on *nix machine, you can use tsocks. It will "socksify" the whole process so you can use it even for anything which does not support proxies at all. This article has some great examples

Basically it's as easy as doing tsocks node myscript.js. I am not sure if it works with tsocks npm start but you could give it a try (npm starts your code as a subprocess)

All you need is some setup first (put server = 127.0.0.1 to etc/tsocks.conf)