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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11530159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:02:36  来源:igfitidea点击:

Doing http requests through a SOCKS5 proxy in NodeJS

httpnode.jsproxytor

提问by Fredefl

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:

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

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?

但它没有用。
那么,有什么建议吗?

采纳答案by Matthew

I've just published two modules that should help you do this: socks5-http-clientand socks5-https-client.

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

Just use those instead of the default httpmodule. The API is the same. For example:

只需使用它们而不是默认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);
    });
});

回答by Point Networks

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.

我知道我在回答一个老问题,但有一个更好的解决方案可以解决这个问题,关于如何在 Node.js 中使用 sock4 和 sock5 代理。为简单起见,我将使用 request-promise 模块,但您也可以使用裸请求模块。

Requrement: socks-proxy-agent, request-promise

要求: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)  }

回答by Hortinstein

Not a complete answer, but you may want to keep your eye on these two modules.

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

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

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

Support is being added into: https://github.com/Ayms/node-bot.

正在添加支持: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.

我给他发了一封电子邮件,询问他预计什么时候完成,很快就会用这些信息更新这篇文章。

回答by Tim

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

你应该试试 polipo,这对我有用; http://ccm.net/faq/805-installing-an-easy-http-proxy-cache-polipo

回答by Simon Fakir

I had the same problem and used polipoas proxy between node and TOR

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

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

For mac (osx with brew) it worked like this:

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

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

        });

回答by Kamil Tom?ík

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 articlehas some great examples

如果你在 *nix 机器上,你可以使用 tsocks。它将“socksify”整个过程,因此您甚至可以将其用于根本不支持代理的任何内容。这篇文章有一些很好的例子

Basically it's as easy as doing tsocks node myscript.js. I am not sure if it works with tsocks npm startbut you could give it a try (npm starts your code as a subprocess)

基本上它就像做一样简单tsocks node myscript.js。我不确定它是否适用,tsocks npm start但您可以尝试一下(npm 将您的代码作为子进程启动)

All you need is some setup first (put server = 127.0.0.1to etc/tsocks.conf)

所有你需要的是一些设置第一(放server = 127.0.0.1etc/tsocks.conf