/** * 审计脚本的**环境横幅**(2026-10-13)。 * * 为什么需要:主系统在 §4.5.22 里更正了一条事实 —— 我方把 `MAIN_SYSTEM_BASE_URL` * 当成"线上/本地"的判据,但实际上 * · `https://deepseek.xdjchuanhui.com:8443` 是本机 nginx → 反代到**本机 dev 栈**(API 3001); * · 本地主系统库是**生产 dump 的副本**(同一张单号、同一批供应商), * 于是"从线上拉回了货"这个判断,实际上是"本机 dev 栈 + 生产 dump 库"。 * ⇒ 光看 URL 分辨不出来,必须把「我方库 / 主系统 BASE_URL / 主系统库 / 对接客户端数」一起打出来。 * * 用法:`await printAuditEnv(mainClient)` —— 两个审计脚本共用。 */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const HERE = path.dirname(fileURLToPath(import.meta.url)); const SERVER_DIR = path.resolve(HERE, '..', '..'); /** `postgresql://user:pass@host:port/db` → `host:port/db`(不带凭据) */ export function describeDbUrl(url) { try { const u = new URL(url); return `${u.hostname}:${u.port || '5432'}${u.pathname}`; } catch { return '(无法解析)'; } } function readEnvValue(key) { try { const text = fs.readFileSync(path.join(SERVER_DIR, '.env'), 'utf8'); const m = text.match(new RegExp(`^${key}=(.*)$`, 'm')); return m ? m[1].trim().replace(/^"|"$/g, '') : ''; } catch { return ''; } } /** * @param mainClient 已连接的主系统库客户端(可选;不传则跳过客户端数探测) */ export async function printAuditEnv(mainClient) { const ourUrl = process.env.DATABASE_URL || readEnvValue('DATABASE_URL'); const base = process.env.MAIN_SYSTEM_BASE_URL || readEnvValue('MAIN_SYSTEM_BASE_URL'); const mainUrl = process.env.MAIN_SYSTEM_DATABASE_URL || (() => { try { const p = '/home/ubuntu/harness/ai-fresh-delivery/server/.env'; const m = fs.readFileSync(p, 'utf8').match(/^DATABASE_URL=(.*)$/m); return m ? m[1].trim().replace(/^"|"$/g, '') : ''; } catch { return ''; } })(); let host = ''; try { host = new URL(base).hostname; } catch { host = ''; } const loopback = host === 'localhost' || host === '127.0.0.1' || host === '::1'; let clients = null; if (mainClient) { try { const r = await mainClient.query('SELECT COUNT(*)::int AS n FROM public.supplier_integration_clients'); clients = Number(r.rows[0]?.n ?? 0); } catch { clients = null; } } console.log('== 环境信息(先看这里,别把本地栈当成线上)=='); console.log(` 我方库 : ${describeDbUrl(ourUrl)}`); console.log(` 主系统 BASE_URL : ${base || '(未配置)'}${loopback ? ' ⇒ 本机地址' : host ? ' ⇒ 非本机' : ''}`); console.log(` 主系统库 : ${mainUrl ? describeDbUrl(mainUrl) : '(未配置)'}`); if (clients !== null) { console.log( ` 主系统对接客户端数: ${clients}${clients === 0 ? ' ⇒ 该库签不出对接 Token(生产当前就是 0;本地/生产 dump 需人工确认)' : ''}` ); } console.log( ' ⚠️ 注意:8443/8444 等域名入口若指向本机 nginx,其上游仍是**本机 dev 栈**;' + '而本地主系统库常是生产 dump 的副本 ⇒ 只看 URL 分不出"线上/本地"。' ); console.log(''); }