54de1dd8e3
DNS/TLS/proxy scripts and docs/local-dev.md replace localhost URLs for H5, memind_adm, Plaza, and Ops during local dev. Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
||
import fs from 'node:fs';
|
||
import http from 'node:http';
|
||
import https from 'node:https';
|
||
import {
|
||
ADMIN_HOST,
|
||
allHostsConfigured,
|
||
DNS_PORT,
|
||
H5_HOST,
|
||
H5_PUBLIC_BASE,
|
||
OPS_HOST,
|
||
PLAZA_HOST,
|
||
PLAZA_PUBLIC_BASE,
|
||
PUBLIC_PORT,
|
||
PUBLIC_SCHEME,
|
||
publicUrl,
|
||
resolverConfigured,
|
||
hostConfigured,
|
||
} from './local-test-config.mjs';
|
||
|
||
function checkUrl(url) {
|
||
const parsed = new URL(url);
|
||
const transport = parsed.protocol === 'https:' ? https : http;
|
||
return new Promise((resolve) => {
|
||
const req = transport.request(
|
||
{
|
||
hostname: parsed.hostname,
|
||
port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
|
||
path: `${parsed.pathname}${parsed.search}`,
|
||
method: 'GET',
|
||
rejectUnauthorized: false,
|
||
headers: parsed.hostname === PLAZA_HOST ? { Host: `${PLAZA_HOST}:${process.env.PLAZA_PORT ?? 3001}` } : {},
|
||
},
|
||
(res) => resolve(res.statusCode ?? 0),
|
||
);
|
||
req.on('error', () => resolve(0));
|
||
req.setTimeout(3000, () => {
|
||
req.destroy();
|
||
resolve(0);
|
||
});
|
||
req.end();
|
||
});
|
||
}
|
||
|
||
console.log('==> 本地测试域名诊断');
|
||
console.log('');
|
||
|
||
const issues = [];
|
||
|
||
for (const host of [H5_HOST, ADMIN_HOST, PLAZA_HOST, OPS_HOST]) {
|
||
if (!hostConfigured(host)) issues.push(`${host} 未写入 /etc/hosts → sudo pnpm setup:local-dns`);
|
||
else if (!resolverConfigured(host)) issues.push(`${host} 缺少 /etc/resolver → sudo pnpm setup:local-dns`);
|
||
else console.log(`✓ DNS ${host}`);
|
||
}
|
||
|
||
try {
|
||
const resolver = fs.readFileSync(`/etc/resolver/${H5_HOST}`, 'utf8');
|
||
const portMatch = resolver.match(/^port\s+(\d+)/m);
|
||
if (portMatch && Number(portMatch[1]) !== DNS_PORT) {
|
||
issues.push(`resolver 端口应为 ${DNS_PORT},请重新执行 sudo pnpm setup:local-dns`);
|
||
}
|
||
} catch {
|
||
// covered above
|
||
}
|
||
|
||
console.log('');
|
||
console.log('==> HTTPS 入口(需 sudo pnpm dev:local-proxy)');
|
||
|
||
const checks = [
|
||
['H5', `${H5_PUBLIC_BASE}/`, 200],
|
||
['memind_adm', `${publicUrl(ADMIN_HOST)}/healthz`, 200],
|
||
['Plaza', `${PLAZA_PUBLIC_BASE}/plaza`, 200],
|
||
['Ops', `${publicUrl(OPS_HOST)}/ops/`, 200],
|
||
];
|
||
|
||
for (const [label, url, expected] of checks) {
|
||
const code = await checkUrl(url);
|
||
if (code === expected || (label === 'Plaza' && [200, 307, 308].includes(code))) {
|
||
console.log(`✓ ${label.padEnd(12)} ${url} → ${code}`);
|
||
} else {
|
||
console.log(`✗ ${label.padEnd(12)} ${url} → ${code || '不可达'}`);
|
||
if (code === 0) {
|
||
issues.push(`${label} 不可达:先 pnpm dev,再 sudo pnpm dev:local-proxy`);
|
||
}
|
||
}
|
||
}
|
||
|
||
console.log('');
|
||
if (issues.length) {
|
||
console.log('待处理:');
|
||
for (const issue of issues) console.log(` - ${issue}`);
|
||
process.exit(1);
|
||
}
|
||
|
||
console.log('本地测试环境正常。');
|