调试 http3 设置

问题描述

我正在尝试为 http3 设置一个测试环境,仅供学习。

到目前为止我做了什么:

  • 使用 dns-01 创建了一个真正的让我们加密证书
  • 使用实验性 CUIC 标志编译 node.js
  • 编译 curl 支持 http3

我用 example内容创建了一个脚本:

'use strict';

const key = getTLSKeySomehow();
const cert = getTLSCertSomehow();

const { createQuicSocket } = require('net');

// Create the QUIC UDP IPv4 socket bound to local IP port 1234
const socket = createQuicSocket({ endpoint: { port: 1234 } });

socket.on('session',async (session) => {
  // A new server side session has been created!

  // The peer opened a new stream!
  session.on('stream',(stream) => {
    // Let's say hello
    stream.end('Hello World');

    // Let's see what the peer has to say...
    stream.setEncoding('utf8');
    stream.on('data',console.log);
    stream.on('end',() => console.log('stream ended'));
  });

  const uni = await session.openStream({ halfOpen: true });
  uni.write('hi ');
  uni.end('from the server!');
});

// Tell the socket to operate as a server using the given
// key and certificate to secure new connections,using
// the fictional 'hello' application protocol.
(async function() {
  await socket.listen({ key,cert,alpn: 'h3-25' });
  console.log('The socket is listening for sessions!');
})();

为了将来的读者,函数 getTLSKeySomehow()getTLSCertSomehow() 可以替换为:

const fs = require("fs")
const key = fs.readFileSync('privkey.pem')
const cert = fs.readFileSync('cert.pem')

然后我尝试通过在 Firefox 中启用 http3 并在 network.http.http3.enabled 中启用功能标志 about:config 来打开网页。使用地址 https://my.dev.domain.name:1234/ 但这不起作用。

使用 curl 不起作用,可能值得注意的是我在 Windows 10 上使用 WSL。每次在 curl 上访问相同的 url 超时。只是为了检查我的设置是否正常:我可以验证 Firefox 和 curl 可以通过 http3 完美访问 www.google.com

当我使用相同的密钥实现第二个 http2 端点时,这可以正常工作,没有任何证书警告。

如何调试我做错的地方?

解决方法

您的代码示例只是实现了 QUIC - 这是 HTTP/3 下的传输协议(类似于 TCP + TLS 是 HTTP/1.1 下的传输协议)。

这意味着您需要额外的代码来在 Quic 之上实现 HTTP/3。理想情况下,您需要一个库,因为它相当复杂。以下是需要实施的规范的最新草案版本的链接:

我认为 node.js 最终也会增加对这些的支持,这样这个任务就会变得更容易。

如果没有 HTTP/3 支持,您可以在您的测试服务器上测试纯 QUIC 流(没有 HTTP 语义)。您可以使用各种 QUIC 库中的任何一个来执行此操作,但可能没有像 curl 这样的开箱即用的 CLI 工具。