Doing http requests through a SOCKS5 proxy in NodeJS

Viewed 40893

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?

6 Answers

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)

Related