mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Support Clash Fake IP Filter Ruleset
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
import path from 'node:path';
|
||||
import { DOMESTICS } from '../Source/non_ip/domestic';
|
||||
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
||||
import { DIRECTS } from '../Source/non_ip/direct';
|
||||
import { readFileIntoProcessedArray } from './lib/fetch-text-by-line';
|
||||
import { compareAndWriteFile } from './lib/create-file';
|
||||
import { task } from './trace';
|
||||
@@ -12,21 +12,42 @@ 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';
|
||||
|
||||
function getRule(domain: string): string[] {
|
||||
const results: string[] = [];
|
||||
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;
|
||||
}
|
||||
|
||||
if (domain[0] === '$' || domain[0] === '+') {
|
||||
results.push(`DOMAIN-SUFFIX,${domain.slice(1)}`);
|
||||
} else if (domain.includes('?')) {
|
||||
results.push(
|
||||
`DOMAIN-WILDCARD,${domain}`,
|
||||
`DOMAIN-WILDCARD,*.${domain}`
|
||||
);
|
||||
} else {
|
||||
results.push(`DOMAIN-SUFFIX,${domain}`);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return results;
|
||||
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 () => {
|
||||
@@ -34,14 +55,13 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a
|
||||
const directs = await readFileIntoProcessedArray(path.resolve(SOURCE_DIR, 'non_ip/direct.conf'));
|
||||
const lans: string[] = [];
|
||||
|
||||
Object.entries(DOMESTICS).forEach(([, { domains }]) => {
|
||||
appendArrayInPlace(domestics, domains.flatMap(getRule));
|
||||
const getDnsMappingRuleWithWildcard = createGetDnsMappingRule(true);
|
||||
|
||||
Object.values(DOMESTICS).forEach(({ domains }) => {
|
||||
appendArrayInPlace(domestics, domains.flatMap(getDnsMappingRuleWithWildcard));
|
||||
});
|
||||
Object.entries(DIRECTS).forEach(([, { domains }]) => {
|
||||
appendArrayInPlace(directs, domains.flatMap(getRule));
|
||||
});
|
||||
Object.entries(LANS).forEach(([, { domains }]) => {
|
||||
appendArrayInPlace(lans, domains.flatMap(getRule));
|
||||
Object.values(DIRECTS).forEach(({ domains }) => {
|
||||
appendArrayInPlace(directs, domains.flatMap(getDnsMappingRuleWithWildcard));
|
||||
});
|
||||
|
||||
return [domestics, directs, lans] as const;
|
||||
@@ -50,9 +70,7 @@ export const getDomesticAndDirectDomainsRulesetPromise = createMemoizedPromise(a
|
||||
export const buildDomesticRuleset = task(require.main === module, __filename)(async (span) => {
|
||||
const [domestics, directs, lans] = await getDomesticAndDirectDomainsRulesetPromise();
|
||||
|
||||
const dataset = Object.entries(DOMESTICS);
|
||||
appendArrayInPlace(dataset, Object.entries(DIRECTS));
|
||||
appendArrayInPlace(dataset, Object.entries(LANS));
|
||||
const dataset = appendArrayInPlace(Object.values(DOMESTICS), Object.values(DIRECTS));
|
||||
|
||||
return Promise.all([
|
||||
new RulesetOutput(span, 'domestic', 'non_ip')
|
||||
@@ -89,7 +107,7 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as
|
||||
`#!desc=Last Updated: ${new Date().toISOString()}`,
|
||||
'',
|
||||
'[Host]',
|
||||
...dataset.flatMap(([, { domains, dns, hosts }]) => [
|
||||
...dataset.flatMap(({ domains, dns, hosts }) => [
|
||||
...Object.entries(hosts).flatMap(([dns, ips]: [dns: string, ips: string[]]) => `${dns} = ${ips.join(', ')}`),
|
||||
...domains.flatMap((domain) => {
|
||||
if (domain[0] === '$') {
|
||||
@@ -114,44 +132,35 @@ export const buildDomesticRuleset = task(require.main === module, __filename)(as
|
||||
compareAndWriteFile(
|
||||
span,
|
||||
yaml.stringify(
|
||||
{
|
||||
dns: {
|
||||
'nameserver-policy': dataset.reduce<Record<string, string | string[]>>(
|
||||
(acc, [, { domains, dns }]) => {
|
||||
domains.forEach((domain) => {
|
||||
let domainWildcard = domain;
|
||||
if (domain[0] === '$') {
|
||||
domainWildcard = domain.slice(1);
|
||||
} else if (domain[0] === '+') {
|
||||
domainWildcard = `*.${domain.slice(1)}`;
|
||||
} else {
|
||||
domainWildcard = `+.${domain}`;
|
||||
}
|
||||
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[domainWildcard] = dns === 'system'
|
||||
? [
|
||||
'system://',
|
||||
'system',
|
||||
'dhcp://system'
|
||||
]
|
||||
: dns;
|
||||
});
|
||||
acc.dns['nameserver-policy'][domainWildcard] = 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;
|
||||
},
|
||||
{}
|
||||
)
|
||||
},
|
||||
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')
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
import path from 'node:path';
|
||||
import { task } from './trace';
|
||||
import { compareAndWriteFile } from './lib/create-file';
|
||||
import { DIRECTS, LANS } from '../Source/non_ip/direct';
|
||||
import { compareAndWriteFile, DomainsetOutput } from './lib/create-file';
|
||||
import { DIRECTS } from '../Source/non_ip/direct';
|
||||
import type { DNSMapping } from '../Source/non_ip/direct';
|
||||
import { DOMESTICS } from '../Source/non_ip/domestic';
|
||||
import * as yaml from 'yaml';
|
||||
import { OUTPUT_INTERNAL_DIR, OUTPUT_MODULES_DIR } from './constants/dir';
|
||||
import { appendArrayInPlace } from './lib/append-array-in-place';
|
||||
import { SHARED_DESCRIPTION } from './lib/constants';
|
||||
import { createGetDnsMappingRule } from './build-domestic-direct-lan-ruleset-dns-mapping-module';
|
||||
|
||||
const HOSTNAMES = [
|
||||
// Network Detection, Captive Portal
|
||||
@@ -33,22 +37,31 @@ const HOSTNAMES = [
|
||||
];
|
||||
|
||||
export const buildAlwaysRealIPModule = task(require.main === module, __filename)(async (span) => {
|
||||
const surge: string[] = [];
|
||||
const clashFakeIpFilter = new DomainsetOutput(span, 'clash_fake_ip_filter')
|
||||
.withTitle('Sukka\'s Ruleset - Always Real IP Plus')
|
||||
.withDescription([
|
||||
...SHARED_DESCRIPTION,
|
||||
'',
|
||||
'Clash.Meta fake-ip-filter as ruleset'
|
||||
]);
|
||||
|
||||
// Intranet, Router Setup, and mant more
|
||||
const dataset = [
|
||||
DIRECTS.HOTSPOT_CAPTIVE_PORTAL,
|
||||
DIRECTS.SYSTEM,
|
||||
...Object.values(LANS)
|
||||
];
|
||||
const surge = dataset.flatMap(({ domains }) => domains.flatMap((domain) => {
|
||||
switch (domain[0]) {
|
||||
case '+':
|
||||
return [`*.${domain.slice(1)}`];
|
||||
case '$':
|
||||
return [domain.slice(1)];
|
||||
default:
|
||||
return [domain, `*.${domain}`];
|
||||
}
|
||||
}));
|
||||
const dataset = [DIRECTS, DOMESTICS].reduce<DNSMapping[]>((acc, item) => {
|
||||
Object.values(item).forEach((i: DNSMapping) => {
|
||||
if (i.realip) {
|
||||
acc.push(i);
|
||||
}
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
|
||||
const getDnsMappingRuleWithoutWildcard = createGetDnsMappingRule(false);
|
||||
|
||||
for (const { domains } of dataset) {
|
||||
clashFakeIpFilter.addFromRuleset(domains.flatMap(getDnsMappingRuleWithoutWildcard));
|
||||
}
|
||||
|
||||
return Promise.all([
|
||||
compareAndWriteFile(
|
||||
@@ -62,6 +75,7 @@ export const buildAlwaysRealIPModule = task(require.main === module, __filename)
|
||||
],
|
||||
path.resolve(OUTPUT_MODULES_DIR, 'sukka_common_always_realip.sgmodule')
|
||||
),
|
||||
clashFakeIpFilter.writeClash(),
|
||||
compareAndWriteFile(
|
||||
span,
|
||||
yaml.stringify(
|
||||
|
||||
@@ -11,7 +11,7 @@ import type {
|
||||
Response
|
||||
} from 'undici';
|
||||
|
||||
export type UndiciResponseData = Dispatcher.ResponseData<any>;
|
||||
export type UndiciResponseData = Dispatcher.ResponseData;
|
||||
|
||||
import CacheableLookup from 'cacheable-lookup';
|
||||
import type { LookupOptions as CacheableLookupOptions } from 'cacheable-lookup';
|
||||
@@ -50,9 +50,9 @@ setGlobalDispatcher(agent.compose(
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (errorCode !== 'UND_ERR_REQ_RETRY') {
|
||||
return cb(err);
|
||||
}
|
||||
// if (errorCode === 'UND_ERR_REQ_RETRY') {
|
||||
// return cb(err);
|
||||
// }
|
||||
|
||||
const { method, retryOptions = {} } = opts;
|
||||
|
||||
|
||||
@@ -65,10 +65,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
return result;
|
||||
};
|
||||
|
||||
constructor(
|
||||
protected readonly span: Span,
|
||||
protected readonly id: string
|
||||
) { }
|
||||
constructor(protected readonly span: Span, protected readonly id: string) { }
|
||||
|
||||
protected title: string | null = null;
|
||||
withTitle(title: string) {
|
||||
@@ -245,7 +242,6 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
}
|
||||
|
||||
private $$preprocessed: TPreprocessed | null = null;
|
||||
|
||||
get $preprocessed() {
|
||||
if (this.$$preprocessed === null) {
|
||||
this.$$preprocessed = this.span.traceChildSync('RuleOutput#preprocess: ' + this.id, () => this.preprocess());
|
||||
@@ -253,6 +249,24 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
return this.$$preprocessed;
|
||||
}
|
||||
|
||||
async writeClash(outputDir?: null | string) {
|
||||
await this.done();
|
||||
|
||||
invariant(this.title, 'Missing title');
|
||||
invariant(this.description, 'Missing description');
|
||||
|
||||
return compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
this.clash()
|
||||
),
|
||||
path.join(outputDir ?? OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
|
||||
);
|
||||
}
|
||||
|
||||
async write(): Promise<void> {
|
||||
await this.done();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user