mirror of
https://github.com/SukkaW/Surge.git
synced 2026-02-02 20:11:54 +08:00
Refactor: adjust more output
This commit is contained in:
@@ -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}`));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user