fix: enable wss connections and add certificate verification toggle in WebSocket Client (#1823)

* fix: enable wss connections and add certificate verification toggle in WebSocket Client

Previous behavior: the WebSocket Client only supported unencrypted ws:// connections; wss:// URLs were not handled and would fail to connect, providing no option to disable TLS certificate validation for development or self-signed certificates.
Fixed behavior: secure WebSocket connections (wss://) are now fully supported. A verifyCertificate configuration option has been introduced, defaulting to true. When set to false, certificate errors (e.g., self-signed certificates) are ignored, enabling safe local testing while maintaining strict validation by default for production.

* fix: improve wss certificate verification and add tests

Previous behavior: wsOptions used 'any' type without type safety, verifyCertificate was added to napcat-protocol without implementation, and no tests existed for the new config option.

Fixed behavior: wsOptions now uses proper WebSocket.ClientOptions type for compile-time safety, verifyCertificate removed from napcat-protocol (no WebSocket client implementation exists there), and comprehensive tests added to validate config loading and default values.
This commit is contained in:
Ventiの酒 authored and GitHub committed 2026-05-13 10:33:47 +08:00
1 parent 25db6f0835
commit 8996b35dd1
5 files changed
+72 -5

No files matched your search

@@ -73,7 +73,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
if (!this.connection && this.isEnable) {
let isClosedByError = false;
this.connection = new WebSocket(this.config.url, {
const wsOptions: WebSocket.ClientOptions = {
maxPayload: 50 * 1024 * 1024, // 50 MB
handshakeTimeout: 2000,
perMessageDeflate: false,
@@ -83,8 +83,14 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
'x-client-role': 'Universal', // 为koishi adpter适配
'User-Agent': 'OneBot/11',
},
};
});
// 如果配置了 verifyCertificate,设置 rejectUnauthorized
if (this.config.verifyCertificate !== undefined) {
wsOptions.rejectUnauthorized = this.config.verifyCertificate;
}
this.connection = new WebSocket(this.config.url, wsOptions);
this.connection.on('pong', () => {
// this.logger.logDebug('[OneBot] [WebSocket Client] 收到pong');
});