From 82d5f782a3cebc40e64fa073dd61b01b1dd90df9 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 14:39:35 +0800 Subject: [PATCH] feat(webui): system settings panel (port/host/TLS/trust-proxy) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New Settings → 服务 tab (SystemPanel) over the /api/system/settings + TLS cert API: edit port / bind host / trust-proxy, paste cert+key PEM to enable HTTPS, delete cert. Flags env-overridden fields ('被环境变量覆盖') and shows a restart-to-apply banner (no self-restart). Typed systemSettings API client domain. View layer — verified by tsc + vite build (webui has no component-test infra). --- .../src/components/pages/settings-page.tsx | 5 +- .../src/components/settings/system-panel.tsx | 205 ++++++++++++++++++ packages/webui/src/lib/api/client.ts | 18 +- packages/webui/src/lib/api/types.ts | 12 + packages/webui/src/router/index.tsx | 2 +- packages/webui/src/types.ts | 17 ++ 6 files changed, 256 insertions(+), 3 deletions(-) create mode 100644 packages/webui/src/components/settings/system-panel.tsx diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 2ee74e48..79ea4c72 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -2,7 +2,7 @@ import { createContext, useContext, useId, useRef, useState, type ReactNode } fr import { motion } from 'motion/react'; import { Accessibility, AlertTriangle, Bell, Bug, Check, Clock, Code2, Download, ExternalLink, Github, Image as ImageIcon, - Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, ShieldCheck, + Info, KeyRound, Loader2, Monitor, Moon, Palette, PanelTop, Plus, RefreshCw, RotateCcw, Server, ShieldCheck, Sparkles, Star, Sun, Tag, Upload, Trash2, } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card'; @@ -41,12 +41,14 @@ import { useAppState } from '@/contexts/AppStateContext'; import { cn } from '@/lib/utils'; import { settingsRoute, type SettingsTab } from '@/router'; import { NotificationsPanel } from '@/components/settings/notifications-panel'; +import { SystemPanel } from '@/components/settings/system-panel'; const TABS: { key: SettingsTab; label: string; icon: typeof Sun }[] = [ { key: 'appearance', label: '外观', icon: Palette }, { key: 'data', label: '数据与格式', icon: RefreshCw }, { key: 'advanced', label: '高级', icon: Code2 }, { key: 'account', label: '账号安全', icon: ShieldCheck }, + { key: 'system', label: '服务', icon: Server }, { key: 'notifications', label: '通知', icon: Bell }, { key: 'about', label: '关于', icon: Info }, ]; @@ -78,6 +80,7 @@ export function SettingsPage() { {tab === 'data' && } {tab === 'advanced' && } {tab === 'account' && } + {tab === 'system' && } {tab === 'notifications' && } {tab === 'about' && } diff --git a/packages/webui/src/components/settings/system-panel.tsx b/packages/webui/src/components/settings/system-panel.tsx new file mode 100644 index 00000000..399fd484 --- /dev/null +++ b/packages/webui/src/components/settings/system-panel.tsx @@ -0,0 +1,205 @@ +// Settings → 服务 tab (Wave A1). Edits the WebUI listener's own config +// (port / bind host / trust-proxy / HTTPS). Listener-level changes are +// persisted but apply only after a restart — there is no supervisor to +// self-restart, so we never offer a "restart now" button, only a banner. +// Fields currently pinned by SNOWLUMA_* env vars are flagged (edits to them +// won't take effect until the env var is removed). +import { useEffect, useState } from 'react'; +import { motion } from 'motion/react'; +import { AlertTriangle, Loader2, Lock, Save, ShieldCheck, Trash2 } from 'lucide-react'; +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; +import { Badge } from '@/components/ui/badge'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { ToggleSwitch } from '@/components/ui/toggle-switch'; +import { useApi } from '@/lib/api'; +import { cn } from '@/lib/utils'; +import type { SystemSettingsResponse } from '@/types'; + +const TEXTAREA_CLS = + 'w-full min-h-28 rounded-lg border border-border bg-background px-3 py-2 font-mono text-xs ' + + 'outline-none focus-visible:ring-2 focus-visible:ring-ring'; + +export function SystemPanel() { + const api = useApi(); + const [data, setData] = useState(null); + const [loading, setLoading] = useState(true); + const [saving, setSaving] = useState(false); + const [msg, setMsg] = useState<{ kind: 'ok' | 'err'; text: string } | null>(null); + + // editable form state + const [port, setPort] = useState(''); + const [host, setHost] = useState('0.0.0.0'); + const [trustProxy, setTrustProxy] = useState(''); + const [tlsEnabled, setTlsEnabled] = useState(false); + const [certPem, setCertPem] = useState(''); + const [keyPem, setKeyPem] = useState(''); + + const load = async () => { + try { + const res = await api.systemSettings.get(); + setData(res); + setPort(String(res.settings.webuiPort)); + setHost(res.settings.webuiHost); + setTrustProxy(res.settings.trustProxy); + setTlsEnabled(res.settings.tlsEnabled); + } catch { + setMsg({ kind: 'err', text: '加载系统设置失败' }); + } finally { + setLoading(false); + } + }; + + useEffect(() => { void load(); }, []); // eslint-disable-line react-hooks/exhaustive-deps + + const overridden = (field: string) => data?.envOverrides.includes(field) ?? false; + const flash = (kind: 'ok' | 'err', text: string) => { + setMsg({ kind, text }); + window.setTimeout(() => setMsg(null), 4000); + }; + + const saveSettings = async () => { + const portNum = Number(port); + if (!Number.isInteger(portNum) || portNum <= 0 || portNum > 65535) { + flash('err', '端口必须是 1–65535 的整数'); + return; + } + setSaving(true); + try { + await api.systemSettings.save({ webuiPort: portNum, webuiHost: host.trim(), trustProxy, tlsEnabled }); + await load(); + flash('ok', '已保存,重启 SnowLuma 后生效'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '保存失败'); + } finally { + setSaving(false); + } + }; + + const uploadCert = async () => { + if (!certPem.trim() || !keyPem.trim()) { flash('err', '请同时填入证书与私钥'); return; } + setSaving(true); + try { + await api.systemSettings.uploadCert(certPem, keyPem); + setCertPem(''); setKeyPem(''); + await load(); + flash('ok', '证书已保存,重启后生效'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '证书保存失败'); + } finally { + setSaving(false); + } + }; + + const deleteCert = async () => { + setSaving(true); + try { + await api.systemSettings.deleteCert(); + await load(); + flash('ok', '证书已删除'); + } catch (e) { + flash('err', e instanceof Error ? e.message : '删除失败'); + } finally { + setSaving(false); + } + }; + + if (loading) { + return 加载中…; + } + + const EnvBadge = ({ field }: { field: string }) => + overridden(field) + ? 被环境变量覆盖 + : null; + + return ( + + {/* restart-to-apply notice */} + + + 监听相关设置(端口 / 绑定地址 / HTTPS)属服务级配置,保存后需重启 SnowLuma 才生效;本机无自动重启。 + + + {msg && ( + {msg.text} + )} + + + + 服务监听 + WebUI 当前监听端口:{data?.listeningPort} + + + + 端口 + setPort(e.target.value)} inputMode="numeric" placeholder="5099" /> + + + 绑定地址 + setHost(e.target.value)} + className="h-9 rounded-lg border border-border bg-background px-2 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring" + > + 0.0.0.0(所有网卡) + 127.0.0.1(仅本机) + + + + 信任代理 (trust-proxy) + setTrustProxy(e.target.value)} placeholder="留空=只信任 socket 对端;1=信任反代头" /> + 仅在 WebUI 位于受信任反向代理之后时才设为 1 / loopback / IP 列表。 + + + + {saving ? : } 保存 + + + + + + + + + HTTPS / TLS + + + {data?.hasCert ? '已安装证书。' : '尚未安装证书。'} 启用 TLS 需先安装有效的证书与私钥(PEM)。 + + + + + 启用 HTTPS + + + + + 证书 (cert.pem) + setCertPem(e.target.value)} placeholder="-----BEGIN CERTIFICATE-----" /> + + + 私钥 (key.pem) + setKeyPem(e.target.value)} placeholder="-----BEGIN PRIVATE KEY-----" /> + + + + {saving ? : } 保存证书 + + {data?.hasCert && ( + + 删除证书 + + )} + + + 私钥仅写入服务器 config/key.pem,不会回显。证书无效时保存会被拒绝;若启用了 TLS 但证书加载失败,启动时会自动回退到 HTTP。 + + + + + ); +} diff --git a/packages/webui/src/lib/api/client.ts b/packages/webui/src/lib/api/client.ts index ee75eb0a..17cb285a 100644 --- a/packages/webui/src/lib/api/client.ts +++ b/packages/webui/src/lib/api/client.ts @@ -1,4 +1,4 @@ -import type { AccountConnections, HookProcessInfo, LogEntry, LogLevel, NotificationDeliveryRecord, NotificationsConfig, QQInfo, SystemInfo, UiAppearance, UiConfig, UpdateInfo } from '@/types'; +import type { AccountConnections, HookProcessInfo, LogEntry, LogLevel, NotificationDeliveryRecord, NotificationsConfig, QQInfo, SystemInfo, SystemSettingsPatch, SystemSettingsResponse, UiAppearance, UiConfig, UpdateInfo } from '@/types'; import type { PasswordRule } from '@/components/pages/change-password-page'; import { normalizeOneBotConfig } from '@/lib/onebot-config'; import { @@ -45,6 +45,7 @@ class HttpApiClient implements ApiClient { readonly update: ApiClient['update']; readonly ui: ApiClient['ui']; readonly notifications: ApiClient['notifications']; + readonly systemSettings: ApiClient['systemSettings']; constructor(opts: CreateApiClientOptions = {}) { this.tokenStore = opts.tokenStore ?? localStorageTokenStore(DEFAULT_TOKEN_KEY); @@ -98,6 +99,21 @@ class HttpApiClient implements ApiClient { this.getJson(`/api/update/check${force ? '?force=true' : ''}`), }; + this.systemSettings = { + get: () => this.getJson('/api/system/settings'), + save: (patch: SystemSettingsPatch) => + this.postJson<{ settings: SystemSettingsResponse['settings']; restartRequiredToApply: boolean }>( + '/api/system/settings', + patch, + ), + uploadCert: async (cert: string, key: string) => { + await this.postJson<{ success: boolean }>('/api/system/tls/cert', { cert, key }); + }, + deleteCert: async () => { + await this.fetchJson<{ success: boolean }>('/api/system/tls/cert', { method: 'DELETE' }); + }, + }; + this.ui = { get: () => this.getJson<{ config: UiConfig }>('/api/ui').then((d) => d.config), save: async (config) => { diff --git a/packages/webui/src/lib/api/types.ts b/packages/webui/src/lib/api/types.ts index 8f8096d3..8257a480 100644 --- a/packages/webui/src/lib/api/types.ts +++ b/packages/webui/src/lib/api/types.ts @@ -8,6 +8,9 @@ import type { OneBotConfig, QQInfo, SystemInfo, + SystemSettings, + SystemSettingsPatch, + SystemSettingsResponse, UiAppearance, UiConfig, UpdateInfo, @@ -74,6 +77,15 @@ export interface ApiClient { save(uin: string, config: OneBotConfig): Promise; }; + // ---- WebUI listener self-config (port / host / TLS / trust-proxy) ---- + systemSettings: { + get(): Promise; + save(patch: SystemSettingsPatch): Promise<{ settings: SystemSettings; restartRequiredToApply: boolean }>; + /** Validate + write config/cert.pem + key.pem (restart to apply). */ + uploadCert(cert: string, key: string): Promise; + deleteCert(): Promise; + }; + // ---- notifications (account up/down webhooks) ---- notifications: { /** Global channel store (channels + debounce). Bearer-gated. */ diff --git a/packages/webui/src/router/index.tsx b/packages/webui/src/router/index.tsx index 3b479b90..b26204ff 100644 --- a/packages/webui/src/router/index.tsx +++ b/packages/webui/src/router/index.tsx @@ -65,7 +65,7 @@ const logsRoute = createRoute({ }); /** Settings sub-tabs — also the contract for the `?tab=` deep link. */ -export const SETTINGS_TABS = ['appearance', 'data', 'advanced', 'account', 'notifications', 'about'] as const; +export const SETTINGS_TABS = ['appearance', 'data', 'advanced', 'account', 'system', 'notifications', 'about'] as const; export type SettingsTab = (typeof SETTINGS_TABS)[number]; export const settingsRoute = createRoute({ diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 04813c8b..0de3ea05 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -141,6 +141,23 @@ export interface NotificationDeliveryRecord { export type NetworkKind = keyof OneBotNetworks; +// WebUI listener self-config (Wave A1). Listener-level changes are +// restart-to-apply; `envOverrides` lists fields currently pinned by env vars. +export interface SystemSettings { + webuiPort: number; + webuiHost: string; + tlsEnabled: boolean; + trustProxy: string; +} +export type SystemSettingsPatch = Partial; +export interface SystemSettingsResponse { + settings: SystemSettings; + hasCert: boolean; + envOverrides: string[]; + listeningPort: number; + restartRequiredToApply: boolean; +} + export interface SystemInfo { hostname: string; platform: string;
仅在 WebUI 位于受信任反向代理之后时才设为 1 / loopback / IP 列表。
+ 私钥仅写入服务器 config/key.pem,不会回显。证书无效时保存会被拒绝;若启用了 TLS 但证书加载失败,启动时会自动回退到 HTTP。 +