【问题标题】:Node HTTP2 request with remote proxy使用远程代理的节点 HTTP2 请求
【发布时间】:2021-04-21 12:02:19
【问题描述】:

我正在尝试在 Node.js 中运行以下示例。它在 HTTP/1.1 下正常运行,但在 HTTP2 下失败:

// Get an HTTP/1.1 tunnel:
const req = http.request({
  method: 'CONNECT',
  host: proxy.host, // This is a remote proxy which works properly with http/1.1
  port: proxy.port,
  path: 'example.com'
});
req.end();

const tunnelledSocket = await new Promise((resolve) => {
  req.on('connect', (_res, socket) => resolve(socket));
});

// We can now read/write to our raw TCP socket to example.com:
const client = http2.connect('https://example.com', {
  // Tunnel this request through the HTTP/1.1 tunnel:
  createConnection: () => tunnelledSocket
});

// Until here all is good, the problem comes when trying to load the url.
const proxiedRequest = client.request({
  ':path': '/test'
});

我从节点收到以下无用的错误:

Error [ERR_HTTP2_ERROR]: Protocol error
    at new NghttpError (node:internal/http2/util:550:5)
    at Http2Session.onSessionInternalError (node:internal/http2/core:776:26) {
  code: 'ERR_HTTP2_ERROR',
  errno: -505
}

我找不到任何关于如何解决它的明确答案。

【问题讨论】:

    标签: javascript node.js http2


    【解决方案1】:

    所以我找到了一个可能的答案。

    创建连接时,你需要用 tls 连接 HTTP2,因为它是一个 HTTPS 请求,但是你需要忽略证书错误,否则,tls 会抱怨你连接到一个没有证书的域.至少这是我设法做到的唯一方法。

    我愿意听取更好的解决方案。

    const client = http2.connect('https://example.com', {
      createConnection: () =>
        tls.connect({
          socket: tunnelledSocket,
          rejectUnauthorized: false,
          ALPNProtocols: ['h2'],
        }),
    });
    

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 2012-04-27
      • 1970-01-01
      • 2023-02-21
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2014-05-27
      相关资源
      最近更新 更多