121 lines
6.4 KiB
JavaScript
121 lines
6.4 KiB
JavaScript
import { readFileSync, readdirSync, writeFileSync } from 'node:fs';
|
||
import { domainToASCII, domainToUnicode } from 'node:url';
|
||
|
||
const configs = new URL('../../config/project-domains/', import.meta.url);
|
||
const roles = new Map([
|
||
['primary', '永久主域名'],
|
||
['direct', '免翻墙域名'],
|
||
['navigation', '导航站域名'],
|
||
]);
|
||
const accessLabels = new Map([
|
||
['requires_proxy', '需翻墙'],
|
||
['suspected_blocked', '疑似被墙'],
|
||
]);
|
||
|
||
function load(project) {
|
||
if (!/^[a-z][a-z0-9-]*$/.test(project)) throw new Error('无效项目代号');
|
||
const { domains } = JSON.parse(readFileSync(new URL(project + '.json', configs), 'utf8'));
|
||
if (!Array.isArray(domains) || !domains.length) throw new Error(project + ' 未配置域名');
|
||
const seenHosts = new Set();
|
||
const seenCodes = new Set();
|
||
const records = domains.map(({ domain, role, access, domainhub, recommended = false }) => {
|
||
const fail = () => { throw new Error(project + ' 域名记录无效:' + domain); };
|
||
if (typeof domain !== 'string' || /[\s:/@?#%\\]/u.test(domain)) fail();
|
||
const host = domainToASCII(domain);
|
||
const labels = host.split('.');
|
||
if (host.length > 253 || labels.length < 2 || !/[a-z]/.test(labels.at(-1))
|
||
|| labels.some(label => !/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(label))
|
||
|| seenHosts.has(host)) fail();
|
||
if (!roles.has(role) || (access !== undefined && !accessLabels.has(access))
|
||
|| typeof recommended !== 'boolean' || (recommended && (role !== 'direct' || access))) fail();
|
||
if (domainhub !== undefined) {
|
||
if (typeof domainhub !== 'string' || !/^[a-z0-9][a-z0-9.-]*$/.test(domainhub)
|
||
|| !domainhub.startsWith(project + '.') || seenCodes.has(domainhub)) fail();
|
||
seenCodes.add(domainhub);
|
||
}
|
||
seenHosts.add(host);
|
||
return {
|
||
name: domainToUnicode(host), origin: 'https://' + host + '/', role, access, domainhub, recommended,
|
||
label: roles.get(role) + (access ? '(' + accessLabels.get(access) + ')' : ''),
|
||
};
|
||
});
|
||
if (records.filter(record => record.recommended).length > 1) throw new Error(project + ' 有多个默认推荐域名');
|
||
return records;
|
||
}
|
||
|
||
function escapeHtml(value) {
|
||
const entities = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
||
return value.replace(/[&<>"']/g, char => entities[char]);
|
||
}
|
||
|
||
function renderNavigation(project, records, source) {
|
||
const recommended = records.find(record => record.recommended);
|
||
if (!recommended || records.filter(record => record.role === 'navigation').length !== 1) {
|
||
throw new Error(project + ' 导航需要一个导航域名和一个默认推荐域名');
|
||
}
|
||
const panels = records.filter(record => record.role !== 'navigation');
|
||
const priority = ({ recommended, role, access }) => role === 'primary' ? 0
|
||
: recommended ? 1 : access === 'suspected_blocked' ? 3 : 2;
|
||
panels.sort((a, b) => priority(a) - priority(b));
|
||
const options = panels.map(({ origin, name, label, recommended, domainhub, role }) => {
|
||
const code = domainhub ? ' data-domainhub-code="' + escapeHtml(domainhub) + '"' : '';
|
||
return [
|
||
' <label class="entry" data-role="' + role + '">',
|
||
' <input type="radio" name="route" value="' + escapeHtml(origin) + '"' + code + (recommended ? ' checked' : '') + '>',
|
||
' <span class="entry-info"><strong>' + escapeHtml(name) + '</strong><small>' + escapeHtml(label.replace('域名', '地址')) + '</small></span>',
|
||
' <span class="latency">—</span>',
|
||
' </label>',
|
||
].join('\n');
|
||
}).join('\n');
|
||
function replaceOnce(html, pattern, replacement) {
|
||
if ([...html.matchAll(pattern)].length !== 1) throw new Error('导航源标记不唯一:' + pattern);
|
||
return html.replace(pattern, replacement);
|
||
}
|
||
|
||
const preview = replaceOnce(source, /<!-- project-domains:start -->[\s\S]*?<!-- project-domains:end -->/g,
|
||
() => '<!-- project-domains:start -->\n' + options + '\n <!-- project-domains:end -->');
|
||
const withDefault = replaceOnce(preview, /(<span id="route-status" role="status">)[^<]*(<\/span>)/g,
|
||
(_, start, end) => start + '已选择 ' + escapeHtml(recommended.name) + end);
|
||
|
||
// The standalone release serves its own logo and does not require the XB theme path.
|
||
const withLogo = replaceOnce(withDefault, /(id="brand-icon" rel="icon" href=")\.\/native\/images\/brand\/fuc-mark\.svg/g,
|
||
(_, start) => start + './logo.svg');
|
||
const withFallback = replaceOnce(withLogo, /(id="enter" class="enter" href=")\/#\/register"/g,
|
||
(_, start) => start + '/_nav-go?route=' + encodeURIComponent(recommended.domainhub) + '"');
|
||
return {
|
||
preview: withDefault,
|
||
standalone: withFallback.replace('<!doctype html>', '<!doctype html>\n<!-- 从 theme/Fuc/assets/connect.html 和项目域名清单生成。 -->'),
|
||
};
|
||
}
|
||
|
||
try {
|
||
const [action, project, ...rest] = process.argv.slice(2);
|
||
if (action === 'list' && rest.length === 0) {
|
||
const projects = project ? [project] : readdirSync(configs)
|
||
.filter(name => /^[a-z][a-z0-9-]*\.json$/.test(name)).sort().map(name => name.slice(0, -5));
|
||
for (const key of projects) {
|
||
for (const record of load(key)) console.log([key, record.name, record.label, record.domainhub ?? ''].join('\t'));
|
||
}
|
||
} else if (['build', 'check'].includes(action) && project === 'fuc' && rest.length === 0) {
|
||
const source = new URL('../../theme/Fuc/assets/connect.html', import.meta.url);
|
||
const read = path => readFileSync(new URL('../../' + path, import.meta.url));
|
||
const { preview, standalone } = renderNavigation(project, load(project), readFileSync(source, 'utf8'));
|
||
const files = [
|
||
['theme/Fuc/assets/connect.html', Buffer.from(preview)],
|
||
['public/brand/fuc-navigation/index.html', Buffer.from(standalone)],
|
||
['public/brand/fuc-navigation/logo.svg', read('theme/Fuc/assets/native/images/brand/fuc-mark.svg')],
|
||
['public/brand/fuc-navigation/clipboard.mjs', read('theme/Fuc/assets/clipboard.mjs')],
|
||
['public/brand/fuc-navigation/navigation-domainhub.js', read('theme/Fuc/assets/navigation-domainhub.js')],
|
||
];
|
||
for (const [path, content] of files) {
|
||
if (action === 'build') writeFileSync(new URL('../../' + path, import.meta.url), content);
|
||
else if (!read(path).equals(content)) throw new Error(path + ' 已过期,请重新生成');
|
||
}
|
||
} else {
|
||
throw new Error('用法:manage.mjs list [项目代号] | build fuc | check fuc');
|
||
}
|
||
} catch (error) {
|
||
console.error(error.message);
|
||
process.exitCode = 1;
|
||
}
|