31

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?

Fredefl
  • 1,391
  • 2
  • 17
  • 33

6 Answers6

31

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);
    });
});
Matthew
  • 15,464
  • 2
  • 37
  • 31
13

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)  }
Point Networks
  • 1,071
  • 1
  • 13
  • 35
  • 1
    upvoted! but how do I know if its working, i am connecting to a websocket ws module thtough this and testing some free socks proxies from here https://www.socks-proxy.net/ the thing is it seems to connect regardless of whether the port and host combination is correct or not – PirateApp Oct 05 '18 at 06:11
  • 2
    you can try replacing this URL https://wtfismyip.com/json in place of target URL. If the proxy is working correctly you should see proxy's IP and proxy's ISP as your own IP. I hope you got the point – Point Networks Oct 05 '18 at 06:48
  • Is there a way to use client certificates with this? – alayor Jul 24 '20 at 16:08
2

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.

Hortinstein
  • 2,667
  • 1
  • 22
  • 22
2

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

        });
Simon Fakir
  • 1,712
  • 19
  • 20
1

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)

Kamil Tomšík
  • 2,415
  • 2
  • 28
  • 32
0

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

Tim
  • 585
  • 1
  • 5
  • 14
  • Note that [link-only answers](http://meta.stackoverflow.com/tags/link-only-answers/info) are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference – kleopatra Jul 22 '15 at 07:16