fix(onebot): support standard forward WebSocket routes
Accept the standard action, event, and universal paths when a forward WebSocket server listens at the root endpoint. Keep authentication, custom endpoints, embedded listeners, and rejected-path reporting compatible while assigning each accepted connection the correct role. Add real-listener regression coverage for standard paths, trailing slashes, query authentication, invalid paths, and existing lifecycle behavior. Fixes #317
This commit is contained in:
5 files changed
+386
-19
No files matched your search
@@ -131,9 +131,6 @@ export function normalizePath(pathValue: string | undefined): string {
|
||||
}
|
||||
|
||||
export function parseRequestPath(urlValue: string): string {
|
||||
try {
|
||||
return new URL(urlValue, 'ws://127.0.0.1').pathname;
|
||||
} catch {
|
||||
return '/';
|
||||
}
|
||||
const queryIndex = urlValue.indexOf('?');
|
||||
return queryIndex === -1 ? urlValue : urlValue.slice(0, queryIndex);
|
||||
}
|
||||
@@ -4,8 +4,11 @@ import type { IncomingMessage } from 'node:http';
|
||||
import type { DispatchPayload } from '../event-filter';
|
||||
import type { JsonObject, WsServerNetwork } from '../types';
|
||||
import { IOneBotNetworkAdapter, type AdapterStatus, type NetworkAdapterContext } from './adapter';
|
||||
import { normalizePath } from './utils';
|
||||
import { WsServerConnections } from './ws-server-connections';
|
||||
import { normalizePath, parseRequestPath } from './utils';
|
||||
import {
|
||||
WsServerConnections,
|
||||
type WsServerConnectionConfig,
|
||||
} from './ws-server-connections';
|
||||
|
||||
const moduleLog = createLogger('OneBot.WS-Server');
|
||||
|
||||
@@ -17,7 +20,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
|
||||
constructor(name: string, config: WsServerNetwork, ctx: NetworkAdapterContext) {
|
||||
super(name, config, ctx, moduleLog);
|
||||
this.connections = new WsServerConnections(name, config, ctx, this.log, {
|
||||
this.connections = new WsServerConnections(name, standaloneConnectionConfig(config), ctx, this.log, {
|
||||
frame: (event, options) => this.metaFrame(event, options),
|
||||
bootstrap: (options) => this.bootstrapMetaFrames(options),
|
||||
});
|
||||
@@ -90,7 +93,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
}
|
||||
|
||||
protected override onConfigReplaced(next: WsServerNetwork): void {
|
||||
this.connections.updateConfig(next);
|
||||
this.connections.updateConfig(standaloneConnectionConfig(next));
|
||||
}
|
||||
|
||||
onEvent(_event: JsonObject, payload: DispatchPayload): void {
|
||||
@@ -105,9 +108,22 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
wss = new WebSocketServer({
|
||||
host: this.config.host ?? '0.0.0.0',
|
||||
port: this.config.port,
|
||||
path: normalizePath(this.config.path),
|
||||
verifyClient: ({ req }: { req: IncomingMessage }) =>
|
||||
this.connections.authorizeUpgrade(req),
|
||||
verifyClient: (
|
||||
{ req }: { req: IncomingMessage },
|
||||
done: (allow: boolean, code?: number, message?: string) => void,
|
||||
) => {
|
||||
const requestPath = parseRequestPath(req.url ?? '/');
|
||||
if (!this.connections.acceptsUpgradePath(requestPath)) {
|
||||
this.log.debug('[%s] rejected unknown WebSocket path %s', this.name, requestPath);
|
||||
done(false, 400, 'Bad path for WebSocket');
|
||||
return;
|
||||
}
|
||||
if (!this.connections.authorizeUpgrade(req)) {
|
||||
done(false, 401, 'Unauthorized');
|
||||
return;
|
||||
}
|
||||
done(true);
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
this.recordTransportFailure(error);
|
||||
@@ -170,3 +186,7 @@ export class WsServerAdapter extends IOneBotNetworkAdapter<WsServerNetwork> {
|
||||
function isAlreadyClosedError(error: Error): boolean {
|
||||
return (error as NodeJS.ErrnoException).code === 'ERR_SERVER_NOT_RUNNING';
|
||||
}
|
||||
|
||||
function standaloneConnectionConfig(config: WsServerNetwork): WsServerConnectionConfig {
|
||||
return { ...config, role: config.role ?? 'Universal' };
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import type { JsonObject, NetworkBase, WsRole } from '../types';
|
||||
import type { NetworkAdapterContext } from './adapter';
|
||||
import {
|
||||
isAuthorized,
|
||||
normalizePath,
|
||||
parseRequestPath,
|
||||
rawDataToString,
|
||||
safeClose,
|
||||
@@ -27,7 +28,7 @@ const HEARTBEAT_INTERVAL_MS = 30_000;
|
||||
const HEARTBEAT_MAX_MISSED = 2;
|
||||
const HEARTBEAT_DEAD_AFTER_S = (HEARTBEAT_INTERVAL_MS * (HEARTBEAT_MAX_MISSED + 1)) / 1000;
|
||||
|
||||
export type WsServerConnectionConfig = NetworkBase & { role?: WsRole };
|
||||
export type WsServerConnectionConfig = NetworkBase & { path?: string; role?: WsRole };
|
||||
|
||||
interface ForwardConnection {
|
||||
socket: WebSocket;
|
||||
@@ -100,6 +101,10 @@ export class WsServerConnections {
|
||||
if (tokenChanged) this.disconnectAll(1008, 'access token changed');
|
||||
}
|
||||
|
||||
acceptsUpgradePath(requestPath: string): boolean {
|
||||
return resolveForwardRole(requestPath, this.config) !== null;
|
||||
}
|
||||
|
||||
onEvent(payload: DispatchPayload): void {
|
||||
if (!this.acceptingActions || this.connections.size === 0) return;
|
||||
for (const connection of this.connections.values()) {
|
||||
@@ -128,7 +133,13 @@ export class WsServerConnections {
|
||||
return;
|
||||
}
|
||||
|
||||
const role = this.config.role ?? classifyForwardRole(request);
|
||||
const requestPath = parseRequestPath(request.url ?? '/');
|
||||
const role = resolveForwardRole(requestPath, this.config);
|
||||
if (role === null) {
|
||||
this.log.warn('[%s] rejected WebSocket path after upgrade', this.name);
|
||||
safeClose(socket, 1008, 'invalid path');
|
||||
return;
|
||||
}
|
||||
const stopHeartbeat = startHeartbeat(
|
||||
socket,
|
||||
{ intervalMs: HEARTBEAT_INTERVAL_MS, maxMissed: HEARTBEAT_MAX_MISSED },
|
||||
@@ -252,8 +263,24 @@ export class WsServerConnections {
|
||||
}
|
||||
}
|
||||
|
||||
function classifyForwardRole(request: IncomingMessage): WsRole {
|
||||
const path = parseRequestPath(request.url ?? '/');
|
||||
function resolveForwardRole(requestPath: string, config: WsServerConnectionConfig): WsRole | null {
|
||||
if (
|
||||
config.role !== undefined
|
||||
&& (!requestPath.startsWith('/') || requestPath.startsWith('//'))
|
||||
) return null;
|
||||
|
||||
const path = normalizePath(requestPath);
|
||||
if (config.role === undefined) return classifyForwardRole(path);
|
||||
|
||||
const basePath = normalizePath(config.path);
|
||||
if (path === basePath) return config.role;
|
||||
if (config.role !== 'Universal' || basePath !== '/') return null;
|
||||
if (path === '/api') return 'Api';
|
||||
if (path === '/event') return 'Event';
|
||||
return null;
|
||||
}
|
||||
|
||||
function classifyForwardRole(path: string): WsRole {
|
||||
if (path.endsWith('/api')) return 'Api';
|
||||
if (path.endsWith('/event')) return 'Event';
|
||||
return 'Universal';
|
||||
|
||||
Reference in new issue
Block a user