Fix AdGuardHome & Clash Output

This commit is contained in:
SukkaW 2024-12-14 21:20:32 +08:00
parent 9c82e5346c
commit 716c23b44b
2 changed files with 30 additions and 26 deletions

View File

@ -242,7 +242,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
return this; return this;
} }
protected abstract preprocess(): NonNullable<TPreprocessed>; protected abstract preprocess(): TPreprocessed extends null ? null : NonNullable<TPreprocessed>;
async done() { async done() {
await this.pendingPromise; await this.pendingPromise;
@ -258,13 +258,17 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
} }
private $$preprocessed: TPreprocessed | null = null; private $$preprocessed: TPreprocessed | null = null;
get $preprocessed() { protected runPreprocess() {
if (this.$$preprocessed === null) { if (this.$$preprocessed === null) {
this.guardPendingPromise(); this.guardPendingPromise();
this.$$preprocessed = this.span.traceChildSync('preprocess', () => this.preprocess()); this.$$preprocessed = this.span.traceChildSync('preprocess', () => this.preprocess());
} }
return this.$$preprocessed; }
get $preprocessed(): TPreprocessed extends null ? null : NonNullable<TPreprocessed> {
this.runPreprocess();
return this.$$preprocessed as any;
} }
async writeClash(outputDir?: null | string) { async writeClash(outputDir?: null | string) {

View File

@ -15,10 +15,9 @@ export class DomainsetOutput extends RuleOutput<string[]> {
private $clash: 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: 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 $singbox_domains_suffixes: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
private $adguardhome: string[] = [];
preprocess() { preprocess() {
const kwfilter = createKeywordFilter(this.domainKeywords); const kwfilter = createKeywordFilter(this.domainKeywords);
const results: string[] = [];
this.domainTrie.dumpWithoutDot((domain, subdomain) => { this.domainTrie.dumpWithoutDot((domain, subdomain) => {
if (kwfilter(domain)) { if (kwfilter(domain)) {
@ -27,23 +26,31 @@ export class DomainsetOutput extends RuleOutput<string[]> {
this.$surge.push(subdomain ? '.' + domain : domain); this.$surge.push(subdomain ? '.' + domain : domain);
this.$clash.push(subdomain ? `+.${domain}` : domain); this.$clash.push(subdomain ? `+.${domain}` : domain);
(subdomain ? this.$singbox_domains : this.$singbox_domains_suffixes).push(domain); (subdomain ? this.$singbox_domains_suffixes : this.$singbox_domains).push(domain);
results.push(domain); if (subdomain) {
this.$adguardhome.push(`||${domain}^`);
} else {
this.$adguardhome.push(`|${domain}^`);
}
}, true); }, true);
return results; return this.$surge;
} }
surge(): string[] { surge(): string[] {
this.runPreprocess();
return this.$surge; return this.$surge;
} }
clash(): string[] { clash(): string[] {
this.runPreprocess();
return this.$clash; return this.$clash;
} }
singbox(): string[] { singbox(): string[] {
this.runPreprocess();
return RuleOutput.jsonToLines({ return RuleOutput.jsonToLines({
version: 2, version: 2,
rules: [{ rules: [{
@ -55,6 +62,8 @@ export class DomainsetOutput extends RuleOutput<string[]> {
protected apexDomainMap: Map<string, string> | null = null; protected apexDomainMap: Map<string, string> | null = null;
getStatMap() { getStatMap() {
this.runPreprocess();
invariant(this.$preprocessed, 'Non dumped yet'); invariant(this.$preprocessed, 'Non dumped yet');
if (!this.apexDomainMap) { if (!this.apexDomainMap) {
@ -90,7 +99,7 @@ export class DomainsetOutput extends RuleOutput<string[]> {
mitmSgmodule = undefined; mitmSgmodule = undefined;
adguardhome(): string[] { adguardhome(): string[] {
const results: string[] = []; this.runPreprocess();
// const whitelistArray = sortDomains(Array.from(whitelist)); // const whitelistArray = sortDomains(Array.from(whitelist));
// for (let i = 0, len = whitelistArray.length; i < len; i++) { // for (let i = 0, len = whitelistArray.length; i < len; i++) {
@ -102,51 +111,42 @@ export class DomainsetOutput extends RuleOutput<string[]> {
// } // }
// } // }
for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
const domain = this.$preprocessed[i];
if (domain[0] === '.') {
results.push(`||${domain.slice(1)}^`);
} else {
results.push(`|${domain}^`);
}
}
for (const wildcard of this.domainWildcard) { for (const wildcard of this.domainWildcard) {
const processed = wildcard.replaceAll('?', '*'); const processed = wildcard.replaceAll('?', '*');
if (processed.startsWith('*.')) { if (processed.startsWith('*.')) {
results.push(`||${processed.slice(2)}^`); this.$adguardhome.push(`||${processed.slice(2)}^`);
} else { } else {
results.push(`|${processed}^`); this.$adguardhome.push(`|${processed}^`);
} }
} }
for (const keyword of this.domainKeywords) { for (const keyword of this.domainKeywords) {
// Use regex to match keyword // Use regex to match keyword
results.push(`/${escapeStringRegexp(keyword)}/`); this.$adguardhome.push(`/${escapeStringRegexp(keyword)}/`);
} }
for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) { for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
for (const ipcidr of ipGroup) { for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/32')) { if (ipcidr.endsWith('/32')) {
results.push(`||${ipcidr.slice(0, -3)}`); this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
/* else if (ipcidr.endsWith('.0/24')) { /* else if (ipcidr.endsWith('.0/24')) {
results.push(`||${ipcidr.slice(0, -6)}.*`); results.push(`||${ipcidr.slice(0, -6)}.*`);
} */ } */
} else { } else {
results.push(`||${ipcidr}^`); this.$adguardhome.push(`||${ipcidr}^`);
} }
} }
} }
for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) { for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
for (const ipcidr of ipGroup) { for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/128')) { if (ipcidr.endsWith('/128')) {
results.push(`||${ipcidr.slice(0, -4)}`); this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
} else { } else {
results.push(`||${ipcidr}`); this.$adguardhome.push(`||${ipcidr}`);
} }
} }
} }
return results; return this.$adguardhome;
} }
} }