【问题标题】:Use localhost as an http proxy使用 localhost 作为 http 代理
【发布时间】:2019-08-09 03:50:09
【问题描述】:

我正在尝试创建一个 localhost Http 代理,但到目前为止没有任何效果。我要做的是使用托管在端口 8200 上的 localhost 代理向不同的 URL 发送请求。到目前为止,我已经尝试过使用 this node library

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'https://www.google.com/recaptcha/api.js'}).listen(8200); // See (†)

//
// Create your target server
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000); 

我使用 requests npm 库发送请求。

const request = require('request');


    var req = request.get({
      url: site,
      proxy: `http://127.0.0.1:8200`,
      timeout: 10000
    }) 

我收到的错误是这样的:

Error: tunneling socket could not be established, cause=connect ECONNREFUSED 127.0.0.1:8200
    at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
    at Object.onceWrapper (events.js:282:20)
    at ClientRequest.emit (events.js:194:13)
    at Socket.socketErrorListener (_http_client.js:401:9)
    at Socket.emit (events.js:194:13)
    at emitErrorNT (internal/streams/destroy.js:91:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
    at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
    at Object.onceWrapper (events.js:282:20)
    at ClientRequest.emit (events.js:194:13)
    at Socket.socketOnEnd (_http_client.js:435:9)
    at Socket.emit (events.js:199:15)
    at endReadableNT (_stream_readable.js:1141:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=socket hang up
    at ClientRequest.onError (/Users/project/node_modules/tunnel-agent/index.js:177:17)
    at Object.onceWrapper (events.js:282:20)
    at ClientRequest.emit (events.js:194:13)
    at Socket.socketOnEnd (_http_client.js:435:9)
    at Socket.emit (events.js:199:15)
    at endReadableNT (_stream_readable.js:1141:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)
Error: tunneling socket could not be established, cause=read ECONNRESET 

是否可以创建 localhost HTTP 代理?如果是这样,我应该怎么做?

【问题讨论】:

  • 使用一些用于这项工作的代理软件。 Fiddler 适用于 Windows,也有适用于 Linux 和 MacOS 的 beta 版本。
  • 我试图避免使用外部软件,因为我希望将它打包到我的电子应用程序中。

标签: javascript node.js http-proxy


【解决方案1】:

http-proxy docs 并不清楚如何设置动态 http 代理。

代码中的第一个.listen() 并没有真正设置代理服务器,即使它附加到.createProxyServer()。相反,它只是创建一个常规的 http 服务器,就像在 .createServer() 之后的第二个 .listen()

基于examples,我对它进行了一些处理,并将其作为代理 http 请求的最低要求。 (它不处理 https,我不确定它可能还有什么其他限制):

var http = require('http'),
    httpProxy = require('http-proxy');

/* This creates a kind of agent used to fetch content on behalf of an 
   http proxy request. 
*/
var proxy = httpProxy.createProxyServer();

/* This creates a regular web server. Http proxies still use http,
   just a little differently than usual.
*/
http.createServer(function (req, res) {
  /* Urls in proxy requests includes parts, like domain and port, that aren't
     included in the `GET` of regular http requests.
  */
  /* Serve up some content. Normally this would be some page content or 
     something but, since we're supposed to be an http proxy, we tell `proxy`
     to handle the request.
  */
  proxy.web(req, res, { target: req.url });
}).listen(8200);

【讨论】:

  • 是否可以让它处理https?
  • 是的,这也在我链接到的文档中。它需要创建 SSL 证书和一些额外的配置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 2023-02-02
  • 2020-05-12
  • 2020-01-31
相关资源
最近更新 更多