Refactor: new write strategy (#58)

This commit is contained in:
Sukka
2025-01-29 03:58:49 +08:00
committed by GitHub
parent 6606d5cb01
commit b22079f5eb
17 changed files with 1096 additions and 673 deletions

View File

@@ -0,0 +1,107 @@
import { escapeStringRegexp } from 'foxts/escape-string-regexp';
import { BaseWriteStrategy } from './base';
import { noop } from 'foxts/noop';
import { notSupported } from '../misc';
export class AdGuardHome extends BaseWriteStrategy {
// readonly type = 'domainset';
readonly fileExtension = 'txt';
readonly type = '';
protected result: string[] = [];
// eslint-disable-next-line @typescript-eslint/class-methods-use-this -- abstract method
withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[] {
return [
`! Title: ${title}`,
'! Last modified: ' + date.toUTCString(),
'! Expires: 6 hours',
'! License: https://github.com/SukkaW/Surge/blob/master/LICENSE',
'! Homepage: https://github.com/SukkaW/Surge',
`! Description: ${description.join(' ')}`,
'!',
...content,
'! EOF'
];
}
writeDomain(domain: string): void {
this.result.push(`|${domain}^`);
}
// const whitelistArray = sortDomains(Array.from(whitelist));
// for (let i = 0, len = whitelistArray.length; i < len; i++) {
// const domain = whitelistArray[i];
// if (domain[0] === '.') {
// results.push(`@@||${domain.slice(1)}^`);
// } else {
// results.push(`@@|${domain}^`);
// }
// }
writeDomainSuffix(domain: string): void {
this.result.push(`||${domain}^`);
}
writeDomainKeywords(keywords: Set<string>): void {
for (const keyword of keywords) {
// Use regex to match keyword
this.result.push(`/${escapeStringRegexp(keyword)}/`);
}
}
writeDomainWildcards(wildcards: Set<string>): void {
for (const wildcard of wildcards) {
const processed = wildcard.replaceAll('?', '*');
if (processed.startsWith('*.')) {
this.result.push(`||${processed.slice(2)}^`);
} else {
this.result.push(`|${processed}^`);
}
}
}
writeUserAgents = noop;
writeProcessNames = noop;
writeProcessPaths = noop;
writeUrlRegexes = noop;
writeIpCidrs(ipGroup: string[], noResolve: boolean): void {
if (noResolve) {
// When IP is provided to AdGuardHome, any domain resolve to those IP will be blocked
// So we can't do noResolve
return;
}
for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/32')) {
this.result.push(`||${ipcidr.slice(0, -3)}`);
/* else if (ipcidr.endsWith('.0/24')) {
results.push(`||${ipcidr.slice(0, -6)}.*`);
} */
} else {
this.result.push(`||${ipcidr}^`);
}
}
}
writeIpCidr6s(ipGroup: string[], noResolve: boolean): void {
if (noResolve) {
// When IP is provided to AdGuardHome, any domain resolve to those IP will be blocked
// So we can't do noResolve
return;
}
for (const ipcidr of ipGroup) {
if (ipcidr.endsWith('/128')) {
this.result.push(`||${ipcidr.slice(0, -4)}`);
} else {
this.result.push(`||${ipcidr}`);
}
}
};
writeGeoip = notSupported('writeGeoip');
writeIpAsns = notSupported('writeIpAsns');
writeSourceIpCidrs = notSupported('writeSourceIpCidrs');
writeSourcePorts = notSupported('writeSourcePorts');
writeDestinationPorts = noop;
writeOtherRules = noop;
}

View File

