Files
NapCatQQ/packages/napcat-webui-frontend/src/components/network_edit/ws_client.tsx
T
Ventiの酒 8996b35dd1 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.
2026-05-13 10:33:47 +08:00

124 lines
2.8 KiB
TypeScript

import GenericForm, { random_token } from './generic_form';
import type { Field } from './generic_form';
export interface WebsocketClientFormProps {
data?: OneBotConfig['network']['websocketClients'][0];
onClose: () => void;
onSubmit: (
data: OneBotConfig['network']['websocketClients'][0]
) => Promise<void>;
}
type WebsocketClientFormType = OneBotConfig['network']['websocketClients'];
const WebsocketClientForm: React.FC<WebsocketClientFormProps> = ({
data,
onClose,
onSubmit,
}) => {
const defaultValues: WebsocketClientFormType[0] = {
enable: false,
name: '',
url: 'ws://localhost:8082',
reportSelfMessage: false,
messagePostFormat: 'array',
token: random_token(16),
debug: false,
heartInterval: 30000,
reconnectInterval: 30000,
verifyCertificate: true,
};
const fields: Field<'websocketClients'>[] = [
{
name: 'enable',
label: '启用',
type: 'switch',
description: '保存后启用此配置',
colSpan: 1,
},
{
name: 'debug',
label: '开启Debug',
type: 'switch',
description: '是否开启调试模式',
colSpan: 1,
},
{
name: 'name',
label: '名称',
type: 'input',
placeholder: '请输入名称',
isRequired: true,
isDisabled: !!data,
},
{
name: 'url',
label: 'URL',
type: 'input',
placeholder: '请输入URL',
isRequired: true,
},
{
name: 'reportSelfMessage',
label: '上报自身消息',
type: 'switch',
description: '是否上报自身消息',
colSpan: 1,
},
{
name: 'messagePostFormat',
label: '消息格式',
type: 'select',
placeholder: '请选择消息格式',
isRequired: true,
options: [
{ key: 'array', value: 'Array' },
{ key: 'string', value: 'String' },
],
colSpan: 1,
},
{
name: 'token',
label: 'Token',
type: 'input',
placeholder: '请输入Token',
},
{
name: 'heartInterval',
label: '心跳间隔',
type: 'input',
placeholder: '请输入心跳间隔',
isRequired: true,
colSpan: 1,
},
{
name: 'reconnectInterval',
label: '重连间隔',
type: 'input',
placeholder: '请输入重连间隔',
isRequired: true,
colSpan: 1,
},
{
name: 'verifyCertificate',
label: 'SSL证书验证',
type: 'switch',
description: '是否验证SSL证书(wss连接)。关闭后可连接自签名证书的服务器,但会降低安全性',
colSpan: 2,
},
];
return (
<GenericForm
data={data}
defaultValues={defaultValues}
onClose={onClose}
onSubmit={onSubmit}
fields={fields}
/>
);
};
export default WebsocketClientForm;