Feat: Surge DOMAIN-WILDCARD -> Clash DOMAIN-REGEX

This commit is contained in:
SukkaW
2024-08-13 02:01:15 +08:00
parent 5bceb6cac7
commit f35958208f
6 changed files with 35 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import picocolors from 'picocolors';
import { domainWildCardToRegex } from './misc';
const identity = <T>(x: T): T => x;
const unsupported = Symbol('unsupported');
@@ -8,6 +9,7 @@ const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => s
DOMAIN: identity,
'DOMAIN-SUFFIX': identity,
'DOMAIN-KEYWORD': identity,
'DOMAIN-WILDCARD': (_raw, _type, value) => `DOMAIN-REGEX,${domainWildCardToRegex(value)}`,
GEOIP: identity,
'IP-CIDR': identity,
'IP-CIDR6': identity,
@@ -17,8 +19,8 @@ const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => s
'DST-PORT': identity,
'PROCESS-NAME': identity,
'PROCESS-PATH': identity,
'DEST-PORT': (_raw, type, value) => `DST-PORT,${value}`,
'IN-PORT': (_raw, type, value) => `SRC-PORT,${value}`,
'DEST-PORT': (_raw, _type, value) => `DST-PORT,${value}`,
'IN-PORT': (_raw, _type, value) => `SRC-PORT,${value}`,
'URL-REGEX': unsupported,
'USER-AGENT': unsupported
};

View File

@@ -110,6 +110,7 @@ const sortTypeOrder: Record<string | typeof defaultSortTypeOrder, number> = {
'DOMAIN-KEYWORD': 10,
// experimental domain wildcard support
'DOMAIN-WILDCARD': 20,
'DOMAIN-REGEX': 21,
'USER-AGENT': 30,
'PROCESS-NAME': 40,
[defaultSortTypeOrder]: 50, // default sort order for unknown type

View File

@@ -1,6 +1,7 @@
import { dirname } from 'path';
import fs from 'fs';
import fsp from 'fs/promises';
import { makeRe } from 'picomatch';
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i;
@@ -28,3 +29,7 @@ export const writeFile: Write = async (destination: string, input, dir = dirname
}
return fsp.writeFile(destination, input, { encoding: 'utf-8' });
};
export const domainWildCardToRegex = (domain: string) => {
return makeRe(domain, { contains: false, strictSlashes: true }).source;
};

View File

@@ -1,4 +1,5 @@
import picocolors from 'picocolors';
import { domainWildCardToRegex } from './misc';
const unsupported = Symbol('unsupported');
@@ -7,6 +8,7 @@ const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => [
DOMAIN: (_1, _2, value) => ['domain', value],
'DOMAIN-SUFFIX': (_1, _2, value) => ['domain_suffix', value],
'DOMAIN-KEYWORD': (_1, _2, value) => ['domain_keyword', value],
'DOMAIN-WILDCARD': (_1, _2, value) => ['domain_regex', domainWildCardToRegex(value)],
GEOIP: unsupported,
'IP-CIDR': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],
'IP-CIDR6': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],