繁体   English   中英

Node.js http模块未收到WS请求(使用https-proxy-agent)

[英]Node.js http module doesn't receive WS request (using https-proxy-agent)

这个概念很简单,创建一个将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表单:

卷曲-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'请求,或者至少没有将其打印出来(显然也没有用)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM