miaopanel
1<script setup lang="ts">
2import { computed, inject, onMounted, ref } from 'vue'
3import { api } from '../api'
4import type { AISettings, UpdateInfo, UpdateResult } from '../types'
5import { time } from '../format'
6
7interface PanelSettings {
8 entry: string
9 session_ttl_minutes: number
10 https: boolean
11 app_domain: string
12 cert_path: string
13 cert_issuer: string
14 cert_not_after: number
15 cert_self_signed: boolean
16 port: string
17 version: string
18}
19
20interface AppAccessResult {
21 domain: string
22 issuer: string
23 notAfter: number
24 daysLeft: number
25 url: string
26 staging?: boolean
27 message?: string
28}
29
30const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
31
32const settings = ref<PanelSettings | null>(null)
33const loading = ref(true)
34
35const entry = ref('')
36const entryBusy = ref(false)
37
38const ttl = ref(30)
39const ttlBusy = ref(false)
40
41const domain = ref('')
42const appBusy = ref(false)
43const appResult = ref('')
44const staging = ref(false)
45const appElapsed = ref(0)
46let appTimer = -1
47
48function startAppTimer() {
49 appElapsed.value = 0
50 stopAppTimer()
51 appTimer = window.setInterval(() => {
52 appElapsed.value += 1
53 }, 1000)
54}
55
56function stopAppTimer() {
57 if (appTimer >= 0) {
58 window.clearInterval(appTimer)
59 appTimer = -1
60 }
61}
62
63const updateBusy = ref(false)
64const updateInfo = ref<UpdateInfo | null>(null)
65const updateMsg = ref('')
66const applyBusy = ref(false)
67const applyNote = ref('')
68
69const ai = ref<AISettings | null>(null)
70const aiEnabled = ref(false)
71const aiBaseURL = ref('')
72const aiModel = ref('')
73const aiKey = ref('')
74const aiClearKey = ref(false)
75const aiTemperature = ref(0.3)
76const aiPrompt = ref('')
77const aiBusy = ref(false)
78const aiTesting = ref(false)
79const aiTestMsg = ref('')
80
81const certDaysLeft = computed<number | null>(() => {
82 const ts = settings.value?.cert_not_after
83 if (!ts) {
84 return null
85 }
86 return Math.max(0, Math.floor((ts * 1000 - Date.now()) / 86400000))
87})
88
89// The panel keeps listening on its own port (8888 by default), so the mobile
90// app must include it: only 443 can be omitted.
91const appUrl = computed<string>(() => {
92 const s = settings.value
93 if (!s?.app_domain) {
94 return ''
95 }
96 const port = s.port === '443' || !s.port ? '' : `:${s.port}`
97 return `https://${s.app_domain}${port}/${s.entry}`
98})
99
100async function load() {
101 loading.value = true
102 try {
103 const data = await api.get<PanelSettings>('api/v1/settings')
104 settings.value = data
105 entry.value = data.entry
106 ttl.value = data.session_ttl_minutes
107 if (data.app_domain) {
108 domain.value = data.app_domain
109 }
110 } catch (e) {
111 notify((e as Error).message, 'error')
112 } finally {
113 loading.value = false
114 }
115}
116
117async function loadAI() {
118 try {
119 const data = await api.get<AISettings>('api/v1/settings/ai')
120 ai.value = data
121 aiEnabled.value = data.enabled
122 aiBaseURL.value = data.base_url
123 aiModel.value = data.model
124 aiTemperature.value = data.temperature
125 aiPrompt.value = data.system_prompt
126 aiKey.value = ''
127 aiClearKey.value = false
128 aiTestMsg.value = ''
129 } catch (e) {
130 notify((e as Error).message, 'error')
131 }
132}
133
134async function saveAI() {
135 if (!aiModel.value.trim()) {
136 notify('请填写模型名称', 'error')
137 return
138 }
139 aiBusy.value = true
140 try {
141 const body: Record<string, unknown> = {
142 enabled: aiEnabled.value,
143 base_url: aiBaseURL.value.trim(),
144 model: aiModel.value.trim(),
145 temperature: Number(aiTemperature.value),
146 system_prompt: aiPrompt.value,
147 }
148 if (aiKey.value.trim()) body.api_key = aiKey.value.trim()
149 if (aiClearKey.value) body.clear_api_key = true
150 ai.value = await api.put<AISettings>('api/v1/settings/ai', body)
151 aiKey.value = ''
152 aiClearKey.value = false
153 notify('AI 设置已保存')
154 } catch (e) {
155 notify((e as Error).message, 'error')
156 } finally {
157 aiBusy.value = false
158 }
159}
160
161async function testAI() {
162 aiTesting.value = true
163 aiTestMsg.value = ''
164 try {
165 const res = await api.post<{ ok: boolean; latency_ms: number; model: string; message: string }>(
166 'api/v1/settings/ai/test',
167 {
168 base_url: aiBaseURL.value.trim(),
169 api_key: aiKey.value.trim(),
170 model: aiModel.value.trim(),
171 temperature: Number(aiTemperature.value),
172 },
173 )
174 aiTestMsg.value = `连接成功(${res.latency_ms} ms,${res.model}):${res.message}`
175 notify('AI 接口连接成功')
176 } catch (e) {
177 aiTestMsg.value = `测试失败:${(e as Error).message}`
178 notify((e as Error).message, 'error')
179 } finally {
180 aiTesting.value = false
181 }
182}
183
184async function saveEntry() {
185 const next = entry.value.trim().toLowerCase().replace(/^\/+|\/+$/g, '')
186 if (!/^[a-z0-9]{4,32}$/.test(next)) {
187 notify('安全入口需为 4-32 位小写字母或数字', 'error')
188 return
189 }
190 if (next === settings.value?.entry) {
191 notify('安全入口没有变化', 'error')
192 return
193 }
194 entryBusy.value = true
195 try {
196 await api.put<{ entry: string }>('api/v1/settings/entry', { entry: next })
197 notify('安全入口已更新,即将跳转到新地址')
198 setTimeout(() => {
199 window.location.href = `/${next}/`
200 }, 1200)
201 } catch (e) {
202 notify((e as Error).message, 'error')
203 } finally {
204 entryBusy.value = false
205 }
206}
207
208async function copyText(text: string) {
209 if (!text) return
210 try {
211 await navigator.clipboard.writeText(text)
212 notify('已复制')
213 } catch {
214 notify('浏览器不允许访问剪贴板', 'error')
215 }
216}
217
218async function saveTtl() {
219 if (!Number.isFinite(ttl.value) || ttl.value < 1 || ttl.value > 10080) {
220 notify('登录有效期需在 1 到 10080 分钟之间', 'error')
221 return
222 }
223 ttlBusy.value = true
224 try {
225 settings.value = await api.put<PanelSettings>('api/v1/settings/session', {
226 minutes: Math.round(ttl.value),
227 })
228 notify('登录有效期已保存,对新登录生效')
229 } catch (e) {
230 notify((e as Error).message, 'error')
231 } finally {
232 ttlBusy.value = false
233 }
234}
235
236async function enableAppAccess() {
237 const target = domain.value.trim().toLowerCase().replace(/^https?:\/\//, '').replace(/\/+$/, '')
238 if (!target) {
239 notify('请填写已解析到本机的域名', 'error')
240 return
241 }
242 appBusy.value = true
243 appResult.value = ''
244 startAppTimer()
245 try {
246 const res = await api.post<AppAccessResult>('api/v1/settings/app-access', {
247 domain: target,
248 staging: staging.value,
249 })
250 if (res.staging) {
251 appResult.value = res.message ?? '测试证书已签发,80 端口通道正常'
252 notify('测试通过:80 端口通道正常,请取消测试模式再申请正式证书')
253 domain.value = res.domain
254 return
255 }
256 appResult.value = `已签发受信任证书:${res.issuer},${res.daysLeft} 天后到期`
257 notify('证书已签发,手机 App 现在可以用该域名访问')
258 domain.value = res.domain
259 await load()
260 } catch (e) {
261 notify((e as Error).message, 'error')
262 } finally {
263 appBusy.value = false
264 stopAppTimer()
265 }
266}
267
268async function disableAppAccess() {
269 if (!window.confirm('关闭后将回退到自签证书,手机 App 只能通过 HTTP 访问,确定继续?')) {
270 return
271 }
272 appBusy.value = true
273 try {
274 settings.value = await api.del<PanelSettings>('api/v1/settings/app-access')
275 appResult.value = ''
276 notify('已关闭域名证书,回退到自签证书')
277 } catch (e) {
278 notify((e as Error).message, 'error')
279 } finally {
280 appBusy.value = false
281 }
282}
283
284async function applyUpdate() {
285 const target = updateInfo.value
286 if (!target || applyBusy.value) {
287 return
288 }
289 const ok = window.confirm(
290 `将下载并替换面板二进制到 ${target.version},完成后面板会自动重启(约 5-10 秒)。\n` +
291 '升级前会做 SHA256 与试运行校验,失败不会替换。确定继续?',
292 )
293 if (!ok) {
294 return
295 }
296 applyBusy.value = true
297 applyNote.value = '正在下载并校验,可能需要十几秒…'
298 try {
299 const res = await api.post<UpdateResult>('api/v1/system/update', { confirm: 'UPDATE' })
300 applyNote.value = `${res.message}(${res.version},已备份旧版本到 ${res.backup})`
301 notify('更新完成,面板正在重启')
302 setTimeout(() => window.location.reload(), 8000)
303 } catch (e) {
304 // A restart kills the connection mid-flight, so a network error here does
305 // not mean the upgrade failed. Reload after a pause and let the version
306 // number tell the truth.
307 applyNote.value = `连接已中断(${(e as Error).message}),面板可能正在重启,5 秒后自动刷新…`
308 setTimeout(() => window.location.reload(), 5000)
309 } finally {
310 applyBusy.value = false
311 }
312}
313
314async function checkUpdate() {
315 updateBusy.value = true
316 updateMsg.value = ''
317 try {
318 const info = await api.get<UpdateInfo>('api/v1/system/update')
319 updateInfo.value = info
320 if (info.enabled && info.available) {
321 // Let the shell show its 「新版本」 badge immediately.
322 window.dispatchEvent(new CustomEvent('mp:update-available', { detail: info }))
323 }
324 if (!info.enabled) {
325 updateMsg.value = info.message || '面板已关闭更新检查(config.json 的 update_check)'
326 } else if (info.available) {
327 updateMsg.value = `发现新版本 ${info.version},当前 ${info.current}`
328 } else {
329 updateMsg.value = `当前已是最新版本 ${info.current}`
330 }
331 } catch (e) {
332 updateMsg.value = `检查失败:${(e as Error).message}`
333 } finally {
334 updateBusy.value = false
335 }
336}
337
338onMounted(() => {
339 void load()
340 void loadAI()
341})
342</script>
343
344<template>
345 <div class="card">
346 <h2>安全入口</h2>
347 <p class="hint">
348 面板只响应 <code>/<安全入口>/</code> 下的请求,其他路径伪装成 nginx 404。
349 修改后旧地址立即失效,当前登录也会因为 Cookie 路径变化而失效,需要重新登录。
350 </p>
351 <div class="grid">
352 <label class="field">
353 安全入口
354 <input v-model="entry" class="input mono" />
355 </label>
356 <button class="btn btn-primary" :disabled="entryBusy" @click="saveEntry">
357 <span v-if="entryBusy" class="spin"></span>
358 保存并跳转
359 </button>
360 </div>
361 <p class="hint">4-32 位小写字母或数字,建议使用随机字符串。也可以用命令 <code>miaopanel entry</code> 重新生成。</p>
362 </div>
363
364 <div class="card">
365 <h2>登录有效期</h2>
366 <p class="hint">会话在无操作后自动失效的时间,每次操作会滑动续期。修改只影响之后的登录。</p>
367 <div class="grid">
368 <label class="field">
369 有效期(分钟)
370 <input v-model.number="ttl" class="input" type="number" min="1" max="10080" />
371 </label>
372 <button class="btn btn-primary" :disabled="ttlBusy" @click="saveTtl">
373 <span v-if="ttlBusy" class="spin"></span>
374 保存
375 </button>
376 </div>
377 <p class="hint">1 分钟到 7 天(10080 分钟)。当前值:{{ settings?.session_ttl_minutes ?? '-' }} 分钟</p>
378 </div>
379
380 <div class="card">
381 <h2>HTTPS</h2>
382 <p class="hint">
383 面板在同一个端口同时接受 HTTP 与 HTTPS。首次启动自动生成 10 年自签证书;
384 浏览器首次访问需要手动信任,手机 App 则需要下面「手机 App 访问支持」里的受信任证书。
385 </p>
386 <dl class="kv">
387 <dt>当前状态</dt>
388 <dd>
389 <span class="badge" :class="settings?.https ? 'ok' : 'err'">
390 {{ settings?.https ? '已开启' : '已关闭' }}
391 </span>
392 </dd>
393 <dt>监听端口</dt>
394 <dd class="mono">{{ settings?.port ?? '-' }}(HTTP 与 HTTPS 共用)</dd>
395 <dt>证书来源</dt>
396 <dd>{{ settings?.cert_self_signed ? '自签证书' : settings?.cert_issuer || '-' }}</dd>
397 <dt>到期时间</dt>
398 <dd class="mono">{{ settings?.cert_not_after ? time(settings.cert_not_after) : '-' }}</dd>
399 </dl>
400 <p class="hint">
401 关闭或重新开启 HTTPS 需要编辑 config.json 的 <code>https</code> 字段并重启面板:
402 <span class="mono">systemctl restart miaopanel</span>。为避免把自己锁在门外,面板不提供一键关闭。
403 </p>
404 </div>
405
406 <div class="card">
407 <h2>手机 App 访问支持</h2>
408 <p class="hint">
409 手机 App 不信任自签证书,只有受信任的证书才能走 HTTPS。填写一条已经解析到本机的域名,
410 面板会通过 Let's Encrypt 为它申请证书;验证需要 80 端口能从公网访问。
411 </p>
412
413 <div v-if="!settings?.https" class="alert danger" style="margin-bottom: 14px">
414 面板当前关闭了 HTTPS,请先在 config.json 中把 <code>https</code> 设为 true 并重启。
415 </div>
416
417 <div
418 v-if="settings?.app_domain"
419 class="alert"
420 :class="certDaysLeft !== null && certDaysLeft < 21 ? 'danger' : 'ok'"
421 style="margin-bottom: 14px"
422 >
423 已启用 {{ settings.app_domain }}
424 <span v-if="settings.cert_not_after"> · 有效期至 {{ time(settings.cert_not_after) }}</span>
425 <span v-if="certDaysLeft !== null"> (剩余 {{ certDaysLeft }} 天)</span>
426 <br />
427 <span class="mono" style="font-size: 12px">
428 手机端填写:{{ appUrl }}
429 </span>
430 <button class="btn btn-ghost btn-sm" style="margin-left: 8px" @click="copyText(appUrl)">
431 复制
432 </button>
433 <div v-if="certDaysLeft !== null && certDaysLeft < 21" style="margin-top: 8px">
434 证书将在 {{ certDaysLeft }} 天后过期,请在到期前重新点击「申请证书」。
435 </div>
436 </div>
437
438 <div class="grid">
439 <label class="field">
440 域名
441 <input v-model="domain" class="input mono" placeholder="panel.example.com" />
442 </label>
443 <button class="btn btn-primary" :disabled="appBusy || !settings?.https" @click="enableAppAccess">
444 <span v-if="appBusy" class="spin"></span>
445 {{ appBusy ? `正在申请… ${appElapsed} 秒` : '申请证书' }}
446 </button>
447 </div>
448
449 <label class="row" style="gap: 8px; margin-top: 10px; font-size: 13px; color: #5b7c85">
450 <input v-model="staging" type="checkbox" :disabled="appBusy" style="width: 16px; height: 16px" />
451 测试模式(Let's Encrypt staging:签出的证书浏览器不信任,但能验证 80 端口通道,且不消耗正式额度)
452 </label>
453
454 <div v-if="appBusy && appElapsed >= 25" class="alert" style="margin-top: 12px">
455 Let's Encrypt 正在回调验证,正常 10-30 秒完成。超过 1 分钟仍在等待,通常是
456 <b>80 端口无法从公网访问</b>:检查云厂商安全组、ufw/firewalld、以及域名是否解析到本机公网 IP。
457 可在手机流量下打开 <span class="mono">http://{{ domain }}/.well-known/acme-challenge/test</span> 自测(应返回 404 而不是超时)。
458 </div>
459
460 <div v-if="appResult" class="alert ok" style="margin-top: 12px">{{ appResult }}</div>
461
462 <div v-if="settings?.app_domain" style="margin-top: 12px">
463 <button class="btn btn-ghost" :disabled="appBusy" @click="disableAppAccess">关闭域名证书</button>
464 </div>
465
466 <p class="hint">
467 前提:域名 A/AAAA 记录指向本机,80 端口对公网开放。若域名已被某个站点使用,
468 临时验证配置可能与站点冲突,建议用一个专用子域名。证书信息见上方「HTTPS」卡片。
469 </p>
470 </div>
471
472 <div class="card">
473 <h2>AI 助手</h2>
474 <p class="hint">
475 配置 OpenAI 兼容接口(Chat Completions)。API Key 加密保存在本机,保存后只显示掩码;
476 API Key 留空表示保持不变。
477 </p>
478
479 <label class="row" style="gap: 8px; margin-bottom: 12px; font-size: 13px; color: #5b7c85">
480 <input v-model="aiEnabled" type="checkbox" style="width: 16px; height: 16px" />
481 启用 AI 助手(对话页与智能终端)
482 </label>
483
484 <div class="grid">
485 <label class="field">
486 Base URL
487 <input v-model="aiBaseURL" class="input mono" placeholder="https://api.openai.com/v1" />
488 </label>
489 <label class="field">
490 模型
491 <input v-model="aiModel" class="input mono" placeholder="gpt-4o-mini" />
492 </label>
493 </div>
494
495 <label class="field">
496 API Key
497 <input
498 v-model="aiKey"
499 class="input mono"
500 type="password"
501 autocomplete="new-password"
502 :disabled="aiClearKey"
503 :placeholder="ai?.api_key_set ? `已配置 ${ai.api_key_masked},留空保持不变` : 'sk-...'"
504 />
505 </label>
506
507 <label class="row" style="gap: 8px; margin-bottom: 12px; font-size: 13px; color: #5b7c85">
508 <input v-model="aiClearKey" type="checkbox" style="width: 16px; height: 16px" />
509 清除已保存的 API Key
510 </label>
511
512 <div class="grid">
513 <label class="field">
514 Temperature
515 <input v-model.number="aiTemperature" class="input" type="number" min="0" max="2" step="0.1" />
516 </label>
517 </div>
518
519 <label class="field">
520 系统提示词
521 <textarea
522 v-model="aiPrompt"
523 class="textarea"
524 rows="6"
525 placeholder="留空使用面板默认运维提示词"
526 ></textarea>
527 </label>
528
529 <div class="row" style="gap: 10px; flex-wrap: wrap">
530 <button class="btn btn-primary" :disabled="aiBusy" @click="saveAI">
531 <span v-if="aiBusy" class="spin"></span>
532 保存 AI 设置
533 </button>
534 <button class="btn btn-ghost" :disabled="aiTesting" @click="testAI">
535 <span v-if="aiTesting" class="spin"></span>
536 {{ aiTesting ? '测试中…' : '测试连接' }}
537 </button>
538 <span v-if="aiTestMsg" class="hint">{{ aiTestMsg }}</span>
539 </div>
540 </div>
541
542 <div class="card">
543 <h2>检查更新</h2>
544 <p class="hint">立即向发布服务器查询是否有新版本,不会自动下载。</p>
545 <div class="row" style="gap: 10px">
546 <button class="btn btn-primary" :disabled="updateBusy" @click="checkUpdate">
547 <span v-if="updateBusy" class="spin"></span>
548 立即检查
549 </button>
550 <span class="muted">当前版本 v{{ settings?.version ?? '-' }}</span>
551 </div>
552
553 <div v-if="updateMsg" class="alert" :class="updateInfo?.available ? 'ok' : ''" style="margin-top: 12px">
554 {{ updateMsg }}
555 </div>
556
557 <div v-if="updateInfo?.available" style="margin-top: 14px">
558 <dl class="kv">
559 <dt>新版本</dt>
560 <dd>v{{ updateInfo.version }}</dd>
561 <dt>发布时间</dt>
562 <dd>{{ updateInfo.published_at ? time(updateInfo.published_at) : '-' }}</dd>
563 <dt>大小</dt>
564 <dd>{{ (updateInfo.size / 1024 / 1024).toFixed(1) }} MB</dd>
565 </dl>
566 <pre v-if="updateInfo.notes" class="code" style="margin-top: 10px">{{ updateInfo.notes }}</pre>
567
568 <div class="row" style="gap: 10px; margin-top: 12px">
569 <button class="btn btn-primary" :disabled="applyBusy" @click="applyUpdate">
570 <span v-if="applyBusy" class="spin"></span>
571 {{ applyBusy ? '正在升级…' : '立即更新到 v' + updateInfo.version }}
572 </button>
573 </div>
574 <p class="hint">
575 升级会先做 SHA256、ELF 与试运行校验,全部通过才替换二进制并备份旧版本;
576 替换后由 systemd 延迟 2 秒重启,页面会自动刷新。
577 </p>
578 <div v-if="applyNote" class="alert" style="margin-top: 12px">{{ applyNote }}</div>
579 </div>
580 </div>
581</template>