diff --git a/.env.example b/.env.example index 39781fd..e14312f 100644 --- a/.env.example +++ b/.env.example @@ -1,7 +1,7 @@ # MongoDB 连接(留空则回落到本地默认,配置/历史不持久化) # 本地: mongodb://127.0.0.1:27017 -# 云端 Atlas 示例(把 换成真实密码,数据库会建在 MONGODB_DB 指定的库,不会落到 test): -# MONGODB_URI=mongodb://devops:@ac-eqksaup-shard-00-00.utlbkzb.mongodb.net:27017,ac-eqksaup-shard-00-01.utlbkzb.mongodb.net:27017,ac-eqksaup-shard-00-02.utlbkzb.mongodb.net:27017/?ssl=true&replicaSet=atlas-ofsrbz-shard-0&authSource=admin&appName=devops +# 云端 Atlas 示例(把 换成真实密码、 换成你的集群地址,数据库会建在 MONGODB_DB 指定的库,不会落到 test): +# MONGODB_URI=mongodb://:@-shard-00-00..mongodb.net:27017,-shard-00-01..mongodb.net:27017,-shard-00-02..mongodb.net:27017/?ssl=true&replicaSet=atlas--shard-0&authSource=admin&appName=app MONGODB_URI=mongodb://127.0.0.1:27017 MONGODB_DB=torrent_console diff --git a/app/layouts/default.vue b/app/layouts/default.vue index ee839b5..c1ca116 100644 --- a/app/layouts/default.vue +++ b/app/layouts/default.vue @@ -16,7 +16,7 @@ onMounted(() => { const mongoPill = computed(() => { if (!health.value) return { cls: '', text: 'MONGO ?' } return health.value.mongo.ok - ? { cls: 'on', text: `MONGO · ${health.value.mongo.db}` } + ? { cls: 'on', text: 'MONGO' } : { cls: 'off', text: 'MONGO 未连接' } }) diff --git a/app/pages/index.vue b/app/pages/index.vue index ff09e4e..d956ea9 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -153,8 +153,7 @@ function previewLink(item: TorrentView) {
- MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI(云端实例请填真实密码)后刷新即可: - {{ health.mongo.uri }} + MongoDB 未连接,搜索记录与种子缓存不会被保存(其余功能可用)。在 .env 配置正确的 MONGODB_URI 后刷新即可。
diff --git a/app/pages/settings.vue b/app/pages/settings.vue index c05416c..7ad0710 100644 --- a/app/pages/settings.vue +++ b/app/pages/settings.vue @@ -321,7 +321,6 @@ onMounted(load)
MongoDB
{{ health?.mongo.ok ? '已连接' : '未连接' }} - · {{ health?.mongo.uri }} / {{ health?.mongo.db }}
{{ health.mongo.error }}
磁力解析
diff --git a/app/types/api.ts b/app/types/api.ts index a161d5b..957224f 100644 --- a/app/types/api.ts +++ b/app/types/api.ts @@ -95,7 +95,7 @@ export interface ClientsResponse { export interface HealthResponse { ok: boolean time: string - mongo: { ok: boolean; uri: string; db: string; error: string | null } + mongo: { ok: boolean; error: string | null } webTorrent: boolean providers: { id: string; label: string; homepage: string }[] enabledProviders: string[] diff --git a/app/utils/i18n-dict.ts b/app/utils/i18n-dict.ts index 266f9fb..2347351 100644 --- a/app/utils/i18n-dict.ts +++ b/app/utils/i18n-dict.ts @@ -225,7 +225,7 @@ export const zh2en: Record = { '点击关键词直接搜索': 'Click a keyword to search', '已写入 MongoDB': 'Saved to MongoDB', '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 (use the real password for cloud instances) 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.' } export const en2zh: Record = Object.fromEntries( diff --git a/server/utils/mongo.ts b/server/utils/mongo.ts index 9b24993..f90c27a 100644 --- a/server/utils/mongo.ts +++ b/server/utils/mongo.ts @@ -68,18 +68,23 @@ export async function downloadsCol(): Promise> { return (await getDb()).collection('downloads') } -/** 供 /api/health 使用:探测 MongoDB 是否可用 */ +/** 把驱动错误里的连接串 / 主机名脱敏,避免泄露凭证与数据库地址 */ +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() { try { const db = await getDb() await db.command({ ping: 1 }) - return { ok: true, uri: mongoUri(), db: mongoDbName(), error: null as string | null } + return { ok: true, error: null as string | null } } catch (err) { return { ok: false, - uri: mongoUri(), - db: mongoDbName(), - error: lastError?.message || (err instanceof Error ? err.message : String(err)) + error: sanitizeMongoError(lastError?.message || (err instanceof Error ? err.message : String(err))) } } }