Chore: update clash parse handling

This commit is contained in:
SukkaW 2024-06-13 22:22:12 +08:00
parent 634cce0801
commit 536046b429

View File

@ -1,33 +1,43 @@
// @ts-check const identity = <T>(x: T): T => x;
import Trie from 'mnemonist/trie';
// https://dreamacro.github.io/clash/configuration/rules.html // https://dreamacro.github.io/clash/configuration/rules.html
const CLASH_SUPPORTED_RULE_TYPE = [ const PROCESSOR: Record<string, (raw: string, type: string, value: string) => string> = {
'DOMAIN', DOMAIN: identity,
'DOMAIN-SUFFIX', 'DOMAIN-SUFFIX': identity,
'DOMAIN-KEYWORD', 'DOMAIN-KEYWORD': identity,
'GEOIP', GEOIP: identity,
'IP-CIDR', 'IP-CIDR': identity,
'IP-CIDR6', 'IP-CIDR6': identity,
'SRC-IP-CIDR', 'SRC-IP-CIDR': identity,
'SRC-PORT', 'SRC-PORT': identity,
'DST-PORT', 'DST-PORT': identity,
'PROCESS-NAME', 'PROCESS-NAME': identity,
'PROCESS-PATH' 'PROCESS-PATH': identity,
]; 'DEST-PORT': (_raw, type, value) => `DST-PORT,${value}`,
'IN-PORT': (_raw, type, value) => `SRC-PORT,${value}`
const REQUIRE_REWRITE = { };
'DEST-PORT': 'DST-PORT',
'IN-PORT': 'SRC-PORT'
} as const;
export const surgeRulesetToClashClassicalTextRuleset = (rules: string[] | Set<string>) => { export const surgeRulesetToClashClassicalTextRuleset = (rules: string[] | Set<string>) => {
const trie = Trie.from(rules); return Array.from(rules).reduce<string[]>((acc, cur) => {
let buf = '';
return CLASH_SUPPORTED_RULE_TYPE.flatMap(type => trie.find(`${type},`)).concat( let type = '';
Object.keys(REQUIRE_REWRITE).flatMap((type) => trie.find(`${type},`) let i = 0;
.map(line => `${REQUIRE_REWRITE[type as keyof typeof REQUIRE_REWRITE]}${line.slice(type.length)}`)) 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) {
acc.push(PROCESSOR[type](cur, type, value));
}
return acc;
}, []);
}; };
export const surgeDomainsetToClashDomainset = (domainset: string[]) => { export const surgeDomainsetToClashDomainset = (domainset: string[]) => {