NodeJS 使用现有TLS套接字发送HTTP/2请求时出错

mkshixfv  于 2023-05-22  发布在  Node.js
关注(0)|答案(1)|浏览(204)

我写了一个脚本来建立到目标网站的TCP连接,用TLS连接 Package 它,然后使用TLS连接打开HTTP/2会话。它在所有网站上都正常工作,但当我向www.example.com发送HTTP/2请求m.twitch.tv,脚本显示此错误:

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: Client network socket disconnected before secure TLS connection was established
    at connResetException (node:internal/errors:717:14)
    at TLSSocket.onConnectEnd (node:_tls_wrap:1595:19)
    at TLSSocket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ECONNRESET',
  path: undefined,
  host: undefined,
  port: undefined,
  localAddress: undefined
}

这是我的剧本:

// Usage: node file.js m.twitch.tv 443
const net = require("net");
const tls = require("tls");
const http2 = require("http2");
const crypto = require("crypto");
const host = process.argv[2];
const port = ~~process.argv[3];
const headers = {};
headers[":authority"] = host;
headers[":method"] = "GET";
headers[":path"] = "/";
headers[":scheme"] = "https";
headers["accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7";
headers["accept-language"] = "en-US,en;q=0.9";
headers["dnt"] = "1";
headers["sec-ch-ua"] = '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"';
headers["sec-ch-ua-mobile"] = "?0";
headers["sec-ch-ua-platform"] = '"Windows"';
headers["sec-fetch-dest"] = "document";
headers["sec-fetch-mode"] = "navigate";
headers["sec-fetch-site"] = "none";
headers["sec-fetch-user"] = "?1";
headers["upgrade-insecure-requests"] = "1";
headers["user-agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.7.7895.91 Safari/537.36";
const socket = net.connect(port, host);
const tlsOptions = {
    ALPNProtocols: ["h2"],
    servername: host,
    socket: socket,
};
const tlsConn = tls.connect(tlsOptions);
tlsConn.on("secureConnect", () => {
    const client = http2.connect("https://" + host, {
        createConnection: () => tlsConn
    });
    client.on("connect", () => {
        let responseCode = 0;
        const request = client.request(headers);
        request.on("response", response => {
            responseCode = response[":status"];
        });
        request.on("data", data => {
            console.log(data.toString(), "\nResponse status: " + responseCode);
            client.close();
        });
    });
})

使用www.google.com可以正常工作。

$ node file.js www.google.com 443
...
events:none;position:fixed;right:0;top:-80px;transition:opacity 0.218s,box-shadow 0.218s}.s2wfp .permission-bar-gradient{box-shadow:0 1px 80px #4285f4;opacity:1;pointer-events:none;animation:allow-alert .75s 0 infinite;animation-direction:alternate;animation-timing-function:ease-out;transition:opacity 0.218s,box-shadow 0.218s}@-webkit-keyframes allow-alert {from{opacity:1}to{opacity:.35}}\x3c/style\x3e\x3cdiv class\x3d\x22permission-bar-gradient\x22\x3e\x3c/div\x3e\x3c/div\x3e\x3c/div\x3e');})();(function(){google.drty&&google.drty(undefined,true);})();});google.drty&&google.drty(undefined,true);</script></body></html>
Response status: 200

但在www.example.com上m.twitch.tv,程序退出时出现此错误。

$ node file.js m.twitch.tv 443
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: Client network socket disconnected before secure TLS connection was established
    at connResetException (node:internal/errors:717:14)
    at TLSSocket.onConnectEnd (node:_tls_wrap:1595:19)
    at TLSSocket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on TLSSocket instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ECONNRESET',
  path: undefined,
  host: undefined,
  port: undefined,
  localAddress: undefined
}
nbysray5

nbysray51#

twitch.tv不支持HTTP2。您可以使用cURL查看:

curl -v -o /dev/null --http2 https://m.twitch.tv

请注意在协商()之后的输出中,您如何获得HTTP1.1响应。
Twitch确实支持HTTP3,但这个协议与HTTP2非常不同。服务器可以支持HTTP1、2和3,但它们选择不支持。很可能他们的目标受众都在使用现代浏览器,他们只支持1和3就可以了。
)如果运行命令,您将得到以下输出:

# curl indicates that it understands these two protocols:
* ALPN: offers h2
* ALPN: offers http/1.1
...
# The server picks from this list:
* ALPN: server accepted http/1.1
...
# Response:
< HTTP/1.1 200 OK

与以下内容相反(本示例使用google.com):

* ALPN: offers h2
* ALPN: offers http/1.1
...
* ALPN: server accepted h2
...
< HTTP/2 200

相关问题