javascript 在 Node.js 中从不同的 IP 发送 HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20294190/
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
Send HTTP request from different IPs in Node.js
提问by user3050837
Is there a way to send an HTTP request with a different IP from what I have in Node.js?
有没有办法使用与我在 Node.js 中拥有的 IP 不同的 IP 发送 HTTP 请求?
I want to send a request from an IP that I choose before, and not from the IP of the server or from my computer's IP.
我想从我之前选择的 IP 发送请求,而不是从服务器的 IP 或我的计算机的 IP 发送请求。
I know that Tor Project does this kind of manipulation, but I didn't find any library that Tor uses to do this stuff.
我知道 Tor Project 会执行这种操作,但是我没有找到 Tor 用来执行这些操作的任何库。
Is there any API or Node.js module that Tor uses to handle this kind of private browsing in Node.js?
Tor 是否有任何 API 或 Node.js 模块可用于处理 Node.js 中的这种隐私浏览?
回答by Mike Causer
In the node http module there is a localAddressoption for binding to specific network interface.
在节点 http 模块中有一个localAddress选项用于绑定到特定的网络接口。
var http = require('http');
var options = {
hostname: 'www.example.com',
localAddress: '202.1.1.1'
};
var req = http.request(options, function(res) {
res.on('data', function (chunk) {
console.log(chunk.toString());
});
});
Check out Mikeal's Requeston Github.
在 Github 上查看Mikeal 的请求。
Tor uses SOCKS5 and these two modules can help: socks5-http-clientand socks5-https-client
Tor 使用 SOCKS5,这两个模块可以提供帮助:socks5-http-client和socks5-https-client
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);
});
});
Another option is to use a free web proxy, such as the Hide My Ass web proxy. They also provide a list of ip:port proxieswhich you can use. Both http, https and even SOCKS4/5. Either use the above modules or simply configure your web browser to use one of them.
另一种选择是使用免费的网络代理,例如 Hide My Ass网络代理。他们还提供您可以使用的ip:port 代理列表。http、https 甚至 SOCKS4/5。要么使用上述模块,要么简单地配置您的 Web 浏览器以使用其中之一。
You could even setup your own private http proxy node app and deploy on Heroku. I found a ton of easy to follow examples on Google.
您甚至可以设置自己的私有 http 代理节点应用程序并部署在 Heroku 上。我在 Google 上找到了大量易于理解的示例。