@@ -0,0 +1,81 @@
import path from 'node:path';
import type { Span } from '../../trace';
import { compareAndWriteFile } from '../create-file';
export abstract class BaseWriteStrategy {
// abstract readonly type: 'domainset' | 'non_ip' | 'ip' | (string & {});
public overwriteFilename: string | null = null;
public abstract readonly type: 'domainset' | 'non_ip' | 'ip' | (string & {});
abstract readonly fileExtension: 'conf' | 'txt' | 'json' | (string & {});
constructor(protected outputDir: string) {}
protected abstract result: string[] | null;
abstract writeDomain(domain: string): void;
abstract writeDomainSuffix(domain: string): void;
abstract writeDomainKeywords(keyword: Set<string>): void;
abstract writeDomainWildcards(wildcard: Set<string>): void;
abstract writeUserAgents(userAgent: Set<string>): void;
abstract writeProcessNames(processName: Set<string>): void;
abstract writeProcessPaths(processPath: Set<string>): void;
abstract writeUrlRegexes(urlRegex: Set<string>): void;
abstract writeIpCidrs(ipCidr: string[], noResolve: boolean): void;
abstract writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void;
abstract writeGeoip(geoip: Set<string>, noResolve: boolean): void;
abstract writeIpAsns(asns: Set<string>, noResolve: boolean): void;
abstract writeSourceIpCidrs(sourceIpCidr: string[]): void;
abstract writeSourcePorts(port: Set<string>): void;
abstract writeDestinationPorts(port: Set<string>): void;
abstract writeOtherRules(rule: string[]): void;
static readonly domainWildCardToRegex = (domain: string) => {
let result = '^';
for (let i = 0, len = domain.length; i < len; i++) {
switch (domain[i]) {
case '.':
result += String.raw`\.`;
break;
case '*':
result += String.raw`[\w.-]*?`;
break;
case '?':
result += String.raw`[\w.-]`;
break;
default:
result += domain[i];
}
}
result += '$';
return result;
};
abstract withPadding(title: string, description: string[] | readonly string[], date: Date, content: string[]): string[];
output(
span: Span,
title: string,
description: string[] | readonly string[],
date: Date,
relativePath: string
): void | Promise<void> {
if (!this.result) {
return;
}
return compareAndWriteFile(
span,
this.withPadding(
title,
description,
date,
this.result
),
path.join(this.outputDir, relativePath)
);
};
get content() {
return this.result;
}
}

View File

@@ -0,0 +1,169 @@
import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
import { BaseWriteStrategy } from './base';
import { noop } from 'foxts/noop';
import { fastIpVersion, notSupported, withBannerArray } from '../misc';
import { OUTPUT_CLASH_DIR } from '../../constants/dir';
import { appendArrayInPlace } from '../append-array-in-place';
export class ClashDomainSet extends BaseWriteStrategy {
// readonly type = 'domainset';
readonly fileExtension = 'txt';
readonly type = 'domainset';
protected result: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
constructor(protected outputDir = OUTPUT_CLASH_DIR) {
super(outputDir);
}
withPadding = withBannerArray;
writeDomain(domain: string): void {
this.result.push(domain);
}
writeDomainSuffix(domain: string): void {
this.result.push('+.' + domain);
}
writeDomainKeywords = noop;
writeDomainWildcards = noop;
writeUserAgents = noop;
writeProcessNames = noop;
writeProcessPaths = noop;
writeUrlRegexes = noop;
writeIpCidrs = noop;
writeIpCidr6s = noop;
writeGeoip = noop;
writeIpAsns = noop;
writeSourceIpCidrs = noop;
writeSourcePorts = noop;
writeDestinationPorts = noop;
writeOtherRules = noop;
}
export class ClashIPSet extends BaseWriteStrategy {
// readonly type = 'domainset';
readonly fileExtension = 'txt';
readonly type = 'ip';
protected result: string[] = [];
constructor(protected outputDir = OUTPUT_CLASH_DIR) {
super(outputDir);
}
withPadding = withBannerArray;
writeDomain = notSupported('writeDomain');
writeDomainSuffix = notSupported('writeDomainSuffix');
writeDomainKeywords = notSupported('writeDomainKeywords');
writeDomainWildcards = notSupported('writeDomainWildcards');
writeUserAgents = notSupported('writeUserAgents');
writeProcessNames = notSupported('writeProcessNames');
writeProcessPaths = notSupported('writeProcessPaths');
writeUrlRegexes = notSupported('writeUrlRegexes');
writeIpCidrs(ipCidr: string[]): void {
appendArrayInPlace(this.result, ipCidr);
}
writeIpCidr6s(ipCidr6: string[]): void {
appendArrayInPlace(this.result, ipCidr6);
}
writeGeoip = notSupported('writeGeoip');
writeIpAsns = notSupported('writeIpAsns');
writeSourceIpCidrs = notSupported('writeSourceIpCidrs');
writeSourcePorts = notSupported('writeSourcePorts');
writeDestinationPorts = noop;
writeOtherRules = noop;
}
export class ClashClassicRuleSet extends BaseWriteStrategy {
readonly fileExtension = 'txt';
protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
constructor(public readonly type: string, protected outputDir = OUTPUT_CLASH_DIR) {
super(outputDir);
}
withPadding = withBannerArray;
writeDomain(domain: string): void {
this.result.push('DOMAIN,' + domain);
}
writeDomainSuffix(domain: string): void {
this.result.push('DOMAIN-SUFFIX,' + domain);
}
writeDomainKeywords(keyword: Set<string>): void {
appendSetElementsToArray(this.result, keyword, i => `DOMAIN-KEYWORD,${i}`);
}
writeDomainWildcards(wildcard: Set<string>): void {
appendSetElementsToArray(this.result, wildcard, i => `DOMAIN-REGEX,${ClashClassicRuleSet.domainWildCardToRegex(i)}`);
}
writeUserAgents = noop;
writeProcessNames(processName: Set<string>): void {
appendSetElementsToArray(this.result, processName, i => `PROCESS-NAME,${i}`);
}
writeProcessPaths(processPath: Set<string>): void {
appendSetElementsToArray(this.result, processPath, i => `PROCESS-PATH,${i}`);
}
writeUrlRegexes = noop;
writeIpCidrs(ipCidr: string[], noResolve: boolean): void {
for (let i = 0, len = ipCidr.length; i < len; i++) {
this.result.push(`IP-CIDR,${ipCidr[i]}${noResolve ? ',no-resolve' : ''}`);
}
}
writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void {
for (let i = 0, len = ipCidr6.length; i < len; i++) {
this.result.push(`IP-CIDR6,${ipCidr6[i]}${noResolve ? ',no-resolve' : ''}`);
}
}
writeGeoip(geoip: Set<string>, noResolve: boolean): void {
appendSetElementsToArray(this.result, geoip, i => `GEOIP,${i}${noResolve ? ',no-resolve' : ''}`);
}
writeIpAsns(asns: Set<string>, noResolve: boolean): void {
appendSetElementsToArray(this.result, asns, i => `IP-ASN,${i}${noResolve ? ',no-resolve' : ''}`);
}
writeSourceIpCidrs(sourceIpCidr: string[]): void {
for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
const value = sourceIpCidr[i];
if (value.includes('/')) {
this.result.push(`SRC-IP-CIDR,${value}`);
continue;
}
const v = fastIpVersion(value);
if (v === 4) {
this.result.push(`SRC-IP-CIDR,${value}/32`);
continue;
}
if (v === 6) {
this.result.push(`SRC-IP-CIDR6,${value}/128`);
continue;
}
}
}
writeSourcePorts(port: Set<string>): void {
appendSetElementsToArray(this.result, port, i => `SRC-PORT,${i}`);
}
writeDestinationPorts(port: Set<string>): void {
appendSetElementsToArray(this.result, port, i => `DST-PORT,${i}`);
}
writeOtherRules = noop;
}

