mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Feat: internal Clash YAML merge
This commit is contained in:
@@ -7,6 +7,7 @@ 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'));
|
||||
@@ -29,6 +30,8 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a
|
||||
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,
|
||||
@@ -79,20 +82,54 @@ export const buildDomesticRuleset = task(import.meta.main, import.meta.path)(asy
|
||||
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
||||
'',
|
||||
'[Host]',
|
||||
...([...Object.entries(DOMESTICS), ...Object.entries(DIRECTS), ...Object.entries(LANS)])
|
||||
.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}`
|
||||
])
|
||||
...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' }
|
||||
)
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -2,10 +2,9 @@ import path from 'path';
|
||||
import { task } from './trace';
|
||||
import { compareAndWriteFile } from './lib/create-file';
|
||||
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
||||
import * as yaml from 'yaml';
|
||||
|
||||
const HOSTNAMES = [
|
||||
// Intranet, Router Setup, and mant more
|
||||
...([Object.entries(DIRECTS), Object.entries(LANS)]).flatMap(data => data.flatMap(([, { domains }]) => domains.flatMap((domain) => [`*.${domain}`, domain]))),
|
||||
// Network Detection, Captive Portal
|
||||
'msftncsi.com',
|
||||
'msftconnecttest.com',
|
||||
@@ -41,18 +40,36 @@ const HOSTNAMES = [
|
||||
'*.battlenet.com.cn',
|
||||
'*.blzstatic.cn',
|
||||
'*.battlenet.com'
|
||||
] as const;
|
||||
];
|
||||
|
||||
export const buildAlwaysRealIPModule = task(import.meta.main, import.meta.path)(async (span) => {
|
||||
return compareAndWriteFile(
|
||||
span,
|
||||
[
|
||||
'#!name=[Sukka] Always Real IP Plus',
|
||||
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
||||
'',
|
||||
'[General]',
|
||||
`always-real-ip = %APPEND% ${HOSTNAMES.join(', ')}`
|
||||
],
|
||||
path.resolve(import.meta.dir, '../Modules/sukka_common_always_realip.sgmodule')
|
||||
);
|
||||
// Intranet, Router Setup, and mant more
|
||||
const dataset = ([Object.entries(DIRECTS), Object.entries(LANS)]);
|
||||
const surge = dataset.flatMap(data => data.flatMap(([, { domains }]) => domains.flatMap((domain) => [`*.${domain}`, domain])));
|
||||
const clash = dataset.flatMap(data => data.flatMap(([, { domains }]) => domains.map((domain) => `+.${domain}`)));
|
||||
|
||||
return Promise.all([
|
||||
compareAndWriteFile(
|
||||
span,
|
||||
[
|
||||
'#!name=[Sukka] Always Real IP Plus',
|
||||
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
||||
'',
|
||||
'[General]',
|
||||
`always-real-ip = %APPEND% ${HOSTNAMES.concat(surge).join(', ')}`
|
||||
],
|
||||
path.resolve(import.meta.dir, '../Modules/sukka_common_always_realip.sgmodule')
|
||||
),
|
||||
Bun.write(
|
||||
path.resolve(import.meta.dir, '../Internal/clash_fake_ip_filter.yaml'),
|
||||
yaml.stringify(
|
||||
{
|
||||
dns: {
|
||||
'fake-ip-filter': HOSTNAMES.concat(clash)
|
||||
}
|
||||
},
|
||||
{ version: '1.1' }
|
||||
)
|
||||
)
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user