#!/usr/bin/env node /** * QLinker 外部能力就绪度自检:HTTP 节点出站 + Webhook 入站。 * * 只做本机只读检查: * - GET /actuator/health(本机回环) * - TCP 连接 127.0.0.1:8080 与本机各网卡地址:8080(验证监听范围) * - OPTIONS /api/v1/automation/hooks/{ruleUid}(验证路由存在,不创建任务、不写审计) * - 读取仓库 application.yml 与启动脚本中的“变量名”判断是否覆盖了默认配置 * * 不发起任意外部网络请求,不创建/修改任何数据,不读取或输出凭据值。 * 用法:node tools/probe/readiness-http-webhook.mjs */ import fs from 'node:fs' import net from 'node:net' import os from 'node:os' import path from 'node:path' import { fileURLToPath } from 'node:url' const SCRIPT_DIR = path.dirname(fileURLToPath(import.meta.url)) const REPO_ROOT = path.resolve(SCRIPT_DIR, '..', '..') const BASE_URL = process.env.QLINKER_BASE_URL || 'http://127.0.0.1:8080' const PORT = Number(new URL(BASE_URL).port || 80) const results = [] function add(level, title, detail = '') { results.push({ level, title, detail }) } function readIfExists(file) { try { return fs.readFileSync(file, 'utf8') } catch { return null } } function findStartScript() { if (process.env.QLINKER_START_SCRIPT) { return process.env.QLINKER_START_SCRIPT } const dir = path.join(os.homedir(), '.qlinker') try { const files = fs.readdirSync(dir) .filter((name) => /^start-backend.*\.ps1$/i.test(name)) .map((name) => path.join(dir, name)) .sort() return files.at(-1) || null } catch { return null } } async function checkHealth() { try { const response = await fetch(`${BASE_URL}/actuator/health`, { signal: AbortSignal.timeout(5000) }) const text = await response.text() if (response.ok && text.includes('"UP"')) { add('OK', '健康端点', `${BASE_URL}/actuator/health -> ${response.status} ${text.trim()}`) return true } add('FAIL', '健康端点', `${response.status} ${text.slice(0, 200)}`) return false } catch (error) { add('FAIL', '健康端点', `不可访问:${error.message}`) return false } } function tcpProbe(host, port, timeoutMs = 3000) { return new Promise((resolve) => { const socket = net.connect({ host, port }) const done = (ok, message) => { socket.destroy() resolve({ ok, message }) } socket.setTimeout(timeoutMs) socket.once('connect', () => done(true, 'connected')) socket.once('timeout', () => done(false, 'timeout')) socket.once('error', (error) => done(false, error.code || error.message)) }) } async function checkListeners() { const loopback = await tcpProbe('127.0.0.1', PORT) add(loopback.ok ? 'OK' : 'FAIL', '回环监听', `127.0.0.1:${PORT} ${loopback.message}`) const interfaces = os.networkInterfaces() const addresses = [] for (const [name, infos] of Object.entries(interfaces)) { for (const info of infos || []) { if (info.family === 'IPv4' && !info.internal) { addresses.push({ name, address: info.address }) } } } if (addresses.length === 0) { add('WARN', '网卡地址', '未发现非回环 IPv4 地址') } for (const item of addresses) { const probe = await tcpProbe(item.address, PORT) add(probe.ok ? 'OK' : 'WARN', `网卡监听 ${item.name}`, `${item.address}:${PORT} ${probe.ok ? '可连接(说明监听在所有接口)' : `不可连接:${probe.message}`}`) } } async function checkWebhookRoute() { try { const response = await fetch(`${BASE_URL}/api/v1/automation/hooks/__readiness_probe__`, { method: 'OPTIONS', signal: AbortSignal.timeout(5000) }) const allow = response.headers.get('allow') || '' const routePresent = response.status === 200 && allow.toUpperCase().includes('POST') add(routePresent ? 'OK' : 'WARN', 'Webhook 路由', `OPTIONS -> ${response.status}${allow ? `,Allow=${allow}` : ''}`) return routePresent } catch (error) { add('FAIL', 'Webhook 路由', `不可访问:${error.message}`) return false } } function inspectConfigSources() { const applicationYml = readIfExists(path.join(REPO_ROOT, 'src', 'main', 'resources', 'application.yml')) || '' const hasAutomationHttp = /http-allowed-hosts|http-timeout-ms|http-max-response-bytes|http-credentials/.test(applicationYml) add(hasAutomationHttp ? 'INFO' : 'OK', 'application.yml 覆盖', hasAutomationHttp ? '发现自动化 HTTP 配置项,请人工核对生效值' : '未配置自动化 HTTP 相关键,生效值为 QLinkerProperties 默认值') const aiLine = applicationYml.split(/\r?\n/).find((line) => line.includes('allow-private-endpoints')) add(aiLine ? 'INFO' : 'WARN', 'AI 私网端点开关', aiLine ? aiLine.trim() : '未在 application.yml 找到') const startScript = findStartScript() if (!startScript) { add('WARN', '启动脚本', '未找到 ~/.qlinker/start-backend*.ps1,无法核对进程环境变量') return } const content = readIfExists(startScript) || '' const envNames = [...content.matchAll(/\$env:([A-Z0-9_]+)\s*=/g)].map((match) => match[1]) const unique = [...new Set(envNames)].sort() const automationHttp = unique.filter((name) => name.includes('AUTOMATION_HTTP')) add('INFO', '启动脚本环境变量名', unique.join(', ') || '(未发现 $env: 赋值)') add(automationHttp.length === 0 ? 'OK' : 'WARN', '自动化 HTTP 环境覆盖', automationHttp.length === 0 ? '启动脚本未设置 QLINKER_AUTOMATION_HTTP_*,默认白名单为空' : automationHttp.join(', ')) const springJson = process.env.SPRING_APPLICATION_JSON add(springJson ? 'WARN' : 'INFO', 'SPRING_APPLICATION_JSON', springJson ? '当前 shell 设置了 SPRING_APPLICATION_JSON;请确认其中是否覆盖 qlinker.automation.*' : '当前 shell 未设置') } function printStaticFacts() { add('INFO', '出站白名单默认值', '空列表 => 全部拒绝(HTTP_EGRESS_DENIED)') add('INFO', '内网拒绝', '硬编码纵深防御:回环/私网/链路本地/组播/保留地址一律拒绝,无配置开关') add('INFO', '重定向', '硬编码不跟随;3xx 返回 HTTP_REDIRECT_DENIED') add('INFO', '超时默认值', '节点 timeoutMs 默认 5000ms,取值被限制在 100..30000ms') add('INFO', '响应体上限', '默认 262144 字节(256KB),超出返回 HTTP_RESPONSE_TOO_LARGE') add('INFO', 'Webhook 入站', '路径 /api/v1/automation/hooks/{ruleUid},token 头 X-Webhook-Token,请求体上限 16KB') } function printSummary() { const icon = { OK: '[OK]', WARN: '[WARN]', FAIL: '[FAIL]', INFO: '[INFO]' } for (const item of results) { console.log(`${icon[item.level] || '[INFO]'} ${item.title}${item.detail ? `: ${item.detail}` : ''}`) } const failed = results.filter((item) => item.level === 'FAIL').length console.log('\n结论:本脚本只证明“本机进程/路由可达性”,不证明外部网络可达。') console.log('未验证项:出站白名单放行后的真实 HTTP 调用、外部服务回调 Webhook、端口映射/防火墙放行。') process.exitCode = failed === 0 ? 0 : 1 } async function main() { console.log(`QLinker 外部能力就绪度自检(只读) base=${BASE_URL}\n`) printStaticFacts() inspectConfigSources() const healthy = await checkHealth() if (healthy) { await checkListeners() await checkWebhookRoute() } printSummary() } main()