mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
136 lines
4.4 KiB
TypeScript
136 lines
4.4 KiB
TypeScript
// @ts-check
|
|
import path from 'path';
|
|
import { DOMESTICS } from '../Source/non_ip/domestic';
|
|
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
|
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
|
import { compareAndWriteFile, createRuleset } 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';
|
|
|
|
export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(async () => {
|
|
const domestics = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/non_ip/domestic.conf'));
|
|
const directs = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/non_ip/direct.conf'));
|
|
const lans: string[] = [];
|
|
|
|
Object.entries(DOMESTICS).forEach(([, { domains }]) => {
|
|
domestics.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
|
});
|
|
Object.entries(DIRECTS).forEach(([, { domains }]) => {
|
|
directs.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
|
});
|
|
Object.entries(LANS).forEach(([, { domains }]) => {
|
|
lans.push(...domains.map((domain) => `DOMAIN-SUFFIX,${domain}`));
|
|
});
|
|
|
|
return [domestics, directs, lans] as const;
|
|
});
|
|
|
|
export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(async (span) => {
|
|
const res = await getDomesticAndDirectDomainsRulesetPromise();
|
|
|
|
const dataset = [...Object.entries(DOMESTICS), ...Object.entries(DIRECTS), ...Object.entries(LANS)];
|
|
|
|
return Promise.all([
|
|
createRuleset(
|
|
span,
|
|
'Sukka\'s Ruleset - Domestic Domains',
|
|
[
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file contains known addresses that are avaliable in the Mainland China.'
|
|
],
|
|
new Date(),
|
|
res[0],
|
|
'ruleset',
|
|
path.resolve(import.meta.dir, '../List/non_ip/domestic.conf'),
|
|
path.resolve(import.meta.dir, '../Clash/non_ip/domestic.txt')
|
|
),
|
|
createRuleset(
|
|
span,
|
|
'Sukka\'s Ruleset - Direct Rules',
|
|
[
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file contains domains and process that should not be proxied.'
|
|
],
|
|
new Date(),
|
|
res[1],
|
|
'ruleset',
|
|
path.resolve(import.meta.dir, '../List/non_ip/direct.conf'),
|
|
path.resolve(import.meta.dir, '../Clash/non_ip/direct.txt')
|
|
),
|
|
createRuleset(
|
|
span,
|
|
'Sukka\'s Ruleset - LAN',
|
|
[
|
|
...SHARED_DESCRIPTION,
|
|
'',
|
|
'This file includes rules for LAN DOMAIN and reserved TLDs.'
|
|
],
|
|
new Date(),
|
|
res[2],
|
|
'ruleset',
|
|
path.resolve(import.meta.dir, '../List/non_ip/lan.conf'),
|
|
path.resolve(import.meta.dir, '../Clash/non_ip/lan.txt')
|
|
),
|
|
compareAndWriteFile(
|
|
span,
|
|
[
|
|
'#!name=[Sukka] Local DNS Mapping',
|
|
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
|
'',
|
|
'[Host]',
|
|
...dataset.flatMap(([, { domains, dns, ...rest }]) => [
|
|
...(
|
|
'hosts' in rest
|
|
? Object.entries(rest.hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`)
|
|
: []
|
|
),
|
|
...domains.flatMap((domain) => [
|
|
`${domain} = server:${dns}`,
|
|
`*.${domain} = server:${dns}`
|
|
])
|
|
])
|
|
],
|
|
path.resolve(import.meta.dir, '../Modules/sukka_local_dns_mapping.sgmodule')
|
|
),
|
|
Bun.write(
|
|
path.resolve(import.meta.dir, '../Internal/clash_nameserver_policy.yaml'),
|
|
yaml.stringify(
|
|
{
|
|
dns: {
|
|
'nameserver-policy': dataset.reduce<Record<string, string | string[]>>(
|
|
(acc, [, { domains, dns }]) => {
|
|
domains.forEach((domain) => {
|
|
acc[`+.${domain}`] = dns === 'system'
|
|
? [
|
|
'system://',
|
|
'system',
|
|
'dhcp://system'
|
|
]
|
|
: dns;
|
|
});
|
|
|
|
return acc;
|
|
},
|
|
{}
|
|
)
|
|
},
|
|
hosts: dataset.reduce<Record<string, string>>(
|
|
(acc, [, { domains, dns, ...rest }]) => {
|
|
if ('hosts' in rest) {
|
|
Object.assign(acc, rest.hosts);
|
|
}
|
|
return acc;
|
|
},
|
|
{}
|
|
)
|
|
},
|
|
{ version: '1.1' }
|
|
)
|
|
)
|
|
]);
|
|
});
|