Doing http requests through a SOCKS5 proxy in NodeJS

丶灬走出姿态 提交于 2019-11-29 20:11:14

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

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

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.

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

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

        });

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!