feat(onebot): make the websocket frame cap configurable and diagnosable (#631)
- expose DEFAULT_MAX_PAYLOAD_BYTES; an inbound frame over the cap now reports its declared byte size, message type and the effective limit, recovered by walking the buffered frame stream so the number is right when a chunk carries several frames, the oversized header spans chunks, or control frames are interleaved; when the walk cannot reach a verdict the error names the cap without a size - truncate close reasons to the RFC 6455 control-frame room on a UTF-8 boundary (an over-long reason previously produced an invalid control frame) - add optional maxPayloadBytes (default 100 MiB unchanged) to the three adapters that read WebSocket frames: wsServers, wsClients and httpServers with enableWebSocket. httpClients rejects the field because it has no WebSocket transport, and the value is part of the binding signature so a change rebinds the transport instead of being saved without taking effect - keep the existing 1009 log lines and append the actionable hint instead of replacing the template - WebUI field (including httpServers once the embedded listener is on) and README docs for the 100 MiB => ~445 s (7:26) inline PCM consequence
This commit is contained in:
1 parent
bd40ac3522
commit
8d9f44b6b0
16 files changed
+1035
-77
No files matched your search
@@ -215,7 +215,12 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
|
||||
protected override bindingSignature(config: HttpServerNetwork): string {
|
||||
const webSocket = config.enableWebSocket === true ? 'ws' : 'http';
|
||||
return `${config.host ?? '0.0.0.0'}:${config.port}${normalizePath(config.path ?? '/')}#${webSocket}`;
|
||||
// The frame cap is fixed when the WebSocketServer is constructed, so a
|
||||
// change must rebind instead of silently keeping the old limit — but only
|
||||
// when this adapter actually owns a WebSocket transport, otherwise the
|
||||
// HTTP listener would be torn down for a value it never uses.
|
||||
const maxPayload = config.enableWebSocket === true ? (config.maxPayloadBytes ?? '') : '';
|
||||
return `${config.host ?? '0.0.0.0'}:${config.port}${normalizePath(config.path ?? '/')}#${webSocket}#${maxPayload}`;
|
||||
}
|
||||
|
||||
protected override onConfigReplaced(next: HttpServerNetwork): void {
|
||||
@@ -242,6 +247,7 @@ export class HttpServerAdapter extends IOneBotNetworkAdapter<HttpServerNetwork>
|
||||
if (this.config.enableWebSocket === true) {
|
||||
webSocketServer = new WebSocketServer({
|
||||
server,
|
||||
maxPayload: this.config.maxPayloadBytes,
|
||||
verifyClient: ({ req }: { req: IncomingMessage }) =>
|
||||
this.webSocketConnections.authorizeUpgrade(req),
|
||||
});
|
||||
|
||||
@@ -2,6 +2,13 @@ import { StreamTransportClosedError } from '../streaming';
|
||||
import type { WebSocket } from '@snowluma/websocket';
|
||||
import type { IncomingMessage } from 'http';
|
||||
|
||||
/**
|
||||
* Appended to a 1009 (frame-over-cap) log line. Shared so the client and server
|
||||
* adapters cannot drift apart on the advice they give.
|
||||
*/
|
||||
export const MAX_PAYLOAD_HINT =
|
||||
"Raise this adapter's maxPayloadBytes, or have the peer send a smaller frame (a URL/file reference or SILK instead of inline PCM)";
|
||||
|
||||
export function isAuthorized(request: IncomingMessage, token: string): boolean {
|
||||
if (!token) {
|
||||
return true;
|
||||
|
||||
@@ -8,7 +8,14 @@ import {
|
||||
} from '../event-filter';
|
||||
import type { JsonObject, WsClientNetwork, WsRole } from '../types';
|
||||
import { IOneBotNetworkAdapter, type AdapterStatus, type NetworkAdapterContext } from './adapter';
|
||||
import { rawDataToString, safeClose, safeSend, safeSendAsync, startHeartbeat } from './utils';
|
||||
import {
|
||||
MAX_PAYLOAD_HINT,
|
||||
rawDataToString,
|
||||
safeClose,
|
||||
safeSend,
|
||||
safeSendAsync,
|
||||
startHeartbeat,
|
||||
} from './utils';
|
||||
|
||||
const moduleLog = createLogger('OneBot.WS-Client');
|
||||
const DEFAULT_RECONNECT_INTERVAL_MS = 5000;
|
||||
@@ -107,7 +114,9 @@ export class WsClientAdapter extends IOneBotNetworkAdapter<WsClientNetwork> {
|
||||
}
|
||||
|
||||
protected override bindingSignature(config: WsClientNetwork): string {
|
||||
return `${config.url}#${config.role ?? 'Universal'}#${Math.max(1000, config.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS)}#${config.accessToken ?? ''}`;
|
||||
// The frame cap is baked into the socket when it is created, so a change
|
||||
// must reconnect instead of silently keeping the old limit.
|
||||
return `${config.url}#${config.role ?? 'Universal'}#${Math.max(1000, config.reconnectIntervalMs ?? DEFAULT_RECONNECT_INTERVAL_MS)}#${config.accessToken ?? ''}#${config.maxPayloadBytes ?? ''}`;
|
||||
}
|
||||
|
||||
protected override willEnable(config: WsClientNetwork): boolean {
|
||||
@@ -160,7 +169,10 @@ export class WsClientAdapter extends IOneBotNetworkAdapter<WsClientNetwork> {
|
||||
headers.Authorization = `Bearer ${this.config.accessToken}`;
|
||||
}
|
||||
|
||||
const socket = new WebSocket(this.config.url, { headers });
|
||||
const socket = new WebSocket(this.config.url, {
|
||||
headers,
|
||||
maxPayload: this.config.maxPayloadBytes,
|
||||
});
|
||||
this.socket = socket;
|
||||
// Per-socket so a hot-reload overlap (old close firing after the new socket
|
||||
// is assigned, see #97) can only ever stop its own keepalive.
|
||||
@@ -255,11 +267,16 @@ export class WsClientAdapter extends IOneBotNetworkAdapter<WsClientNetwork> {
|
||||
});
|
||||
|
||||
socket.on('error', (err: Error) => {
|
||||
// Keep the established `error <url>:` line intact (deployments grep
|
||||
// it); the actionable hint is appended, never substituted.
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
this.log.warn(
|
||||
'[%s] error %s: %s',
|
||||
this.name,
|
||||
this.config.url,
|
||||
err instanceof Error ? err.message : String(err),
|
||||
(err as Error & { code?: number }).code === 1009
|
||||
? `${message}. ${MAX_PAYLOAD_HINT}`
|
||||
: message,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -154,7 +154,9 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
}
|
||||
|
||||
protected override bindingSignature(config: WsServerNetwork): string {
|
||||
return `${config.host ?? '0.0.0.0'}:${config.port}${normalizePath(config.path)}#${config.role ?? 'auto'}#${config.accessToken ?? ''}`;
|
||||
// The frame cap is fixed when the WebSocketServer is constructed, so a
|
||||
// change must rebind instead of silently keeping the old limit.
|
||||
return `${config.host ?? '0.0.0.0'}:${config.port}${normalizePath(config.path)}#${config.role ?? 'auto'}#${config.accessToken ?? ''}#${config.maxPayloadBytes ?? ''}`;
|
||||
}
|
||||
|
||||
protected override onConfigReplaced(next: WsServerNetwork): void {
|
||||
@@ -177,6 +179,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
wss = new WebSocketServer({
|
||||
host: this.config.host ?? '0.0.0.0',
|
||||
port: this.config.port,
|
||||
maxPayload: this.config.maxPayloadBytes,
|
||||
verifyClient: (
|
||||
{ req }: { req: IncomingMessage },
|
||||
done: (allow: boolean, code?: number, message?: string) => void,
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
import type { JsonObject, NetworkBase, WsRole } from '../types';
|
||||
import type { NetworkAdapterContext } from './adapter';
|
||||
import {
|
||||
MAX_PAYLOAD_HINT,
|
||||
isAuthorized,
|
||||
normalizePath,
|
||||
parseRequestPath,
|
||||
@@ -211,10 +212,15 @@ export class WsServerConnections {
|
||||
this.connections.delete(socket);
|
||||
});
|
||||
socket.on('error', (error: Error) => {
|
||||
// Keep the established `socket error:` line intact (deployments grep
|
||||
// it); the actionable hint is appended, never substituted.
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.log.warn(
|
||||
'[%s] socket error: %s',
|
||||
this.name,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
(error as Error & { code?: number }).code === 1009
|
||||
? `${message}. ${MAX_PAYLOAD_HINT}`
|
||||
: message,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user