Skip to content

Commit e25bf90

Browse files
committed
Fix AdGuardHome & Clash Output
1 parent 9c82e53 commit e25bf90

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

Build/lib/rules/base.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
242242
return this;
243243
}
244244

245-
protected abstract preprocess(): NonNullable<TPreprocessed>;
245+
protected abstract preprocess(): TPreprocessed extends null ? null : NonNullable<TPreprocessed>;
246246

247247
async done() {
248248
await this.pendingPromise;
@@ -258,13 +258,17 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
258258
}
259259

260260
private $$preprocessed: TPreprocessed | null = null;
261-
get $preprocessed() {
261+
protected runPreprocess() {
262262
if (this.$$preprocessed === null) {
263263
this.guardPendingPromise();
264264

265265
this.$$preprocessed = this.span.traceChildSync('preprocess', () => this.preprocess());
266266
}
267-
return this.$$preprocessed;
267+
}
268+
269+
get $preprocessed(): TPreprocessed extends null ? null : NonNullable<TPreprocessed> {
270+
this.runPreprocess();
271+
return this.$$preprocessed as any;
268272
}
269273

270274
async writeClash(outputDir?: null | string) {

Build/lib/rules/domainset.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ export class DomainsetOutput extends RuleOutput<string[]> {
1515
private $clash: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
1616
private $singbox_domains: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
1717
private $singbox_domains_suffixes: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
18-
18+
private $adguardhome: string[] = [];
1919
preprocess() {
2020
const kwfilter = createKeywordFilter(this.domainKeywords);
21-
const results: string[] = [];
2221

2322
this.domainTrie.dumpWithoutDot((domain, subdomain) => {
2423
if (kwfilter(domain)) {
@@ -29,21 +28,29 @@ export class DomainsetOutput extends RuleOutput<string[]> {
2928
this.$clash.push(subdomain ? `+.${domain}` : domain);
3029
(subdomain ? this.$singbox_domains : this.$singbox_domains_suffixes).push(domain);
3130

32-
results.push(domain);
31+
if (subdomain) {
32+
this.$adguardhome.push(`||${domain}^`);
33+
} else {
34+
this.$adguardhome.push(`|${domain}^`);
35+
}
3336
}, true);
3437

35-
return results;
38+
return this.$surge;
3639
}
3740

3841
surge(): string[] {
42+
this.runPreprocess();
3943
return this.$surge;
4044
}
4145

4246
clash(): string[] {
47+
this.runPreprocess();
4348
return this.$clash;
4449
}
4550

4651
singbox(): string[] {
52+
this.runPreprocess();
53+
4754
return RuleOutput.jsonToLines({
4855
version: 2,
4956
rules: [{
@@ -55,6 +62,8 @@ export class DomainsetOutput extends RuleOutput<string[]> {
5562

5663
protected apexDomainMap: Map<string, string> | null = null;
5764
getStatMap() {
65+
this.runPreprocess();
66+
5867
invariant(this.$preprocessed, 'Non dumped yet');
5968

6069
if (!this.apexDomainMap) {
@@ -90,7 +99,7 @@ export class DomainsetOutput extends RuleOutput<string[]> {
9099
mitmSgmodule = undefined;
91100

92101
adguardhome(): string[] {
93-
const results: string[] = [];
102+
this.runPreprocess();
94103

95104
// const whitelistArray = sortDomains(Array.from(whitelist));
96105
// for (let i = 0, len = whitelistArray.length; i < len; i++) {
@@ -102,51 +111,42 @@ export class DomainsetOutput extends RuleOutput<string[]> {
102111
// }
103112
// }
104113

105-
for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
106-
const domain = this.$preprocessed[i];
107-
if (domain[0] === '.') {
108-
results.push(`||${domain.slice(1)}^`);
109-
} else {
110-
results.push(`|${domain}^`);
111-
}
112-
}
113-
114114
for (const wildcard of this.domainWildcard) {
115115
const processed = wildcard.replaceAll('?', '*');
116116
if (processed.startsWith('*.')) {
117-
results.push(`||${processed.slice(2)}^`);
117+
this.$adguardhome.push(`||${processed.slice(2)}^`);
118118
} else {
119-
results.push(`|${processed}^`);
119+
this.$adguardhome.push(`|${processed}^`);
120120
}
121121
}
122122

123123
for (const keyword of this.domainKeywords) {
124124
// Use regex to match keyword
125-
results.push(`/${escapeStringRegexp(keyword)}/`);
125+
this.$adguardhome.push(`/${escapeStringRegexp(keyword)}/`);
126126
}
127127

128128
for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
129129
for (const ipcidr of ipGroup) {
130130
if (ipcidr.endsWith('/32')) {
131-
results.push(`||${ipcidr.slice(0, -3)}`);
131+
this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
132132
/* else if (ipcidr.endsWith('.0/24')) {
133133
results.push(`||${ipcidr.slice(0, -6)}.*`);
134134
} */
135135
} else {
136-
results.push(`||${ipcidr}^`);
136+
this.$adguardhome.push(`||${ipcidr}^`);
137137
}
138138
}
139139
}
140140
for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
141141
for (const ipcidr of ipGroup) {
142142
if (ipcidr.endsWith('/128')) {
143-
results.push(`||${ipcidr.slice(0, -4)}`);
143+
this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
144144
} else {
145-
results.push(`||${ipcidr}`);
145+
this.$adguardhome.push(`||${ipcidr}`);
146146
}
147147
}
148148
}
149149

150-
return results;
150+
return this.$adguardhome;
151151
}
152152
}

0 commit comments

Comments
 (0)