View File

@@ -0,0 +1,152 @@
import { BaseWriteStrategy } from './base';
import { appendArrayInPlace } from '../append-array-in-place';
import { noop } from 'foxts/noop';
import { fastIpVersion, withIdentityContent } from '../misc';
import stringify from 'json-stringify-pretty-compact';
import { OUTPUT_SINGBOX_DIR } from '../../constants/dir';
interface SingboxHeadlessRule {
domain: string[], // this_ruleset_is_made_by_sukkaw.ruleset.skk.moe
domain_suffix: string[], // this_ruleset_is_made_by_sukkaw.ruleset.skk.moe
domain_keyword?: string[],
domain_regex?: string[],
source_ip_cidr?: string[],
ip_cidr?: string[],
source_port?: number[],
source_port_range?: string[],
port?: number[],
port_range?: string[],
process_name?: string[],
process_path?: string[]
}
export interface SingboxSourceFormat {
version: 2 | number & {},
rules: SingboxHeadlessRule[]
}
export class SingboxSource extends BaseWriteStrategy {
readonly fileExtension = 'json';
static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
private singbox: SingboxHeadlessRule = {
domain: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'],
domain_suffix: ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe']
};
protected get result() {
return SingboxSource.jsonToLines({
version: 2,
rules: [this.singbox]
});
}
constructor(public type: string, protected outputDir = OUTPUT_SINGBOX_DIR) {
super(outputDir);
}
withPadding = withIdentityContent;
writeDomain(domain: string): void {
this.singbox.domain.push(domain);
}
writeDomainSuffix(domain: string): void {
(this.singbox.domain_suffix ??= []).push(domain);
}
writeDomainKeywords(keyword: Set<string>): void {
appendArrayInPlace(
this.singbox.domain_keyword ??= [],
Array.from(keyword)
);
}
writeDomainWildcards(wildcard: Set<string>): void {
appendArrayInPlace(
this.singbox.domain_regex ??= [],
Array.from(wildcard, SingboxSource.domainWildCardToRegex)
);
}
writeUserAgents = noop;
writeProcessNames(processName: Set<string>): void {
appendArrayInPlace(
this.singbox.process_name ??= [],
Array.from(processName)
);
}
writeProcessPaths(processPath: Set<string>): void {
appendArrayInPlace(
this.singbox.process_path ??= [],
Array.from(processPath)
);
}
writeUrlRegexes = noop;
writeIpCidrs(ipCidr: string[]): void {
appendArrayInPlace(
this.singbox.ip_cidr ??= [],
ipCidr
);
}
writeIpCidr6s(ipCidr6: string[]): void {
appendArrayInPlace(
this.singbox.ip_cidr ??= [],
ipCidr6
);
}
writeGeoip = noop;
writeIpAsns = noop;
writeSourceIpCidrs(sourceIpCidr: string[]): void {
this.singbox.source_ip_cidr ??= [];
for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
const value = sourceIpCidr[i];
if (value.includes('/')) {
this.singbox.source_ip_cidr.push(value);
continue;
}
const v = fastIpVersion(value);
if (v === 4) {
this.singbox.source_ip_cidr.push(`${value}/32`);
continue;
}
if (v === 6) {
this.singbox.source_ip_cidr.push(`${value}/128`);
continue;
}
}
}
writeSourcePorts(port: Set<string>): void {
this.singbox.source_port ??= [];
for (const i of port) {
const tmp = Number(i);
if (!Number.isNaN(tmp)) {
this.singbox.source_port.push(tmp);
}
}
}
writeDestinationPorts(port: Set<string>): void {
this.singbox.port ??= [];
for (const i of port) {
const tmp = Number(i);
if (!Number.isNaN(tmp)) {
this.singbox.port.push(tmp);
}
}
}
writeOtherRules = noop;
}

