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:
1 parent
25db6f0835
commit
8996b35dd1
5 files changed
+72
-5
No files matched your search
@@ -59,6 +59,7 @@ const WebsocketClientConfigSchema = Type.Object({
|
|||||||
token: Type.String({ default: '' }),
|
token: Type.String({ default: '' }),
|
||||||
debug: Type.Boolean({ default: false }),
|
debug: Type.Boolean({ default: false }),
|
||||||
heartInterval: Type.Number({ default: 30000 }),
|
heartInterval: Type.Number({ default: 30000 }),
|
||||||
|
verifyCertificate: Type.Optional(Type.Boolean({ default: true })),
|
||||||
});
|
});
|
||||||
|
|
||||||
const PluginConfigSchema = Type.Object({
|
const PluginConfigSchema = Type.Object({
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
if (!this.connection && this.isEnable) {
|
if (!this.connection && this.isEnable) {
|
||||||
let isClosedByError = false;
|
let isClosedByError = false;
|
||||||
|
|
||||||
this.connection = new WebSocket(this.config.url, {
|
const wsOptions: WebSocket.ClientOptions = {
|
||||||
maxPayload: 50 * 1024 * 1024, // 50 MB
|
maxPayload: 50 * 1024 * 1024, // 50 MB
|
||||||
handshakeTimeout: 2000,
|
handshakeTimeout: 2000,
|
||||||
perMessageDeflate: false,
|
perMessageDeflate: false,
|
||||||
@@ -83,8 +83,14 @@ export class OB11WebSocketClientAdapter extends IOB11NetworkAdapter<WebsocketCli
|
|||||||
'x-client-role': 'Universal', // 为koishi adpter适配
|
'x-client-role': 'Universal', // 为koishi adpter适配
|
||||||
'User-Agent': 'OneBot/11',
|
'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.connection.on('pong', () => {
|
||||||
// this.logger.logDebug('[OneBot] [WebSocket Client] 收到pong');
|
// this.logger.logDebug('[OneBot] [WebSocket Client] 收到pong');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -97,6 +97,57 @@ describe('NapCat Configuration Loaders', () => {
|
|||||||
expect(loaded.musicSignUrl).toBe('');
|
expect(loaded.musicSignUrl).toBe('');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('OneBotConfig websocketClients should apply verifyCertificate default', () => {
|
||||||
|
const partialConfig: Partial<OneBotConfig> = {
|
||||||
|
network: {
|
||||||
|
httpServers: [],
|
||||||
|
httpSseServers: [],
|
||||||
|
httpClients: [],
|
||||||
|
websocketServers: [],
|
||||||
|
websocketClients: [{
|
||||||
|
name: 'test-ws-client',
|
||||||
|
enable: true,
|
||||||
|
url: 'wss://example.com',
|
||||||
|
messagePostFormat: 'array',
|
||||||
|
reportSelfMessage: false,
|
||||||
|
reconnectInterval: 5000,
|
||||||
|
token: '',
|
||||||
|
debug: false,
|
||||||
|
heartInterval: 30000,
|
||||||
|
}],
|
||||||
|
plugins: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const loaded = loadOneBotConfig(partialConfig);
|
||||||
|
expect(loaded.network.websocketClients[0]?.verifyCertificate).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('OneBotConfig websocketClients should preserve explicit verifyCertificate false', () => {
|
||||||
|
const partialConfig: Partial<OneBotConfig> = {
|
||||||
|
network: {
|
||||||
|
httpServers: [],
|
||||||
|
httpSseServers: [],
|
||||||
|
httpClients: [],
|
||||||
|
websocketServers: [],
|
||||||
|
websocketClients: [{
|
||||||
|
name: 'test-ws-client',
|
||||||
|
enable: true,
|
||||||
|
url: 'wss://example.com',
|
||||||
|
messagePostFormat: 'array',
|
||||||
|
reportSelfMessage: false,
|
||||||
|
reconnectInterval: 5000,
|
||||||
|
token: '',
|
||||||
|
debug: false,
|
||||||
|
heartInterval: 30000,
|
||||||
|
verifyCertificate: false,
|
||||||
|
}],
|
||||||
|
plugins: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const loaded = loadOneBotConfig(partialConfig);
|
||||||
|
expect(loaded.network.websocketClients[0]?.verifyCertificate).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
test('NapcatConfig should compile and apply defaults', () => {
|
test('NapcatConfig should compile and apply defaults', () => {
|
||||||
let compiled: ReturnType<typeof TypeCompiler.Compile> | undefined;
|
let compiled: ReturnType<typeof TypeCompiler.Compile> | undefined;
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import GenericForm, { random_token } from './generic_form';
|
|||||||
import type { Field } from './generic_form';
|
import type { Field } from './generic_form';
|
||||||
|
|
||||||
export interface WebsocketClientFormProps {
|
export interface WebsocketClientFormProps {
|
||||||
data?: OneBotConfig['network']['websocketClients'][0]
|
data?: OneBotConfig['network']['websocketClients'][0];
|
||||||
onClose: () => void
|
onClose: () => void;
|
||||||
onSubmit: (
|
onSubmit: (
|
||||||
data: OneBotConfig['network']['websocketClients'][0]
|
data: OneBotConfig['network']['websocketClients'][0]
|
||||||
) => Promise<void>
|
) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebsocketClientFormType = OneBotConfig['network']['websocketClients'];
|
type WebsocketClientFormType = OneBotConfig['network']['websocketClients'];
|
||||||
@@ -26,6 +26,7 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
|||||||
debug: false,
|
debug: false,
|
||||||
heartInterval: 30000,
|
heartInterval: 30000,
|
||||||
reconnectInterval: 30000,
|
reconnectInterval: 30000,
|
||||||
|
verifyCertificate: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const fields: Field<'websocketClients'>[] = [
|
const fields: Field<'websocketClients'>[] = [
|
||||||
@@ -99,6 +100,13 @@ const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
|
|||||||
isRequired: true,
|
isRequired: true,
|
||||||
colSpan: 1,
|
colSpan: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'verifyCertificate',
|
||||||
|
label: 'SSL证书验证',
|
||||||
|
type: 'switch',
|
||||||
|
description: '是否验证SSL证书(wss连接)。关闭后可连接自签名证书的服务器,但会降低安全性',
|
||||||
|
colSpan: 2,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ interface WebsocketClientConfig extends AdapterConfig {
|
|||||||
token: string;
|
token: string;
|
||||||
debug: boolean;
|
debug: boolean;
|
||||||
heartInterval: number;
|
heartInterval: number;
|
||||||
|
verifyCertificate?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface HttpSseServerConfig extends HttpServerConfig {
|
interface HttpSseServerConfig extends HttpServerConfig {
|
||||||
|
|||||||
Reference in new issue
Block a user