在NodeJS中通过SOCKS5代理进行http请求

我打算通过Tor在NodeJS中执行一系列的HTTP请求。
Tor使用SOCKS5,所以我出去寻找一种方法来在NodeJS中提供HTTP请求。
我打算使用默认的http.request()函数来完成这项工作。
但是,我似乎无法find一种方法来使用代理。
有人build议我可以这样做:

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

但它没有工作。
那么,有什么build议?

我刚刚发布了两个模块,可以帮助你做到这一点: socks5- http- client和socks5- https -client 。

只需使用那些而不是默认的http模块。 API是相同的。 例如:

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

不是一个完整的答案,但你可能想要关注这两个模块。

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

支持正在被添加到: https : //github.com/Ayms/node-bot 。

我给他发了一封电子邮件,询问他什么时候能完成这个工作,并且很快就会用这个信息更新这个post。

我有同样的问题,并使用polipo作为节点和TOR之间的代理

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

对于mac(osx with brew ),它的工作原理是这样的:

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

工作示例请求

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