通过 NodeJS 中的 SOCKS5 代理执行 http 请求 [英] Doing http requests through a SOCKS5 proxy in NodeJS

查看:139
本文介绍了通过 NodeJS 中的 SOCKS5 代理执行 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

只需使用它们而不是默认的 http 模块.API 是一样的.例如:

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

这篇关于通过 NodeJS 中的 SOCKS5 代理执行 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