【问题标题】:Node.js http module doesn't receive WS request (using https-proxy-agent)Node.js http 模块没有收到 WS 请求(使用 https-proxy-agent)
【发布时间】:2018-09-25 08:22:24
【问题描述】:

这个概念很简单,创建一个将 websocket 请求转发到另一个端口的 http 服务器。

这是我服务器端的代码:

  http.createServer(onRequest).listen(9999);
  function onRequest(request, response) {
    console.log(request);
    ...
  }

因此,如果 http 服务器收到任何请求,它应该在控制台中打印出请求。

然后在客户端(也是一个node.js应用),代码是:

    var HttpsProxyAgent = require('https-proxy-agent');
    var WebSocket = require('ws');
    ...
    var proxy = `http://${config.proxy.host}:${config.proxy.port}`;
    var options = url.parse(proxy);
    agent = new HttpsProxyAgent(options);
    ws = new WebSocket(target, {
      protocol: 'binary',
      agent: agent
    });

现在,当我使用 Charles 拦截请求时,客户端确实发出了请求,这是 Charles 捕获的 curl 表单:

curl -H '主机:target.host.com:8080' -X CONNECT 'https://target.host.com:8080'

问题似乎是这样的

  function onRequest(request, response) {
    console.log(request);
    ...
  }

实际上没有收到任何-X CONNECT 'https://proxy.host.com:9999' 请求,或者至少它没有打印出来(显然它也没有工作)。

【问题讨论】:

    标签: node.js websocket https-proxy-agent


    【解决方案1】:
      var server = http.createServer(onRequest).listen(9999);
      server.on('connect', (req, cltSocket, head) => {
        const srvSocket = net.connect('8080', '127.0.0.1', () => {
          cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
                          'Proxy-agent: Node.js-Proxy\r\n' +
                          '\r\n');
          srvSocket.write(head);
          srvSocket.pipe(cltSocket);
          cltSocket.pipe(srvSocket);
        });
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 2013-12-20
      • 2017-10-11
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2019-04-14
      相关资源
      最近更新 更多