mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-20 05:10:29 +08:00
Chore: update clash parse handling
This commit is contained in:
parent
634cce0801
commit
536046b429
@ -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[]) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user