带有远程代理的节点HTTP2请求

编程入门 行业动态 更新时间:2024-10-22 09:44:15
本文介绍了带有远程代理的节点HTTP2请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm trying to run the following example in Node. It runs properly with HTTP/1.1 but fails with 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' }); 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: const client = http2.connect('example', { // 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' });

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

I'm getting the following unhelpful error from node:

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 }

我找不到解决方法的明确答案.

I couldn't find any clear answer on how to solve it.

推荐答案

所以我找到了可能的答案.

So I found a possible answer.

在创建连接时,您需要将tls连接到HTTP2,因为它是HTTPS请求,但是您需要忽略证书错误,否则tls会抱怨您正在连接到您不拥有证书的域.至少那是我设法做到的唯一方法.

When creating the connection, you need to connect HTTP2 with tls as it's an HTTPS request, but you need to ignore the certificate errors as otherwise, tls will complain that you are connecting to a domain where you don't own a certificate. At least that is the only way I managed to do it.

我愿意听到更好的解决方案.

I'm open to hear better solutions.

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

更多推荐

带有远程代理的节点HTTP2请求

本文发布于:2023-11-01 20:11:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550296.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!