View File

@@ -0,0 +1,262 @@
import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
import { BaseWriteStrategy } from './base';
import { appendArrayInPlace } from '../append-array-in-place';
import { noop } from 'foxts/noop';
import { isProbablyIpv4 } from 'foxts/is-probably-ip';
import picocolors from 'picocolors';
import { normalizeDomain } from '../normalize-domain';
import { OUTPUT_MODULES_DIR, OUTPUT_SURGE_DIR } from '../../constants/dir';
import { withBannerArray, withIdentityContent } from '../misc';
export class SurgeDomainSet extends BaseWriteStrategy {
// readonly type = 'domainset';
readonly fileExtension = 'conf';
type = 'domainset';
protected result: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
constructor(outputDir = OUTPUT_SURGE_DIR) {
super(outputDir);
}
withPadding = withBannerArray;
writeDomain(domain: string): void {
this.result.push(domain);
}
writeDomainSuffix(domain: string): void {
this.result.push('.' + domain);
}
writeDomainKeywords = noop;
writeDomainWildcards = noop;
writeUserAgents = noop;
writeProcessNames = noop;
writeProcessPaths = noop;
writeUrlRegexes = noop;
writeIpCidrs = noop;
writeIpCidr6s = noop;
writeGeoip = noop;
writeIpAsns = noop;
writeSourceIpCidrs = noop;
writeSourcePorts = noop;
writeDestinationPorts = noop;
writeOtherRules = noop;
}
export class SurgeRuleSet extends BaseWriteStrategy {
readonly fileExtension = 'conf';
protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
constructor(public readonly type: string, outputDir = OUTPUT_SURGE_DIR) {
super(outputDir);
}
withPadding = withBannerArray;
writeDomain(domain: string): void {
this.result.push('DOMAIN,' + domain);
}
writeDomainSuffix(domain: string): void {
this.result.push('DOMAIN-SUFFIX,' + domain);
}
writeDomainKeywords(keyword: Set<string>): void {
appendSetElementsToArray(this.result, keyword, i => `DOMAIN-KEYWORD,${i}`);
}
writeDomainWildcards(wildcard: Set<string>): void {
appendSetElementsToArray(this.result, wildcard, i => `DOMAIN-WILDCARD,${i}`);
}
writeUserAgents(userAgent: Set<string>): void {
appendSetElementsToArray(this.result, userAgent, i => `USER-AGENT,${i}`);
}
writeProcessNames(processName: Set<string>): void {
appendSetElementsToArray(this.result, processName, i => `PROCESS-NAME,${i}`);
}
writeProcessPaths(processPath: Set<string>): void {
appendSetElementsToArray(this.result, processPath, i => `PROCESS-NAME,${i}`);
}
writeUrlRegexes(urlRegex: Set<string>): void {
appendSetElementsToArray(this.result, urlRegex, i => `URL-REGEX,${i}`);
}
writeIpCidrs(ipCidr: string[], noResolve: boolean): void {
for (let i = 0, len = ipCidr.length; i < len; i++) {
this.result.push(`IP-CIDR,${ipCidr[i]}${noResolve ? ',no-resolve' : ''}`);
}
}
writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void {
for (let i = 0, len = ipCidr6.length; i < len; i++) {
this.result.push(`IP-CIDR6,${ipCidr6[i]}${noResolve ? ',no-resolve' : ''}`);
}
}
writeGeoip(geoip: Set<string>, noResolve: boolean): void {
appendSetElementsToArray(this.result, geoip, i => `GEOIP,${i}${noResolve ? ',no-resolve' : ''}`);
}
writeIpAsns(asns: Set<string>, noResolve: boolean): void {
appendSetElementsToArray(this.result, asns, i => `IP-ASN,${i}${noResolve ? ',no-resolve' : ''}`);
}
writeSourceIpCidrs(sourceIpCidr: string[]): void {
for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
this.result.push(`SRC-IP,${sourceIpCidr[i]}`);
}
}
writeSourcePorts(port: Set<string>): void {
appendSetElementsToArray(this.result, port, i => `SRC-PORT,${i}`);
}
writeDestinationPorts(port: Set<string>): void {
appendSetElementsToArray(this.result, port, i => `DEST-PORT,${i}`);
}
writeOtherRules(rule: string[]): void {
appendArrayInPlace(this.result, rule);
}
}
export class SurgeMitmSgmodule extends BaseWriteStrategy {
// readonly type = 'domainset';
readonly fileExtension = 'sgmodule';
type = '';
private rules = new Set<string>();
protected get result() {
if (this.rules.size === 0) {
return null;
}
return [
'#!name=[Sukka] Surge Reject MITM',
`#!desc=为 URL Regex 规则组启用 MITM (size: ${this.rules.size})`,
'',
'[MITM]',
'hostname = %APPEND% ' + Array.from(this.rules).join(', ')
];
}
withPadding = withIdentityContent;
constructor(moduleName: string, outputDir = OUTPUT_MODULES_DIR) {
super(outputDir);
this.overwriteFilename = moduleName;
}
writeDomain = noop;
writeDomainSuffix = noop;
writeDomainKeywords = noop;
writeDomainWildcards = noop;
writeUserAgents = noop;
writeProcessNames = noop;
writeProcessPaths = noop;
writeUrlRegexes(urlRegexes: Set<string>): void {
const urlRegexResults: Array<{ origin: string, processed: string[] }> = [];
const parsedFailures: Array<[original: string, processed: string]> = [];
const parsed: Array<[original: string, domain: string]> = [];
for (let urlRegex of urlRegexes) {
if (
urlRegex.startsWith('http://')
|| urlRegex.startsWith('^http://')
) {
continue;
}
if (urlRegex.startsWith('^https?://')) {
urlRegex = urlRegex.slice(10);
}
if (urlRegex.startsWith('^https://')) {
urlRegex = urlRegex.slice(9);
}
const potentialHostname = urlRegex.split('/')[0]
// pre process regex
.replaceAll(String.raw`\.`, '.')
.replaceAll('.+', '*')
.replaceAll(/([a-z])\?/g, '($1|)')
// convert regex to surge hostlist syntax
.replaceAll('([a-z])', '?')
.replaceAll(String.raw`\d`, '?')
.replaceAll(/\*+/g, '*');
let processed: string[] = [potentialHostname];
const matches = [...potentialHostname.matchAll(/\((?:([^()|]+)\|)+([^()|]*)\)/g)];
if (matches.length > 0) {
const replaceVariant = (combinations: string[], fullMatch: string, options: string[]): string[] => {
const newCombinations: string[] = [];
combinations.forEach(combination => {
options.forEach(option => {
newCombinations.push(combination.replace(fullMatch, option));
});
});
return newCombinations;
};
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const [_, ...options] = match;
processed = replaceVariant(processed, _, options);
}
}
urlRegexResults.push({
origin: potentialHostname,
processed
});
}
for (const i of urlRegexResults) {
for (const processed of i.processed) {
if (
normalizeDomain(
processed
.replaceAll('*', 'a')
.replaceAll('?', 'b')
)
) {
parsed.push([i.origin, processed]);
} else if (!isProbablyIpv4(processed)) {
parsedFailures.push([i.origin, processed]);
}
}
}
if (parsedFailures.length > 0) {
console.error(picocolors.bold('Parsed Failed'));
console.table(parsedFailures);
}
for (let i = 0, len = parsed.length; i < len; i++) {
this.rules.add(parsed[i][1]);
}
}
writeIpCidrs = noop;
writeIpCidr6s = noop;
writeGeoip = noop;
writeIpAsns = noop;
writeSourceIpCidrs = noop;
writeSourcePorts = noop;
writeDestinationPorts = noop;
writeOtherRules = noop;
}