sessionOptions.sni(仅限服务器)


🌐 sessionOptions.sni (server only)

一个将主机名映射到用于服务器名称指示 (SNI) 支持的 TLS 身份选项的对象。这对于服务器会话是必需的,并且必须至少包含一个条目。特殊键 '*' 指定在没有其他主机名匹配时使用的可选默认/备用身份。如果未提供通配符条目,具有无法识别服务器名称的连接将被 TLS unrecognized_name 警报拒绝。每个条目可以包含:

🌐 An object mapping host names to TLS identity options for Server Name Indication (SNI) support. This is required for server sessions and must contain at least one entry. The special key '*' specifies the optional default/fallback identity used when no other host name matches. If no wildcard entry is provided, connections with unrecognized server names will be rejected with a TLS unrecognized_name alert. Each entry may contain:

  • keys <KeyObject> | <KeyObject[]> TLS 私钥。必需。
  • certs <ArrayBuffer> | <ArrayBufferView> | <ArrayBuffer[]> | <ArrayBufferView[]> TLS 证书。必需。 可选的证书吊销列表。
  • verifyPrivateKey <boolean> 验证私钥。默认值:false
  • port <number> 在 ORIGIN 帧中为此主机名广告的端口(RFC 9412)。默认值:443。仅用于 HTTP/3 会话。
  • authoritative <boolean> 是否在 ORIGIN 帧中包含此主机名。默认值: true。设置为 false 可将主机名排除在 ORIGIN 广告之外。通配符('*')条目无论此设置如何,总是被排除。
const endpoint = await listen(callback, {
  sni: {
    '*': { keys: [defaultKey], certs: [defaultCert] },
    'api.example.com': { keys: [apiKey], certs: [apiCert], port: 8443 },
    'www.example.com': { keys: [wwwKey], certs: [wwwCert], ca: [customCA] },
    'internal.example.com': { keys: [intKey], certs: [intCert], authoritative: false },
  },
}); 

共享的 TLS 选项(例如 ciphersgroupskeylogverifyClient)在会话选项的顶层指定,并适用于所有身份。每个 SNI 条目仅覆盖每个身份的证书字段。

🌐 Shared TLS options (such as ciphers, groups, keylog, and verifyClient) are specified at the top level of the session options and apply to all identities. Each SNI entry overrides only the per-identity certificate fields.

SNI 映射可以在运行时使用 endpoint.setSNIContexts() 替换,该操作会以原子方式为新的会话交换映射,而现有会话则继续使用它们的原始身份。

🌐 The SNI map can be replaced at runtime using endpoint.setSNIContexts(), which atomically swaps the map for new sessions while existing sessions continue to use their original identity.