Refactor: adjust more output

This commit is contained in:
SukkaW
2024-09-21 19:04:11 +08:00
parent 7c372b3b8c
commit eeeadbc86b
10 changed files with 131 additions and 133 deletions

View File

@@ -1,11 +1,10 @@
import picocolors from 'picocolors';
import { domainWildCardToRegex, identity } from './misc';
import { isProbablyIpv4, isProbablyIpv6 } from './is-fast-ip';
const unsupported = Symbol('unsupported');
// https://dreamacro.github.io/clash/configuration/rules.html
const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => string) | typeof unsupported> = {
export const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => string) | typeof unsupported> = {
DOMAIN: identity,
'DOMAIN-SUFFIX': identity,
'DOMAIN-KEYWORD': identity,
@@ -35,35 +34,3 @@ const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => s
'URL-REGEX': unsupported,
'USER-AGENT': unsupported
};
export const surgeRulesetToClashClassicalTextRuleset = (rules: string[] | Set<string>) => {
return Array.from(rules).reduce<string[]>((acc, cur) => {
let buf = '';
let type = '';
let i = 0;
for (const len = cur.length; i < len; i++) {
if (cur[i] === ',') {
type = buf;
break;
}
buf += cur[i];
}
if (type === '') {
return acc;
}
const value = cur.slice(i + 1);
if (type in PROCESSOR) {
const proc = PROCESSOR[type];
if (proc !== unsupported) {
acc.push(proc(cur, type, value));
}
} else {
console.log(picocolors.yellow(`[clash] unknown rule type: ${type}`), cur);
}
return acc;
}, []);
};
export const surgeDomainsetToClashRuleset = (domainset: string[]) => {
return domainset.map(i => (i[0] === '.' ? `DOMAIN-SUFFIX,${i.slice(1)}` : `DOMAIN,${i}`));
};

View File

@@ -218,8 +218,12 @@ export abstract class RuleOutput {
abstract clash(): string[];
abstract singbox(): string[];
done() {
return this.pendingPromise;
}
async write(): Promise<void> {
await this.pendingPromise;
await this.done();
invariant(this.title, 'Missing title');
invariant(this.description, 'Missing description');

View File

@@ -76,15 +76,15 @@ export class DomainsetOutput extends RuleOutput {
invariant(this.apexDomainMap, 'Missing apex domain map');
return Array.from(
(
nullthrow(this.sorted, 'Non dumped yet').reduce<Map<string, number>>((acc, cur) => {
nullthrow(this.sorted, 'Non dumped yet')
.reduce<Map<string, number>>((acc, cur) => {
const suffix = this.apexDomainMap!.get(cur);
if (suffix) {
acc.set(suffix, (acc.get(suffix) ?? 0) + 1);
}
return acc;
}, new Map())
).entries()
.entries()
)
.filter(a => a[1] > 9)
.sort(

View File

@@ -12,7 +12,7 @@ export class RulesetOutput extends RuleOutput {
}
private $computed: [domain: string[], domainSuffix: string[], sortedDomainRules: string[]] | null = null;
private get computed() {
private computed() {
if (!this.$computed) {
const kwfilter = createKeywordFilter(this.domainKeywords);
@@ -40,7 +40,7 @@ export class RulesetOutput extends RuleOutput {
surge(): string[] {
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
appendArrayInPlace(results, this.computed[2]);
appendArrayInPlace(results, this.computed()[2]);
appendArrayFromSet(results, this.domainKeywords, i => `DOMAIN-KEYWORD,${i}`);
appendArrayFromSet(results, this.domainWildcard, i => `DOMAIN-WILDCARD,${i}`);
@@ -70,7 +70,7 @@ export class RulesetOutput extends RuleOutput {
clash(): string[] {
const results: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
appendArrayInPlace(results, this.computed[2]);
appendArrayInPlace(results, this.computed()[2]);
appendArrayFromSet(results, this.domainKeywords, i => `DOMAIN-KEYWORD,${i}`);
appendArrayFromSet(results, this.domainWildcard, i => `DOMAIN-REGEX,${RuleOutput.domainWildCardToRegex(i)}`);
@@ -97,8 +97,8 @@ export class RulesetOutput extends RuleOutput {
const singbox: SingboxSourceFormat = {
version: 2,
rules: [{
domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'].concat(this.computed[0]),
domain_suffix: this.computed[1],
domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'].concat(this.computed()[0]),
domain_suffix: this.computed()[1],
domain_keyword: Array.from(this.domainKeywords),
domain_regex: Array.from(this.domainWildcard).map(RuleOutput.domainWildCardToRegex),
ip_cidr: appendArrayFromSet([], [this.ipcidr, this.ipcidrNoResolve, this.ipcidr6, this.ipcidr6NoResolve]),