feat: MongoDB 仅保留连接状态 + 站点级密码门禁(WEBPASSWORD)
This commit is contained in:
@@ -5,6 +5,9 @@
|
|||||||
MONGODB_URI=mongodb://127.0.0.1:27017
|
MONGODB_URI=mongodb://127.0.0.1:27017
|
||||||
MONGODB_DB=torrent_console
|
MONGODB_DB=torrent_console
|
||||||
|
|
||||||
|
# 站点访问密码(留空则站点完全开放;设置后进入站点需在 /gate 输入此密码,cookie 由服务端校验)
|
||||||
|
WEBPASSWORD=
|
||||||
|
|
||||||
# 访问搜索源时使用的 HTTP 代理(国内网络连不上公开磁力站时配这里)
|
# 访问搜索源时使用的 HTTP 代理(国内网络连不上公开磁力站时配这里)
|
||||||
# 例如:http://127.0.0.1:7890
|
# 例如:http://127.0.0.1:7890
|
||||||
SEARCH_PROXY=
|
SEARCH_PROXY=
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ const mongoPill = computed(() => {
|
|||||||
<i class="dot" />
|
<i class="dot" />
|
||||||
{{ health?.webTorrent ? '磁力解析就绪' : '磁力解析不可用' }}
|
{{ health?.webTorrent ? '磁力解析就绪' : '磁力解析不可用' }}
|
||||||
</span>
|
</span>
|
||||||
<NuxtLink to="/settings" class="pill" :class="mongoPill.cls" :title="health?.mongo.error || ''">
|
<NuxtLink to="/settings" class="pill" :class="mongoPill.cls">
|
||||||
<i class="dot" />
|
<i class="dot" />
|
||||||
{{ mongoPill.text }}
|
{{ mongoPill.text }}
|
||||||
</NuxtLink>
|
</NuxtLink>
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
const password = ref('')
|
||||||
|
const error = ref('')
|
||||||
|
const loading = ref(false)
|
||||||
|
|
||||||
|
async function unlock() {
|
||||||
|
if (!password.value) {
|
||||||
|
error.value = '请输入密码'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
loading.value = true
|
||||||
|
error.value = ''
|
||||||
|
try {
|
||||||
|
const res = await $fetch<{ ok?: boolean; enabled?: boolean }>('/api/unlock', {
|
||||||
|
method: 'POST',
|
||||||
|
body: { password: password.value }
|
||||||
|
})
|
||||||
|
if (res && (res.ok || res.enabled === false)) {
|
||||||
|
await navigateTo('/', { reload: true })
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
const msg = (e as { data?: { statusMessage?: string } })?.data?.statusMessage
|
||||||
|
error.value = msg === 'invalid password' ? '密码错误' : '验证失败'
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="gate">
|
||||||
|
<form class="gate-card" @submit.prevent="unlock">
|
||||||
|
<div class="brand-mark big">BT</div>
|
||||||
|
<h1>需要密码</h1>
|
||||||
|
<p class="mute">请输入访问密码以进入</p>
|
||||||
|
<input
|
||||||
|
v-model="password"
|
||||||
|
type="password"
|
||||||
|
class="input"
|
||||||
|
placeholder="访问密码"
|
||||||
|
autofocus
|
||||||
|
@keyup.enter="unlock"
|
||||||
|
/>
|
||||||
|
<button class="btn btn-primary" type="submit" :disabled="loading">
|
||||||
|
<span v-if="loading" class="spin" />
|
||||||
|
{{ loading ? '验证中' : '进入' }}
|
||||||
|
</button>
|
||||||
|
<p v-if="error" class="err small">{{ error }}</p>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.gate {
|
||||||
|
min-height: 70vh;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.gate-card {
|
||||||
|
width: 320px;
|
||||||
|
max-width: 90vw;
|
||||||
|
border: 1px solid var(--line);
|
||||||
|
border-radius: 16px;
|
||||||
|
background: var(--panel);
|
||||||
|
padding: 28px 24px;
|
||||||
|
text-align: center;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
.brand-mark.big {
|
||||||
|
width: 56px;
|
||||||
|
height: 56px;
|
||||||
|
font-size: 22px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
.gate-card input {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -321,7 +321,6 @@ onMounted(load)
|
|||||||
<dt>MongoDB</dt>
|
<dt>MongoDB</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span :class="health?.mongo.ok ? 'ok' : 'err'">{{ health?.mongo.ok ? '已连接' : '未连接' }}</span>
|
<span :class="health?.mongo.ok ? 'ok' : 'err'">{{ health?.mongo.ok ? '已连接' : '未连接' }}</span>
|
||||||
<div v-if="health?.mongo.error" class="small err mono break">{{ health.mongo.error }}</div>
|
|
||||||
</dd>
|
</dd>
|
||||||
<dt>磁力解析</dt>
|
<dt>磁力解析</dt>
|
||||||
<dd :class="health?.webTorrent ? 'ok' : 'err'">
|
<dd :class="health?.webTorrent ? 'ok' : 'err'">
|
||||||
|
|||||||
+1
-1
@@ -95,7 +95,7 @@ export interface ClientsResponse {
|
|||||||
export interface HealthResponse {
|
export interface HealthResponse {
|
||||||
ok: boolean
|
ok: boolean
|
||||||
time: string
|
time: string
|
||||||
mongo: { ok: boolean; error: string | null }
|
mongo: { ok: boolean }
|
||||||
webTorrent: boolean
|
webTorrent: boolean
|
||||||
providers: { id: string; label: string; homepage: string }[]
|
providers: { id: string; label: string; homepage: string }[]
|
||||||
enabledProviders: string[]
|
enabledProviders: string[]
|
||||||
|
|||||||
+11
-1
@@ -225,7 +225,17 @@ export const zh2en: Record<string, string> = {
|
|||||||
'点击关键词直接搜索': 'Click a keyword to search',
|
'点击关键词直接搜索': 'Click a keyword to search',
|
||||||
'已写入 MongoDB': 'Saved to MongoDB',
|
'已写入 MongoDB': 'Saved to MongoDB',
|
||||||
'MongoDB 未连接,未记录': 'MongoDB not connected, not recorded',
|
'MongoDB 未连接,未记录': 'MongoDB not connected, not recorded',
|
||||||
'MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI 后刷新即可。': 'MongoDB not connected; search history and torrent cache will not be saved (other features still work). Set a valid MONGODB_URI in .env and refresh.'
|
'MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI 后刷新即可。': 'MongoDB not connected; search history and torrent cache will not be saved (other features still work). Set a valid MONGODB_URI in .env and refresh.',
|
||||||
|
|
||||||
|
// ===== 访问密码门禁 =====
|
||||||
|
'需要密码': 'Password required',
|
||||||
|
'请输入访问密码以进入': 'Enter the access password to continue',
|
||||||
|
'访问密码': 'Access password',
|
||||||
|
'进入': 'Enter',
|
||||||
|
'验证中': 'Verifying…',
|
||||||
|
'密码错误': 'Wrong password',
|
||||||
|
'验证失败': 'Verification failed',
|
||||||
|
'请输入密码': 'Please enter the password'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const en2zh: Record<string, string> = Object.fromEntries(
|
export const en2zh: Record<string, string> = Object.fromEntries(
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// 校验站点访问密码(来自环境变量 WEBPASSWORD),成功后写入 HTTP-only cookie
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const password = process.env.WEBPASSWORD
|
||||||
|
if (!password) return { enabled: false }
|
||||||
|
|
||||||
|
const body = await readBody<{ password?: string }>(event)
|
||||||
|
if (body.password === password) {
|
||||||
|
setCookie(event, 'web_unlock', '1', {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: 'lax',
|
||||||
|
path: '/',
|
||||||
|
maxAge: 60 * 60 * 24 * 30
|
||||||
|
})
|
||||||
|
return { ok: true }
|
||||||
|
}
|
||||||
|
throw createError({ statusCode: 401, statusMessage: 'invalid password' })
|
||||||
|
})
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// 站点级访问门禁:仅当配置了 WEBPASSWORD 时启用。
|
||||||
|
// 验证通过后由 /api/unlock 写入 HTTP-only cookie,后续请求凭此放行。
|
||||||
|
export default defineEventHandler((event) => {
|
||||||
|
const password = process.env.WEBPASSWORD
|
||||||
|
// 未配置密码则站点完全开放
|
||||||
|
if (!password) return
|
||||||
|
|
||||||
|
const url = (event.node.req.url || '').split('?')[0]
|
||||||
|
|
||||||
|
// 解锁接口、门禁页与 Nuxt 运行所需资源始终放行
|
||||||
|
if (
|
||||||
|
url === '/api/unlock' ||
|
||||||
|
url === '/gate' ||
|
||||||
|
url.startsWith('/_nuxt') ||
|
||||||
|
url.startsWith('/__nuxt') ||
|
||||||
|
url.startsWith('/@vite') ||
|
||||||
|
url.startsWith('/@fs') ||
|
||||||
|
url === '/favicon.ico' ||
|
||||||
|
url.startsWith('/favicon')
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getCookie(event, 'web_unlock') === '1') return
|
||||||
|
|
||||||
|
if (url.startsWith('/api/')) {
|
||||||
|
throw createError({ statusCode: 401, statusMessage: 'unauthorized' })
|
||||||
|
}
|
||||||
|
return sendRedirect(event, '/gate')
|
||||||
|
})
|
||||||
+4
-19
@@ -8,9 +8,6 @@ let indexState: 'pending' | 'done' = 'pending'
|
|||||||
export const mongoUri = () => process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'
|
export const mongoUri = () => process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'
|
||||||
export const mongoDbName = () => process.env.MONGODB_DB || 'torrent_console'
|
export const mongoDbName = () => process.env.MONGODB_DB || 'torrent_console'
|
||||||
|
|
||||||
/** 最近一次连接失败信息,用于在 UI 上提示用户去装 / 启 MongoDB */
|
|
||||||
let lastError: { message: string; at: Date } | null = null
|
|
||||||
|
|
||||||
export async function getClient(): Promise<MongoClient> {
|
export async function getClient(): Promise<MongoClient> {
|
||||||
if (client) return client
|
if (client) return client
|
||||||
if (!connecting) {
|
if (!connecting) {
|
||||||
@@ -25,12 +22,10 @@ export async function getClient(): Promise<MongoClient> {
|
|||||||
.then((cli) => {
|
.then((cli) => {
|
||||||
client = cli
|
client = cli
|
||||||
connecting = null
|
connecting = null
|
||||||
lastError = null
|
|
||||||
return cli
|
return cli
|
||||||
})
|
})
|
||||||
.catch((err: unknown) => {
|
.catch((err: unknown) => {
|
||||||
connecting = null
|
connecting = null
|
||||||
lastError = { message: err instanceof Error ? err.message : String(err), at: new Date() }
|
|
||||||
next.close().catch(() => {})
|
next.close().catch(() => {})
|
||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
@@ -68,24 +63,14 @@ export async function downloadsCol(): Promise<Collection<DownloadDoc>> {
|
|||||||
return (await getDb()).collection<DownloadDoc>('downloads')
|
return (await getDb()).collection<DownloadDoc>('downloads')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 把驱动错误里的连接串 / 主机名脱敏,避免泄露凭证与数据库地址 */
|
/** 供 /api/health 使用:探测 MongoDB 是否可用(只返回 ok,不暴露任何连接信息) */
|
||||||
function sanitizeMongoError(raw: string): string {
|
|
||||||
return raw
|
|
||||||
.replace(/mongodb(\+srv)?:\/\/[^\s'"]+/gi, '***')
|
|
||||||
.replace(/[a-z0-9.-]+\.mongodb\.net(?::\d+)?/gi, '***')
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 供 /api/health 使用:探测 MongoDB 是否可用(不返回任何连接串 / 库名) */
|
|
||||||
export async function mongoStatus() {
|
export async function mongoStatus() {
|
||||||
try {
|
try {
|
||||||
const db = await getDb()
|
const db = await getDb()
|
||||||
await db.command({ ping: 1 })
|
await db.command({ ping: 1 })
|
||||||
return { ok: true, error: null as string | null }
|
return { ok: true }
|
||||||
} catch (err) {
|
} catch {
|
||||||
return {
|
return { ok: false }
|
||||||
ok: false,
|
|
||||||
error: sanitizeMongoError(lastError?.message || (err instanceof Error ? err.message : String(err)))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user