Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing http requests through a SOCKS5 proxy in NodeJS

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?

like image 245
Fredefl Avatar asked Jul 17 '12 20:07

Fredefl


People also ask

Can you use SOCKS5 for https?

Unlike HTTP proxies, which can only interpret and work with HTTP and HTTPS webpages, SOCKS5 proxies can work with any traffic. HTTP proxies are high-level proxies usually designed for a specific protocol. While this means you get better connection speeds, they're not nearly as flexible and secure as SOCKS proxies.

Does SOCKS5 work with proxy?

Fewer limitations SOCK5 protocol works with all traffic and can handle any program or protocol because it's low-level and general. This means you can use SOCKS5 proxies with any network.

How do I connect to SOCKS5 proxy?

First, change Configure Proxies to Access the Internet to Manual proxy configuration. Under SOCKS Host enter the URL or IP address of your proxy server. Under Port, enter the number your Netflix SOCKS5 proxy has provided. Tick SOCKS v5 and Proxy DNS when using SOCKS v5.


3 Answers

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);     }); }); 
like image 85
Matthew Avatar answered Oct 03 '22 16:10

Matthew


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)  }
like image 45
Point Networks Avatar answered Oct 03 '22 16:10

Point Networks


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.

like image 30
Hortinstein Avatar answered Oct 03 '22 16:10

Hortinstein