Skip to content

Commit

Permalink
Fix AdGuardHome Output
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 14, 2024
1 parent 9c82e53 commit 7909d26
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Build/lib/rules/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
return this;
}

protected abstract preprocess(): NonNullable<TPreprocessed>;
protected abstract preprocess(): TPreprocessed extends null ? null : NonNullable<TPreprocessed>;

async done() {
await this.pendingPromise;
Expand All @@ -258,13 +258,13 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
}

private $$preprocessed: TPreprocessed | null = null;
get $preprocessed() {
get $preprocessed(): TPreprocessed extends null ? null : NonNullable<TPreprocessed> {
if (this.$$preprocessed === null) {
this.guardPendingPromise();

this.$$preprocessed = this.span.traceChildSync('preprocess', () => this.preprocess());
}
return this.$$preprocessed;
return this.$$preprocessed as any;
}

async writeClash(outputDir?: null | string) {
Expand Down
35 changes: 15 additions & 20 deletions Build/lib/rules/domainset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DomainsetOutput extends RuleOutput<string[]> {
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(this.domainKeywords);
const results: string[] = [];
Expand All @@ -29,6 +29,12 @@ export class DomainsetOutput extends RuleOutput<string[]> {
this.$clash.push(subdomain ? `+.${domain}` : domain);
(subdomain ? this.$singbox_domains : this.$singbox_domains_suffixes).push(domain);

if (subdomain) {
this.$adguardhome.push(`||${domain}^`);
} else {
this.$adguardhome.push(`|${domain}^`);
}

results.push(domain);
}, true);

Expand Down Expand Up @@ -90,8 +96,6 @@ export class DomainsetOutput extends RuleOutput<string[]> {
mitmSgmodule = undefined;

adguardhome(): string[] {
const results: string[] = [];

// const whitelistArray = sortDomains(Array.from(whitelist));
// for (let i = 0, len = whitelistArray.length; i < len; i++) {
// const domain = whitelistArray[i];
Expand All @@ -102,51 +106,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) {
const processed = wildcard.replaceAll('?', '*');
if (processed.startsWith('*.')) {
results.push(`||${processed.slice(2)}^`);
this.$adguardhome.push(`||${processed.slice(2)}^`);
} else {
results.push(`|${processed}^`);
this.$adguardhome.push(`|${processed}^`);
}
}

for (const keyword of this.domainKeywords) {
// Use regex to match keyword
results.push(`/${escapeStringRegexp(keyword)}/`);
this.$adguardhome.push(`/${escapeStringRegexp(keyword)}/`);
}

for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/32')) {
results.push(`||${ipcidr.slice(0, -3)}`);
this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
/* else if (ipcidr.endsWith('.0/24')) {
results.push(`||${ipcidr.slice(0, -6)}.*`);
} */
} else {
results.push(`||${ipcidr}^`);
this.$adguardhome.push(`||${ipcidr}^`);
}
}
}
for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/128')) {
results.push(`||${ipcidr.slice(0, -4)}`);
this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
} else {
results.push(`||${ipcidr}`);
this.$adguardhome.push(`||${ipcidr}`);
}
}
}

return results;
return this.$adguardhome;
}
}

0 comments on commit 7909d26

Please sign in to comment.