mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-28 17:41:54 +08:00
Refactor: new write strategy (#58)
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
import { OUTPUT_CLASH_DIR, OUTPUT_MODULES_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../../constants/dir';
|
||||
import type { Span } from '../../trace';
|
||||
import { HostnameSmolTrie } from '../trie';
|
||||
import stringify from 'json-stringify-pretty-compact';
|
||||
import path from 'node:path';
|
||||
import { withBannerArray } from '../misc';
|
||||
import { invariant } from 'foxts/guard';
|
||||
import { invariant, not } from 'foxts/guard';
|
||||
import picocolors from 'picocolors';
|
||||
import fs from 'node:fs';
|
||||
import { writeFile } from '../misc';
|
||||
import { fastStringArrayJoin } from 'foxts/fast-string-array-join';
|
||||
import { readFileByLine } from '../fetch-text-by-line';
|
||||
import { asyncWriteToStream } from 'foxts/async-write-to-stream';
|
||||
import type { BaseWriteStrategy } from '../writing-strategy/base';
|
||||
import { merge } from 'fast-cidr-tools';
|
||||
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
||||
import path from 'node:path';
|
||||
import { SurgeMitmSgmodule } from '../writing-strategy/surge';
|
||||
|
||||
export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
protected domainTrie = new HostnameSmolTrie(null);
|
||||
export class FileOutput {
|
||||
protected strategies: Array<BaseWriteStrategy | false> = [];
|
||||
|
||||
public domainTrie = new HostnameSmolTrie(null);
|
||||
protected domainKeywords = new Set<string>();
|
||||
protected domainWildcard = new Set<string>();
|
||||
protected userAgent = new Set<string>();
|
||||
@@ -34,38 +37,14 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
protected destPort = new Set<string>();
|
||||
|
||||
protected otherRules: string[] = [];
|
||||
protected abstract type: 'domainset' | 'non_ip' | 'ip' | (string & {});
|
||||
|
||||
private pendingPromise: Promise<any> | null = null;
|
||||
|
||||
static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
|
||||
|
||||
whitelistDomain = (domain: string) => {
|
||||
this.domainTrie.whitelist(domain);
|
||||
return this;
|
||||
};
|
||||
|
||||
static readonly domainWildCardToRegex = (domain: string) => {
|
||||
let result = '^';
|
||||
for (let i = 0, len = domain.length; i < len; i++) {
|
||||
switch (domain[i]) {
|
||||
case '.':
|
||||
result += String.raw`\.`;
|
||||
break;
|
||||
case '*':
|
||||
result += String.raw`[\w.-]*?`;
|
||||
break;
|
||||
case '?':
|
||||
result += String.raw`[\w.-]`;
|
||||
break;
|
||||
default:
|
||||
result += domain[i];
|
||||
}
|
||||
}
|
||||
result += '$';
|
||||
return result;
|
||||
};
|
||||
|
||||
protected readonly span: Span;
|
||||
|
||||
constructor($span: Span, protected readonly id: string) {
|
||||
@@ -78,6 +57,17 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
return this;
|
||||
}
|
||||
|
||||
replaceStrategies(strategies: Array<BaseWriteStrategy | false>) {
|
||||
this.strategies = strategies;
|
||||
return this;
|
||||
}
|
||||
|
||||
withExtraStrategies(strategy: BaseWriteStrategy | false) {
|
||||
if (strategy) {
|
||||
this.strategies.push(strategy);
|
||||
}
|
||||
}
|
||||
|
||||
protected description: string[] | readonly string[] | null = null;
|
||||
withDescription(description: string[] | readonly string[]) {
|
||||
this.description = description;
|
||||
@@ -233,164 +223,246 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
|
||||
|
||||
bulkAddCIDR4(cidrs: string[]) {
|
||||
for (let i = 0, len = cidrs.length; i < len; i++) {
|
||||
this.ipcidr.add(RuleOutput.ipToCidr(cidrs[i], 4));
|
||||
this.ipcidr.add(FileOutput.ipToCidr(cidrs[i], 4));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddCIDR4NoResolve(cidrs: string[]) {
|
||||
for (let i = 0, len = cidrs.length; i < len; i++) {
|
||||
this.ipcidrNoResolve.add(RuleOutput.ipToCidr(cidrs[i], 4));
|
||||
this.ipcidrNoResolve.add(FileOutput.ipToCidr(cidrs[i], 4));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddCIDR6(cidrs: string[]) {
|
||||
for (let i = 0, len = cidrs.length; i < len; i++) {
|
||||
this.ipcidr6.add(RuleOutput.ipToCidr(cidrs[i], 6));
|
||||
this.ipcidr6.add(FileOutput.ipToCidr(cidrs[i], 6));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddCIDR6NoResolve(cidrs: string[]) {
|
||||
for (let i = 0, len = cidrs.length; i < len; i++) {
|
||||
this.ipcidr6NoResolve.add(RuleOutput.ipToCidr(cidrs[i], 6));
|
||||
this.ipcidr6NoResolve.add(FileOutput.ipToCidr(cidrs[i], 6));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected abstract preprocess(): TPreprocessed extends null ? null : NonNullable<TPreprocessed>;
|
||||
|
||||
async done() {
|
||||
await this.pendingPromise;
|
||||
this.pendingPromise = null;
|
||||
return this;
|
||||
}
|
||||
|
||||
private guardPendingPromise() {
|
||||
// reverse invariant
|
||||
if (this.pendingPromise !== null) {
|
||||
console.trace('Pending promise:', this.pendingPromise);
|
||||
throw new Error('You should call done() before calling this method');
|
||||
// private guardPendingPromise() {
|
||||
// // reverse invariant
|
||||
// if (this.pendingPromise !== null) {
|
||||
// console.trace('Pending promise:', this.pendingPromise);
|
||||
// throw new Error('You should call done() before calling this method');
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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')
|
||||
// );
|
||||
// }
|
||||
private strategiesWritten = false;
|
||||
|
||||
private async writeToStrategies() {
|
||||
if (this.strategiesWritten) {
|
||||
throw new Error('Strategies already written');
|
||||
}
|
||||
}
|
||||
|
||||
private $$preprocessed: TPreprocessed | null = null;
|
||||
protected runPreprocess() {
|
||||
if (this.$$preprocessed === null) {
|
||||
this.guardPendingPromise();
|
||||
this.strategiesWritten = true;
|
||||
|
||||
this.$$preprocessed = this.span.traceChildSync('preprocess', () => this.preprocess());
|
||||
}
|
||||
}
|
||||
|
||||
get $preprocessed(): TPreprocessed extends null ? null : NonNullable<TPreprocessed> {
|
||||
this.runPreprocess();
|
||||
return this.$$preprocessed as any;
|
||||
}
|
||||
|
||||
async writeClash(outputDir?: null | string) {
|
||||
await this.done();
|
||||
|
||||
invariant(this.title, 'Missing title');
|
||||
invariant(this.description, 'Missing description');
|
||||
const kwfilter = createKeywordFilter(Array.from(this.domainKeywords));
|
||||
|
||||
return compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
this.clash()
|
||||
),
|
||||
path.join(outputDir ?? OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
|
||||
);
|
||||
if (this.strategies.filter(not(false)).length === 0) {
|
||||
throw new Error('No strategies to write ' + this.id);
|
||||
}
|
||||
|
||||
this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => {
|
||||
if (kwfilter(domain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (strategy) {
|
||||
if (includeAllSubdomain) {
|
||||
strategy.writeDomainSuffix(domain);
|
||||
} else {
|
||||
strategy.writeDomain(domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, true);
|
||||
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (!strategy) continue;
|
||||
|
||||
if (this.domainKeywords.size) {
|
||||
strategy.writeDomainKeywords(this.domainKeywords);
|
||||
}
|
||||
if (this.domainWildcard.size) {
|
||||
strategy.writeDomainWildcards(this.domainWildcard);
|
||||
}
|
||||
if (this.userAgent.size) {
|
||||
strategy.writeUserAgents(this.userAgent);
|
||||
}
|
||||
if (this.processName.size) {
|
||||
strategy.writeProcessNames(this.processName);
|
||||
}
|
||||
if (this.processPath.size) {
|
||||
strategy.writeProcessPaths(this.processPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.sourceIpOrCidr.size) {
|
||||
const sourceIpOrCidr = Array.from(this.sourceIpOrCidr);
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (strategy) {
|
||||
strategy.writeSourceIpCidrs(sourceIpOrCidr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (strategy) {
|
||||
if (this.sourcePort.size) {
|
||||
strategy.writeSourcePorts(this.sourcePort);
|
||||
}
|
||||
if (this.destPort.size) {
|
||||
strategy.writeDestinationPorts(this.destPort);
|
||||
}
|
||||
if (this.otherRules.length) {
|
||||
strategy.writeOtherRules(this.otherRules);
|
||||
}
|
||||
if (this.urlRegex.size) {
|
||||
strategy.writeUrlRegexes(this.urlRegex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let ipcidr: string[] | null = null;
|
||||
let ipcidrNoResolve: string[] | null = null;
|
||||
let ipcidr6: string[] | null = null;
|
||||
let ipcidr6NoResolve: string[] | null = null;
|
||||
|
||||
if (this.ipcidr.size) {
|
||||
ipcidr = merge(Array.from(this.ipcidr));
|
||||
}
|
||||
if (this.ipcidrNoResolve.size) {
|
||||
ipcidrNoResolve = merge(Array.from(this.ipcidrNoResolve));
|
||||
}
|
||||
if (this.ipcidr6.size) {
|
||||
ipcidr6 = Array.from(this.ipcidr6);
|
||||
}
|
||||
if (this.ipcidr6NoResolve.size) {
|
||||
ipcidr6NoResolve = Array.from(this.ipcidr6NoResolve);
|
||||
}
|
||||
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (strategy) {
|
||||
// no-resolve
|
||||
if (ipcidrNoResolve?.length) {
|
||||
strategy.writeIpCidrs(ipcidrNoResolve, true);
|
||||
}
|
||||
if (ipcidr6NoResolve?.length) {
|
||||
strategy.writeIpCidr6s(ipcidr6NoResolve, true);
|
||||
}
|
||||
if (this.ipasnNoResolve.size) {
|
||||
strategy.writeIpAsns(this.ipasnNoResolve, true);
|
||||
}
|
||||
if (this.groipNoResolve.size) {
|
||||
strategy.writeGeoip(this.groipNoResolve, true);
|
||||
}
|
||||
|
||||
// triggers DNS resolution
|
||||
if (ipcidr?.length) {
|
||||
strategy.writeIpCidrs(ipcidr, false);
|
||||
}
|
||||
if (ipcidr6?.length) {
|
||||
strategy.writeIpCidr6s(ipcidr6, false);
|
||||
}
|
||||
if (this.ipasn.size) {
|
||||
strategy.writeIpAsns(this.ipasn, false);
|
||||
}
|
||||
if (this.geoip.size) {
|
||||
strategy.writeGeoip(this.geoip, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
write({
|
||||
surge = true,
|
||||
clash = true,
|
||||
singbox = true,
|
||||
surgeDir = OUTPUT_SURGE_DIR,
|
||||
clashDir = OUTPUT_CLASH_DIR,
|
||||
singboxDir = OUTPUT_SINGBOX_DIR
|
||||
}: {
|
||||
surge?: boolean,
|
||||
clash?: boolean,
|
||||
singbox?: boolean,
|
||||
surgeDir?: string,
|
||||
clashDir?: string,
|
||||
singboxDir?: string
|
||||
} = {}): Promise<void> {
|
||||
return this.done().then(() => this.span.traceChildAsync('write all', async () => {
|
||||
write(): Promise<void> {
|
||||
return this.span.traceChildAsync('write all', async () => {
|
||||
const promises: Array<Promise<void> | void> = [];
|
||||
|
||||
await this.writeToStrategies();
|
||||
|
||||
invariant(this.title, 'Missing title');
|
||||
invariant(this.description, 'Missing description');
|
||||
|
||||
const promises: Array<Promise<void>> = [];
|
||||
|
||||
if (surge) {
|
||||
promises.push(compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
for (let i = 0, len = this.strategies.length; i < len; i++) {
|
||||
const strategy = this.strategies[i];
|
||||
if (strategy) {
|
||||
const basename = (strategy.overwriteFilename || this.id) + '.' + strategy.fileExtension;
|
||||
promises.push(strategy.output(
|
||||
this.span,
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
this.surge()
|
||||
),
|
||||
path.join(surgeDir, this.type, this.id + '.conf')
|
||||
));
|
||||
}
|
||||
if (clash) {
|
||||
promises.push(compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
this.clash()
|
||||
),
|
||||
path.join(clashDir, this.type, this.id + '.txt')
|
||||
));
|
||||
}
|
||||
if (singbox) {
|
||||
promises.push(compareAndWriteFile(
|
||||
this.span,
|
||||
this.singbox(),
|
||||
path.join(singboxDir, this.type, this.id + '.json')
|
||||
));
|
||||
}
|
||||
|
||||
if (this.mitmSgmodule) {
|
||||
const sgmodule = this.mitmSgmodule();
|
||||
const sgModulePath = this.mitmSgmodulePath ?? path.join(this.type, this.id + '.sgmodule');
|
||||
|
||||
if (sgmodule) {
|
||||
promises.push(
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
sgmodule,
|
||||
path.join(OUTPUT_MODULES_DIR, sgModulePath)
|
||||
)
|
||||
);
|
||||
strategy.type
|
||||
? path.join(strategy.type, basename)
|
||||
: basename
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
abstract surge(): string[];
|
||||
abstract clash(): string[];
|
||||
abstract singbox(): string[];
|
||||
async output(): Promise<Array<string[] | null>> {
|
||||
await this.writeToStrategies();
|
||||
|
||||
protected mitmSgmodulePath: string | null = null;
|
||||
withMitmSgmodulePath(path: string | null) {
|
||||
if (path) {
|
||||
this.mitmSgmodulePath = path;
|
||||
return this.strategies.reduce<Array<string[] | null>>((acc, strategy) => {
|
||||
if (strategy) {
|
||||
acc.push(strategy.content);
|
||||
} else {
|
||||
acc.push(null);
|
||||
}
|
||||
return acc;
|
||||
}, []);
|
||||
}
|
||||
|
||||
withMitmSgmodulePath(moduleName: string | null) {
|
||||
if (moduleName) {
|
||||
this.withExtraStrategies(new SurgeMitmSgmodule(moduleName));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
abstract mitmSgmodule?(): string[] | null;
|
||||
}
|
||||
|
||||
export async function fileEqual(linesA: string[], source: AsyncIterable<string> | Iterable<string>): Promise<boolean> {
|
||||
|
||||
@@ -1,107 +1,15 @@
|
||||
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
||||
import { RuleOutput } from './base';
|
||||
import type { SingboxSourceFormat } from '../singbox';
|
||||
import type { BaseWriteStrategy } from '../writing-strategy/base';
|
||||
import { ClashDomainSet } from '../writing-strategy/clash';
|
||||
import { SingboxSource } from '../writing-strategy/singbox';
|
||||
import { SurgeDomainSet } from '../writing-strategy/surge';
|
||||
import { FileOutput } from './base';
|
||||
|
||||
import { escapeStringRegexp } from 'foxts/escape-string-regexp';
|
||||
|
||||
export class DomainsetOutput extends RuleOutput<string[]> {
|
||||
export class DomainsetOutput extends FileOutput {
|
||||
protected type = 'domainset' as const;
|
||||
|
||||
private $surge: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
private $clash: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
private $singbox_domains: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
private $singbox_domains_suffixes: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
private $adguardhome: string[] = [];
|
||||
preprocess() {
|
||||
const kwfilter = createKeywordFilter(Array.from(this.domainKeywords));
|
||||
|
||||
this.domainTrie.dumpWithoutDot((domain, subdomain) => {
|
||||
if (kwfilter(domain)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.$surge.push(subdomain ? '.' + domain : domain);
|
||||
this.$clash.push(subdomain ? `+.${domain}` : domain);
|
||||
(subdomain ? this.$singbox_domains_suffixes : this.$singbox_domains).push(domain);
|
||||
this.$adguardhome.push(subdomain ? `||${domain}^` : `|${domain}^`);
|
||||
}, true);
|
||||
|
||||
return this.$surge;
|
||||
}
|
||||
|
||||
surge(): string[] {
|
||||
this.runPreprocess();
|
||||
return this.$surge;
|
||||
}
|
||||
|
||||
clash(): string[] {
|
||||
this.runPreprocess();
|
||||
return this.$clash;
|
||||
}
|
||||
|
||||
singbox(): string[] {
|
||||
this.runPreprocess();
|
||||
|
||||
return RuleOutput.jsonToLines({
|
||||
version: 2,
|
||||
rules: [{
|
||||
domain: this.$singbox_domains,
|
||||
domain_suffix: this.$singbox_domains_suffixes
|
||||
}]
|
||||
} satisfies SingboxSourceFormat);
|
||||
}
|
||||
|
||||
mitmSgmodule = undefined;
|
||||
|
||||
adguardhome(): string[] {
|
||||
this.runPreprocess();
|
||||
|
||||
// const whitelistArray = sortDomains(Array.from(whitelist));
|
||||
// for (let i = 0, len = whitelistArray.length; i < len; i++) {
|
||||
// const domain = whitelistArray[i];
|
||||
// if (domain[0] === '.') {
|
||||
// results.push(`@@||${domain.slice(1)}^`);
|
||||
// } else {
|
||||
// results.push(`@@|${domain}^`);
|
||||
// }
|
||||
// }
|
||||
|
||||
for (const wildcard of this.domainWildcard) {
|
||||
const processed = wildcard.replaceAll('?', '*');
|
||||
if (processed.startsWith('*.')) {
|
||||
this.$adguardhome.push(`||${processed.slice(2)}^`);
|
||||
} else {
|
||||
this.$adguardhome.push(`|${processed}^`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const keyword of this.domainKeywords) {
|
||||
// Use regex to match keyword
|
||||
this.$adguardhome.push(`/${escapeStringRegexp(keyword)}/`);
|
||||
}
|
||||
|
||||
for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
|
||||
for (const ipcidr of ipGroup) {
|
||||
if (ipcidr.endsWith('/32')) {
|
||||
this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
|
||||
/* else if (ipcidr.endsWith('.0/24')) {
|
||||
results.push(`||${ipcidr.slice(0, -6)}.*`);
|
||||
} */
|
||||
} else {
|
||||
this.$adguardhome.push(`||${ipcidr}^`);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
|
||||
for (const ipcidr of ipGroup) {
|
||||
if (ipcidr.endsWith('/128')) {
|
||||
this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
|
||||
} else {
|
||||
this.$adguardhome.push(`||${ipcidr}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.$adguardhome;
|
||||
}
|
||||
strategies: Array<false | BaseWriteStrategy> = [
|
||||
new SurgeDomainSet(),
|
||||
new ClashDomainSet(),
|
||||
new SingboxSource(this.type)
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,75 +1,21 @@
|
||||
import type { Span } from '../../trace';
|
||||
import { appendArrayInPlace } from '../append-array-in-place';
|
||||
import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
|
||||
import type { SingboxSourceFormat } from '../singbox';
|
||||
import { RuleOutput } from './base';
|
||||
import type { BaseWriteStrategy } from '../writing-strategy/base';
|
||||
import { ClashClassicRuleSet, ClashIPSet } from '../writing-strategy/clash';
|
||||
import { SingboxSource } from '../writing-strategy/singbox';
|
||||
import { SurgeRuleSet } from '../writing-strategy/surge';
|
||||
import { FileOutput } from './base';
|
||||
|
||||
import { merge } from 'fast-cidr-tools';
|
||||
|
||||
type Preprocessed = string[];
|
||||
|
||||
export class IPListOutput extends RuleOutput<Preprocessed> {
|
||||
export class IPListOutput extends FileOutput {
|
||||
protected type = 'ip' as const;
|
||||
strategies: Array<false | BaseWriteStrategy>;
|
||||
|
||||
constructor(span: Span, id: string, private readonly clashUseRule = true) {
|
||||
super(span, id);
|
||||
}
|
||||
|
||||
mitmSgmodule = undefined;
|
||||
|
||||
protected preprocess() {
|
||||
const results: string[] = [];
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(
|
||||
appendSetElementsToArray(Array.from(this.ipcidrNoResolve), this.ipcidr),
|
||||
true
|
||||
)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6NoResolve);
|
||||
appendSetElementsToArray(results, this.ipcidr6);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private $surge: string[] | null = null;
|
||||
|
||||
surge(): string[] {
|
||||
if (!this.$surge) {
|
||||
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidrNoResolve)).map(i => `IP-CIDR,${i},no-resolve`, true)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6NoResolve, i => `IP-CIDR6,${i},no-resolve`);
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidr)).map(i => `IP-CIDR,${i}`, true)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
|
||||
|
||||
this.$surge = results;
|
||||
}
|
||||
return this.$surge;
|
||||
}
|
||||
|
||||
clash(): string[] {
|
||||
if (this.clashUseRule) {
|
||||
return this.surge();
|
||||
}
|
||||
|
||||
return this.$preprocessed;
|
||||
}
|
||||
|
||||
singbox(): string[] {
|
||||
const singbox: SingboxSourceFormat = {
|
||||
version: 2,
|
||||
rules: [{
|
||||
domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'],
|
||||
ip_cidr: this.$preprocessed
|
||||
}]
|
||||
};
|
||||
return RuleOutput.jsonToLines(singbox);
|
||||
this.strategies = [
|
||||
new SurgeRuleSet(this.type),
|
||||
this.clashUseRule ? new ClashClassicRuleSet(this.type) : new ClashIPSet(),
|
||||
new SingboxSource(this.type)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,283 +1,17 @@
|
||||
import { merge } from 'fast-cidr-tools';
|
||||
import type { Span } from '../../trace';
|
||||
import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
|
||||
import { appendArrayInPlace } from '../append-array-in-place';
|
||||
import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
|
||||
import type { SingboxSourceFormat } from '../singbox';
|
||||
import { RuleOutput } from './base';
|
||||
import picocolors from 'picocolors';
|
||||
import { normalizeDomain } from '../normalize-domain';
|
||||
import { isProbablyIpv4 } from 'foxts/is-probably-ip';
|
||||
import { fastIpVersion } from '../misc';
|
||||
import { ClashClassicRuleSet } from '../writing-strategy/clash';
|
||||
import { SingboxSource } from '../writing-strategy/singbox';
|
||||
import { SurgeRuleSet } from '../writing-strategy/surge';
|
||||
import { FileOutput } from './base';
|
||||
|
||||
type Preprocessed = [domain: string[], domainSuffix: string[], sortedDomainRules: string[]];
|
||||
|
||||
export class RulesetOutput extends RuleOutput<Preprocessed> {
|
||||
export class RulesetOutput extends FileOutput {
|
||||
constructor(span: Span, id: string, protected type: 'non_ip' | 'ip' | (string & {})) {
|
||||
super(span, id);
|
||||
}
|
||||
|
||||
protected preprocess() {
|
||||
const kwfilter = createKeywordFilter(Array.from(this.domainKeywords));
|
||||
|
||||
const domains: string[] = [];
|
||||
const domainSuffixes: string[] = [];
|
||||
const sortedDomainRules: string[] = [];
|
||||
|
||||
this.domainTrie.dumpWithoutDot((domain, includeAllSubdomain) => {
|
||||
if (kwfilter(domain)) {
|
||||
return;
|
||||
}
|
||||
if (includeAllSubdomain) {
|
||||
domainSuffixes.push(domain);
|
||||
sortedDomainRules.push(`DOMAIN-SUFFIX,${domain}`);
|
||||
} else {
|
||||
domains.push(domain);
|
||||
sortedDomainRules.push(`DOMAIN,${domain}`);
|
||||
}
|
||||
}, true);
|
||||
|
||||
return [domains, domainSuffixes, sortedDomainRules] satisfies Preprocessed;
|
||||
}
|
||||
|
||||
surge(): string[] {
|
||||
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
appendArrayInPlace(results, this.$preprocessed[2]);
|
||||
|
||||
appendSetElementsToArray(results, this.domainKeywords, i => `DOMAIN-KEYWORD,${i}`);
|
||||
appendSetElementsToArray(results, this.domainWildcard, i => `DOMAIN-WILDCARD,${i}`);
|
||||
|
||||
appendSetElementsToArray(results, this.userAgent, i => `USER-AGENT,${i}`);
|
||||
|
||||
appendSetElementsToArray(results, this.processName, i => `PROCESS-NAME,${i}`);
|
||||
appendSetElementsToArray(results, this.processPath, i => `PROCESS-NAME,${i}`);
|
||||
|
||||
appendSetElementsToArray(results, this.sourceIpOrCidr, i => `SRC-IP,${i}`);
|
||||
appendSetElementsToArray(results, this.sourcePort, i => `SRC-PORT,${i}`);
|
||||
appendSetElementsToArray(results, this.destPort, i => `DEST-PORT,${i}`);
|
||||
|
||||
appendArrayInPlace(results, this.otherRules);
|
||||
|
||||
appendSetElementsToArray(results, this.urlRegex, i => `URL-REGEX,${i}`);
|
||||
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidrNoResolve), true).map(i => `IP-CIDR,${i},no-resolve`)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6NoResolve, i => `IP-CIDR6,${i},no-resolve`);
|
||||
appendSetElementsToArray(results, this.ipasnNoResolve, i => `IP-ASN,${i},no-resolve`);
|
||||
appendSetElementsToArray(results, this.groipNoResolve, i => `GEOIP,${i},no-resolve`);
|
||||
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidr), true).map(i => `IP-CIDR,${i}`)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
|
||||
appendSetElementsToArray(results, this.ipasn, i => `IP-ASN,${i}`);
|
||||
appendSetElementsToArray(results, this.geoip, i => `GEOIP,${i}`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
clash(): string[] {
|
||||
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
|
||||
|
||||
appendArrayInPlace(results, this.$preprocessed[2]);
|
||||
|
||||
appendSetElementsToArray(results, this.domainKeywords, i => `DOMAIN-KEYWORD,${i}`);
|
||||
appendSetElementsToArray(results, this.domainWildcard, i => `DOMAIN-REGEX,${RuleOutput.domainWildCardToRegex(i)}`);
|
||||
|
||||
appendSetElementsToArray(results, this.processName, i => `PROCESS-NAME,${i}`);
|
||||
appendSetElementsToArray(results, this.processPath, i => `PROCESS-PATH,${i}`);
|
||||
|
||||
appendSetElementsToArray(results, this.sourceIpOrCidr, value => {
|
||||
if (value.includes('/')) {
|
||||
return `SRC-IP-CIDR,${value}`;
|
||||
}
|
||||
const v = fastIpVersion(value);
|
||||
if (v === 4) {
|
||||
return `SRC-IP-CIDR,${value}/32`;
|
||||
}
|
||||
if (v === 6) {
|
||||
return `SRC-IP-CIDR6,${value}/128`;
|
||||
}
|
||||
return '';
|
||||
});
|
||||
appendSetElementsToArray(results, this.sourcePort, i => `SRC-PORT,${i}`);
|
||||
appendSetElementsToArray(results, this.destPort, i => `DST-PORT,${i}`);
|
||||
|
||||
// appendArrayInPlace(results, this.otherRules);
|
||||
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidrNoResolve), true).map(i => `IP-CIDR,${i},no-resolve`)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6NoResolve, i => `IP-CIDR6,${i},no-resolve`);
|
||||
appendSetElementsToArray(results, this.ipasnNoResolve, i => `IP-ASN,${i},no-resolve`);
|
||||
appendSetElementsToArray(results, this.groipNoResolve, i => `GEOIP,${i},no-resolve`);
|
||||
|
||||
appendArrayInPlace(
|
||||
results,
|
||||
merge(Array.from(this.ipcidr), true).map(i => `IP-CIDR,${i}`)
|
||||
);
|
||||
appendSetElementsToArray(results, this.ipcidr6, i => `IP-CIDR6,${i}`);
|
||||
appendSetElementsToArray(results, this.ipasn, i => `IP-ASN,${i}`);
|
||||
appendSetElementsToArray(results, this.geoip, i => `GEOIP,${i}`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
singbox(): string[] {
|
||||
const ip_cidr: string[] = [];
|
||||
appendArrayInPlace(
|
||||
ip_cidr,
|
||||
merge(
|
||||
appendSetElementsToArray(Array.from(this.ipcidrNoResolve), this.ipcidr),
|
||||
true
|
||||
)
|
||||
);
|
||||
appendSetElementsToArray(ip_cidr, this.ipcidr6NoResolve);
|
||||
appendSetElementsToArray(ip_cidr, this.ipcidr6);
|
||||
|
||||
const singbox: SingboxSourceFormat = {
|
||||
version: 2,
|
||||
rules: [{
|
||||
domain: appendArrayInPlace(['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'], this.$preprocessed[0]),
|
||||
domain_suffix: this.$preprocessed[1],
|
||||
domain_keyword: Array.from(this.domainKeywords),
|
||||
domain_regex: Array.from(this.domainWildcard, RuleOutput.domainWildCardToRegex),
|
||||
ip_cidr,
|
||||
source_ip_cidr: [...this.sourceIpOrCidr].reduce<string[]>((acc, cur) => {
|
||||
if (cur.includes('/')) {
|
||||
acc.push(cur);
|
||||
} else {
|
||||
const v = fastIpVersion(cur);
|
||||
|
||||
if (v === 4) {
|
||||
acc.push(cur + '/32');
|
||||
} else if (v === 6) {
|
||||
acc.push(cur + '/128');
|
||||
}
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []),
|
||||
source_port: [...this.sourcePort].reduce<number[]>((acc, cur) => {
|
||||
const tmp = Number(cur);
|
||||
if (!Number.isNaN(tmp)) {
|
||||
acc.push(tmp);
|
||||
}
|
||||
return acc;
|
||||
}, []),
|
||||
port: [...this.destPort].reduce<number[]>((acc, cur) => {
|
||||
const tmp = Number(cur);
|
||||
if (!Number.isNaN(tmp)) {
|
||||
acc.push(tmp);
|
||||
}
|
||||
return acc;
|
||||
}, []),
|
||||
process_name: Array.from(this.processName),
|
||||
process_path: Array.from(this.processPath)
|
||||
}]
|
||||
};
|
||||
|
||||
return RuleOutput.jsonToLines(singbox);
|
||||
}
|
||||
|
||||
mitmSgmodule(): string[] | null {
|
||||
if (this.urlRegex.size === 0 || this.mitmSgmodulePath === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const urlRegexResults: Array<{ origin: string, processed: string[] }> = [];
|
||||
|
||||
const parsedFailures: Array<[original: string, processed: string]> = [];
|
||||
const parsed: Array<[original: string, domain: string]> = [];
|
||||
|
||||
for (let urlRegex of this.urlRegex) {
|
||||
if (
|
||||
urlRegex.startsWith('http://')
|
||||
|| urlRegex.startsWith('^http://')
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (urlRegex.startsWith('^https?://')) {
|
||||
urlRegex = urlRegex.slice(10);
|
||||
}
|
||||
if (urlRegex.startsWith('^https://')) {
|
||||
urlRegex = urlRegex.slice(9);
|
||||
}
|
||||
|
||||
const potentialHostname = urlRegex.split('/')[0]
|
||||
// pre process regex
|
||||
.replaceAll(String.raw`\.`, '.')
|
||||
.replaceAll('.+', '*')
|
||||
.replaceAll(/([a-z])\?/g, '($1|)')
|
||||
// convert regex to surge hostlist syntax
|
||||
.replaceAll('([a-z])', '?')
|
||||
.replaceAll(String.raw`\d`, '?')
|
||||
.replaceAll(/\*+/g, '*');
|
||||
|
||||
let processed: string[] = [potentialHostname];
|
||||
|
||||
const matches = [...potentialHostname.matchAll(/\((?:([^()|]+)\|)+([^()|]*)\)/g)];
|
||||
|
||||
if (matches.length > 0) {
|
||||
const replaceVariant = (combinations: string[], fullMatch: string, options: string[]): string[] => {
|
||||
const newCombinations: string[] = [];
|
||||
|
||||
combinations.forEach(combination => {
|
||||
options.forEach(option => {
|
||||
newCombinations.push(combination.replace(fullMatch, option));
|
||||
});
|
||||
});
|
||||
|
||||
return newCombinations;
|
||||
};
|
||||
|
||||
for (let i = 0; i < matches.length; i++) {
|
||||
const match = matches[i];
|
||||
const [_, ...options] = match;
|
||||
|
||||
processed = replaceVariant(processed, _, options);
|
||||
}
|
||||
}
|
||||
|
||||
urlRegexResults.push({
|
||||
origin: potentialHostname,
|
||||
processed
|
||||
});
|
||||
}
|
||||
|
||||
for (const i of urlRegexResults) {
|
||||
for (const processed of i.processed) {
|
||||
if (
|
||||
normalizeDomain(
|
||||
processed
|
||||
.replaceAll('*', 'a')
|
||||
.replaceAll('?', 'b')
|
||||
)
|
||||
) {
|
||||
parsed.push([i.origin, processed]);
|
||||
} else if (!isProbablyIpv4(processed)) {
|
||||
parsedFailures.push([i.origin, processed]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedFailures.length > 0) {
|
||||
console.error(picocolors.bold('Parsed Failed'));
|
||||
console.table(parsedFailures);
|
||||
}
|
||||
|
||||
const hostnames = Array.from(new Set(parsed.map(i => i[1])));
|
||||
|
||||
return [
|
||||
'#!name=[Sukka] Surge Reject MITM',
|
||||
`#!desc=为 URL Regex 规则组启用 MITM (size: ${hostnames.length})`,
|
||||
'',
|
||||
'[MITM]',
|
||||
'hostname = %APPEND% ' + hostnames.join(', ')
|
||||
this.strategies = [
|
||||
new SurgeRuleSet(this.type),
|
||||
new ClashClassicRuleSet(this.type),
|
||||
new SingboxSource(this.type)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user