From ac1ddee54edfb15ba95184ad1687d9a2fe0659f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 2 May 2026 20:11:11 +0800 Subject: [PATCH] Add QQ avatar proxy and render avatars Add a /avatar/:uin proxy endpoint that fetches QQ avatars (fetchQqAvatar) and caches them server-side (30d TTL) with browser-cache headers (30d max-age, immutable). Validate UINs and handle upstream failures (502 if no cache), and log failures. Update ConfigPage and OverviewPage to render avatar from /avatar/:uin (referrerPolicy=no-referrer) and adjust styles (overflow-hidden, object-cover) to replace initial-letter placeholders with actual avatars. --- src/webui/server.ts | 48 +++++++++++++++++++++ webui/src/components/pages/ConfigPage.tsx | 13 +++++- webui/src/components/pages/OverviewPage.tsx | 13 +++++- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/webui/server.ts b/src/webui/server.ts index 67f61dd8..3bcd8002 100644 --- a/src/webui/server.ts +++ b/src/webui/server.ts @@ -22,6 +22,10 @@ const loginAttempts = new Map(); const LOGIN_MAX_ATTEMPTS = 5; const LOGIN_LOCKOUT_MS = 15 * 60 * 1000; // 15 minutes const TOKEN_TTL_MS = 24 * 60 * 60 * 1000; // 24 hours +const AVATAR_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000; +const AVATAR_BROWSER_CACHE_SECONDS = 30 * 24 * 60 * 60; + +const avatarCache = new Map(); function purgeExpiredTokens() { const now = Date.now(); @@ -34,6 +38,23 @@ function getClientIp(req: Request): string { return req.headers.get('x-forwarded-for')?.split(',')[0].trim() ?? '127.0.0.1'; } +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`, { + headers: { + 'User-Agent': 'SnowLuma WebUI', + 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', + }, + }); + + if (!response.ok) { + throw new Error(`avatar upstream responded with ${response.status}`); + } + + const contentType = response.headers.get('content-type') || 'image/jpeg'; + const body = new Uint8Array(await response.arrayBuffer()); + return { body, contentType }; +} + export function initWebUI(port: number = 8080, oneBotManager: OneBotManager, hookManager?: HookManager) { const app = new Hono(); @@ -98,6 +119,33 @@ export function initWebUI(port: number = 8080, oneBotManager: OneBotManager, hoo } }); + app.get('/avatar/:uin', async (c) => { + const uin = c.req.param('uin'); + if (!/^\d{5,12}$/.test(uin)) { + return c.text('invalid uin', 400); + } + + const now = Date.now(); + let cached = avatarCache.get(uin); + if (!cached || cached.expiresAt <= now) { + try { + const avatar = await fetchQqAvatar(uin); + cached = { ...avatar, expiresAt: now + AVATAR_CACHE_TTL_MS }; + avatarCache.set(uin, cached); + } catch (err) { + log.warn('failed to proxy avatar for UIN %s: %s', uin, err instanceof Error ? err.message : String(err)); + if (!cached) return c.text('avatar unavailable', 502); + } + } + + return new Response(cached.body, { + headers: { + 'Content-Type': cached.contentType, + 'Cache-Control': `public, max-age=${AVATAR_BROWSER_CACHE_SECONDS}, immutable`, + }, + }); + }); + // API Routes app.get('/api/status', (c) => { return c.json({ status: 'running' }); diff --git a/webui/src/components/pages/ConfigPage.tsx b/webui/src/components/pages/ConfigPage.tsx index eabde66b..5b9a6934 100644 --- a/webui/src/components/pages/ConfigPage.tsx +++ b/webui/src/components/pages/ConfigPage.tsx @@ -16,6 +16,10 @@ interface ConfigPageProps { onConfigChange: (config: OneBotConfig) => void; } +function qqAvatarUrl(uin: string): string { + return `/avatar/${encodeURIComponent(uin)}`; +} + export function ConfigPage({ qqList, selectedUin, config, saveStatus, onSelectAccount, onSave, onConfigChange, @@ -63,7 +67,7 @@ export function ConfigPage({ }} >
- {q.nickname[0]?.toUpperCase() || '?'} + {q.nickname
- {q.nickname[0]?.toUpperCase() || '?'} + {q.nickname