diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d7188d3b..4668352e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -51,6 +51,7 @@ async function main() { await initWebUI(runtimeConfig.webuiPort || 5099, oneBotManager, hookManager, notificationManager, { host: runtimeConfig.webuiHost, tlsEnabled: runtimeConfig.webuiTls?.enabled, + trustProxy: runtimeConfig.trustProxy, }); } catch (err) { log.error('Failed to start WebUI: ', err); diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 14b1f764..7b8b29c5 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -80,14 +80,6 @@ function purgeExpiredTokens() { } } -/** - * Resolve the client IP for per-IP rate limiting. Default is the TCP - * socket peer (cannot be spoofed by the client). Operators behind a - * reverse proxy must opt in via the `SNOWLUMA_WEBUI_TRUST_PROXY` env - * var; see `./client-ip.ts` for the accepted values. - */ -const trustProxyMode = parseTrustProxy(process.env.SNOWLUMA_WEBUI_TRUST_PROXY); -const getClientIp = makeClientIpResolver(trustProxyMode); async function fetchQqAvatar(uin: string): Promise<{ body: Uint8Array; contentType: string }> { const response = await fetch(`https://q1.qlogo.cn/g?b=qq&nk=${encodeURIComponent(uin)}&s=100`, { @@ -107,8 +99,15 @@ export async function initWebUI( oneBotManager: OneBotManager, hookManager?: HookManager, notificationManager?: NotificationManager, - listener: { host?: string; tlsEnabled?: boolean } = {}, + listener: { host?: string; tlsEnabled?: boolean; trustProxy?: string } = {}, ): Promise<{ port: number }> { + // Resolve the client IP for per-IP rate limiting from the configured + // trust-proxy directive (runtime.json `trustProxy`, env-overridable via + // SNOWLUMA_WEBUI_TRUST_PROXY which loadRuntimeConfig already merged in). + // Default ('') = trust the TCP socket peer only (cannot be spoofed). + const trustProxyMode = parseTrustProxy(listener.trustProxy); + const getClientIp = makeClientIpResolver(trustProxyMode); + const auth = WebuiAuth.load(); const initialPassword = auth.takeInitialPassword(); if (auth.isDevMode()) {