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

View File

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

View File

@ -1,6 +1,7 @@
import { dirname } from 'path'; import { dirname } from 'path';
import fs from 'fs'; import fs from 'fs';
import fsp from 'fs/promises'; import fsp from 'fs/promises';
import { makeRe } from 'picomatch';
export const isTruthy = <T>(i: T | 0 | '' | false | null | undefined): i is T => !!i; 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' }); 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 picocolors from 'picocolors';
import { domainWildCardToRegex } from './misc';
const unsupported = Symbol('unsupported'); 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: (_1, _2, value) => ['domain', value],
'DOMAIN-SUFFIX': (_1, _2, value) => ['domain_suffix', value], 'DOMAIN-SUFFIX': (_1, _2, value) => ['domain_suffix', value],
'DOMAIN-KEYWORD': (_1, _2, value) => ['domain_keyword', value], 'DOMAIN-KEYWORD': (_1, _2, value) => ['domain_keyword', value],
'DOMAIN-WILDCARD': (_1, _2, value) => ['domain_regex', domainWildCardToRegex(value)],
GEOIP: unsupported, GEOIP: unsupported,
'IP-CIDR': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value], '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], 'IP-CIDR6': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],

View File

@ -34,6 +34,7 @@
"mnemonist": "^0.39.8", "mnemonist": "^0.39.8",
"path-scurry": "^1.11.1", "path-scurry": "^1.11.1",
"picocolors": "^1.0.1", "picocolors": "^1.0.1",
"picomatch": "^4.0.2",
"punycode": "^2.3.1", "punycode": "^2.3.1",
"table": "^6.8.2", "table": "^6.8.2",
"tar-stream": "^3.1.7", "tar-stream": "^3.1.7",
@ -49,6 +50,7 @@
"@types/better-sqlite3": "^7.6.11", "@types/better-sqlite3": "^7.6.11",
"@types/chai": "^4.3.17", "@types/chai": "^4.3.17",
"@types/mocha": "^10.0.7", "@types/mocha": "^10.0.7",
"@types/picomatch": "^3.0.1",
"@types/punycode": "^2.1.4", "@types/punycode": "^2.1.4",
"@types/tar-stream": "^3.1.3", "@types/tar-stream": "^3.1.3",
"chai": "4", "chai": "4",

23
pnpm-lock.yaml generated
View File

@ -37,7 +37,7 @@ importers:
version: 0.2.5 version: 0.2.5
fdir: fdir:
specifier: ^6.2.0 specifier: ^6.2.0
version: 6.2.0 version: 6.2.0(picomatch@4.0.2)
foxact: foxact:
specifier: ^0.2.36 specifier: ^0.2.36
version: 0.2.36 version: 0.2.36
@ -53,6 +53,9 @@ importers:
picocolors: picocolors:
specifier: ^1.0.1 specifier: ^1.0.1
version: 1.0.1 version: 1.0.1
picomatch:
specifier: ^4.0.2
version: 4.0.2
punycode: punycode:
specifier: ^2.3.1 specifier: ^2.3.1
version: 2.3.1 version: 2.3.1
@ -93,6 +96,9 @@ importers:
'@types/mocha': '@types/mocha':
specifier: ^10.0.7 specifier: ^10.0.7
version: 10.0.7 version: 10.0.7
'@types/picomatch':
specifier: ^3.0.1
version: 3.0.1
'@types/punycode': '@types/punycode':
specifier: ^2.1.4 specifier: ^2.1.4
version: 2.1.4 version: 2.1.4
@ -436,6 +442,9 @@ packages:
'@types/node@20.14.11': '@types/node@20.14.11':
resolution: {integrity: sha512-kprQpL8MMeszbz6ojB5/tU8PLN4kesnN8Gjzw349rDlNgsSzg90lAVj3llK99Dh7JON+t9AuscPPFW6mPbTnSA==} resolution: {integrity: sha512-kprQpL8MMeszbz6ojB5/tU8PLN4kesnN8Gjzw349rDlNgsSzg90lAVj3llK99Dh7JON+t9AuscPPFW6mPbTnSA==}
'@types/picomatch@3.0.1':
resolution: {integrity: sha512-1MRgzpzY0hOp9pW/kLRxeQhUWwil6gnrUYd3oEpeYBqp/FexhaCPv3F8LsYr47gtUU45fO2cm1dbwkSrHEo8Uw==}
'@types/punycode@2.1.4': '@types/punycode@2.1.4':
resolution: {integrity: sha512-trzh6NzBnq8yw5e35f8xe8VTYjqM3NE7bohBtvDVf/dtUer3zYTLK1Ka3DG3p7bdtoaOHZucma6FfVKlQ134pQ==} resolution: {integrity: sha512-trzh6NzBnq8yw5e35f8xe8VTYjqM3NE7bohBtvDVf/dtUer3zYTLK1Ka3DG3p7bdtoaOHZucma6FfVKlQ134pQ==}
@ -1259,6 +1268,10 @@ packages:
resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==}
engines: {node: '>=8.6'} engines: {node: '>=8.6'}
picomatch@4.0.2:
resolution: {integrity: sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==}
engines: {node: '>=12'}
pirates@4.0.6: pirates@4.0.6:
resolution: {integrity: sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==} resolution: {integrity: sha512-saLsH7WeYYPiD25LDuLRRY/i+6HaPYr6G1OUlN39otzkSTxKnubR9RTxS3/Kk50s1g2JTgFwWQDQyplC5/SHZg==}
engines: {node: '>= 6'} engines: {node: '>= 6'}
@ -1881,6 +1894,8 @@ snapshots:
dependencies: dependencies:
undici-types: 5.26.5 undici-types: 5.26.5
'@types/picomatch@3.0.1': {}
'@types/punycode@2.1.4': {} '@types/punycode@2.1.4': {}
'@types/retry@0.12.5': {} '@types/retry@0.12.5': {}
@ -2475,7 +2490,9 @@ snapshots:
dependencies: dependencies:
reusify: 1.0.4 reusify: 1.0.4
fdir@6.2.0: {} fdir@6.2.0(picomatch@4.0.2):
optionalDependencies:
picomatch: 4.0.2
file-entry-cache@8.0.0: file-entry-cache@8.0.0:
dependencies: dependencies:
@ -2793,6 +2810,8 @@ snapshots:
picomatch@2.3.1: {} picomatch@2.3.1: {}
picomatch@4.0.2: {}
pirates@4.0.6: {} pirates@4.0.6: {}
prebuild-install@7.1.2: prebuild-install@7.1.2: