mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 01:00:34 +08:00
227 lines
7.2 KiB
TypeScript
227 lines
7.2 KiB
TypeScript
// @ts-check
|
|
import path from 'node:path';
|
|
import { DOMESTICS, DOH_BOOTSTRAP } from '../Source/non_ip/domestic';
|
|
import { DIRECTS } from '../Source/non_ip/direct';
|
|
import type { DNSMapping } from '../Source/non_ip/direct';
|
|
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
|
import { compareAndWriteFile } from './lib/create-file';
|
|
import { task } from './trace';
|
|
import { SHARED_DESCRIPTION } from './lib/constants';
|
|
import { createMemoizedPromise } from './lib/memo-promise';
|
|
import * as yaml from 'yaml';
|
|
import { appendArrayInPlace } from './lib/append-array-in-place';
|
|
import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR, SOURCE_DIR } from './constants/dir';
|
|
import { RulesetOutput } from './lib/create-file';
|
|
|
|
export function createGetDnsMappingRule(allowWildcard: boolean) {
|
|
const hasWildcard = (domain: string) => {
|
|
if (domain.includes('*') || domain.includes('?')) {
|
|
if (!allowWildcard) {
|
|
throw new TypeError(`Wildcard domain is not supported: ${domain}`);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
};
|
|
|
|
return (domain: string): string[] => {
|
|
const results: string[] = [];
|
|
if (domain[0] === '$') {
|
|
const d = domain.slice(1);
|
|
if (hasWildcard(domain)) {
|
|
results.push(`DOMAIN-WILDCARD,${d}`);
|
|
} else {
|
|
results.push(`DOMAIN,${d}`);
|
|
}
|
|
} else if (domain[0] === '+') {
|
|
const d = domain.slice(1);
|
|
if (hasWildcard(domain)) {
|
|
results.push(`DOMAIN-WILDCARD,*.${d}`);
|
|
} else {
|
|
results.push(`DOMAIN-SUFFIX,${d}`);
|
|
}
|
|
} else if (hasWildcard(domain)) {
|
|
results.push(`DOMAIN-WILDCARD,${domain}`, `DOMAIN-WILDCARD,*.${domain}`);
|
|
} else {
|
|
results.push(`DOMAIN-SUFFIX,${domain}`);
|
|
}
|
|
|
|
return results;
|
|
};
|
|
}
|
|
|
|
export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(async () => {
|
|
const domestics = await readFileIntoProcessedArray(path.join(SOURCE_DIR, 'non_ip/domestic.conf'));
|
|
const directs = await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'non_ip/direct.conf'));
|
|
const lans: string[] = [];
|
|
|
|
const getDnsMappingRuleWithWildcard = createGetDnsMappingRule(true);
|
|
|
|
[DOH_BOOTSTRAP, DOMESTICS].forEach((item) => {
|
|
Object.values(item).forEach(({ domains }) => {
|
|
appendArrayInPlace(domestics, domains.flatMap(getDnsMappingRuleWithWildcard));
|
|
});
|
|
});
|
|
|
|
Object.values(DIRECTS).forEach(({ domains }) => {
|
|
appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
|
|
});
|
|
|
|
return [domestics, directs, lans] as const;
|
|
});
|
|
|
|
export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => {
|
|
const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise();
|
|
|
|
const dataset: DNSMapping[] = ([DOH_BOOTSTRAP, DOMESTICS, DIRECTS] as const).flatMap(Object.values);
|
|
|
|
return Promise.all([
|
|
new RulesetOutput(span, 'domestic', 'non_ip')
|
|
.withTitle('Sukka\'s Ruleset - Domestic Domains')
|
|
.withDescription([
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file contains known addresses that are avaliable in the Mainland China.'
|
|
])
|
|
.addFromRuleset(domestics)
|
|
.write(),
|
|
new RulesetOutput(span, 'direct', 'non_ip')
|
|
.withTitle('Sukka\'s Ruleset - Direct Rules')
|
|
.withDescription([
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file contains domains and process that should not be proxied.'
|
|
])
|
|
.addFromRuleset(directs)
|
|
.write(),
|
|
new RulesetOutput(span, 'lan', 'non_ip')
|
|
.withTitle('Sukka\'s Ruleset - LAN')
|
|
.withDescription([
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file includes rules for LAN DOMAIN and reserved TLDs.'
|
|
])
|
|
.addFromRuleset(lans)
|
|
.write(),
|
|
compareAndWriteFile(
|
|
span,
|
|
[
|
|
'#!name=[Sukka] Local DNS Mapping',
|
|
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
|
'',
|
|
'[Host]',
|
|
...Object.entries(
|
|
// I use an object to deduplicate the domains
|
|
// Otherwise I could just construct an array directly
|
|
dataset.reduce<Record<string, string>>((acc, cur) => {
|
|
const { domains, dns, hosts } = cur;
|
|
|
|
Object.entries(hosts).forEach(([dns, ips]) => {
|
|
if (!(dns in acc)) {
|
|
acc[dns] = ips.join(', ');
|
|
}
|
|
});
|
|
|
|
domains.forEach((domain) => {
|
|
if (domain[0] === '$') {
|
|
const d = domain.slice(1);
|
|
if (!(d in acc)) {
|
|
acc[d] = `server:${dns}`;
|
|
}
|
|
} else if (domain[0] === '+') {
|
|
const d = `*.${domain.slice(1)}`;
|
|
if (!(d in acc)) {
|
|
acc[d] = `server:${dns}`;
|
|
}
|
|
} else {
|
|
if (!(domain in acc)) {
|
|
acc[domain] = `server:${dns}`;
|
|
}
|
|
const d = `*.${domain}`;
|
|
if (!(d in acc)) {
|
|
acc[d] = `server:${dns}`;
|
|
}
|
|
}
|
|
});
|
|
|
|
return acc;
|
|
}, {})
|
|
).map(([dns, ips]) => `${dns} = ${ips}`)
|
|
],
|
|
path.resolve(OUTPUT_MODULES_DIR, 'sukka_local_dns_mapping.sgmodule')
|
|
),
|
|
compareAndWriteFile(
|
|
span,
|
|
yaml.stringify(
|
|
dataset.reduce<{
|
|
dns: { 'nameserver-policy': Record<string, string | string[]> },
|
|
hosts: Record<string, string>
|
|
}>((acc, cur) => {
|
|
const { domains, dns, ...rest } = cur;
|
|
domains.forEach((domain) => {
|
|
let domainWildcard = domain;
|
|
if (domain[0] === '$') {
|
|
domainWildcard = domain.slice(1);
|
|
} else if (domain[0] === '+') {
|
|
domainWildcard = `*.${domain.slice(1)}`;
|
|
} else {
|
|
domainWildcard = `+.${domain}`;
|
|
}
|
|
|
|
acc.dns['nameserver-policy'][domainWildcard] = dns === 'system'
|
|
? ['system://', 'system', 'dhcp://system']
|
|
: dns;
|
|
});
|
|
|
|
if ('hosts' in rest) {
|
|
Object.assign(acc.hosts, rest.hosts);
|
|
}
|
|
|
|
return acc;
|
|
}, {
|
|
dns: { 'nameserver-policy': {} },
|
|
hosts: {}
|
|
}),
|
|
{ version: '1.1' }
|
|
).split('\n'),
|
|
path.join(OUTPUT_INTERNAL_DIR, 'clash_nameserver_policy.yaml')
|
|
),
|
|
compareAndWriteFile(
|
|
span,
|
|
[
|
|
'# Local DNS Mapping for AdGuard Home',
|
|
'',
|
|
'[//]udp://10.10.1.1:53',
|
|
...dataset.flatMap(({ domains, dns: _dns }) => domains.flatMap((domain) => {
|
|
const dns = _dns === 'system'
|
|
? 'udp://10.10.1.1:53'
|
|
: _dns;
|
|
if (
|
|
// AdGuard Home has built-in AS112 / private PTR handling
|
|
domain.endsWith('.arpa')
|
|
// Ignore simple hostname
|
|
|| !domain.includes('.')
|
|
) {
|
|
return [];
|
|
}
|
|
if (domain[0] === '$') {
|
|
return [
|
|
`[/${domain.slice(1)}/]${dns}`
|
|
];
|
|
}
|
|
if (domain[0] === '+') {
|
|
return [
|
|
`[/${domain.slice(1)}/]${dns}`
|
|
];
|
|
}
|
|
return [
|
|
`[/${domain}/]${dns}`
|
|
];
|
|
}))
|
|
],
|
|
path.resolve(OUTPUT_INTERNAL_DIR, 'dns_mapping_adguardhome.conf')
|
|
)
|
|
]);
|
|
});
|