mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: new output
This commit is contained in:
249
Build/lib/create-file-new.ts
Normal file
249
Build/lib/create-file-new.ts
Normal file
@@ -0,0 +1,249 @@
|
||||
import path from 'node:path';
|
||||
|
||||
import type { Span } from '../trace';
|
||||
import { surgeDomainsetToClashDomainset } from './clash';
|
||||
import { compareAndWriteFile, withBannerArray } from './create-file';
|
||||
import { ipCidrListToSingbox, surgeDomainsetToSingbox } from './singbox';
|
||||
import { sortDomains } from './stable-sort-domain';
|
||||
import { createTrie } from './trie';
|
||||
import { invariant } from 'foxact/invariant';
|
||||
import { OUTPUT_CLASH_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../constants/dir';
|
||||
import stringify from 'json-stringify-pretty-compact';
|
||||
import { appendArrayInPlace } from './append-array-in-place';
|
||||
|
||||
abstract class RuleOutput {
|
||||
protected domainTrie = createTrie<unknown>(null, true);
|
||||
protected domainKeywords = new Set<string>();
|
||||
protected domainWildcard = new Set<string>();
|
||||
protected ipcidr = new Set<string>();
|
||||
protected ipcidrNoResolve = new Set<string>();
|
||||
protected ipcidr6 = new Set<string>();
|
||||
protected ipcidr6NoResolve = new Set<string>();
|
||||
protected otherRules = new Set<string>();
|
||||
protected abstract type: 'domainset' | 'non_ip' | 'ip';
|
||||
|
||||
protected pendingPromise = Promise.resolve();
|
||||
|
||||
static jsonToLines(this: void, json: unknown): string[] {
|
||||
return stringify(json).split('\n');
|
||||
}
|
||||
|
||||
constructor(
|
||||
protected readonly span: Span,
|
||||
protected readonly id: string
|
||||
) {}
|
||||
|
||||
protected title: string | null = null;
|
||||
withTitle(title: string) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected description: string[] | readonly string[] | null = null;
|
||||
withDescription(description: string[] | readonly string[]) {
|
||||
this.description = description;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected date = new Date();
|
||||
withDate(date: Date) {
|
||||
this.date = date;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected apexDomainMap: Map<string, string> | null = null;
|
||||
protected subDomainMap: Map<string, string> | null = null;
|
||||
withDomainMap(apexDomainMap: Map<string, string>, subDomainMap: Map<string, string>) {
|
||||
this.apexDomainMap = apexDomainMap;
|
||||
this.subDomainMap = subDomainMap;
|
||||
return this;
|
||||
}
|
||||
|
||||
addDomain(domain: string) {
|
||||
this.domainTrie.add(domain);
|
||||
return this;
|
||||
}
|
||||
|
||||
addDomainSuffix(domain: string) {
|
||||
this.domainTrie.add(domain[0] === '.' ? domain : '.' + domain);
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddDomainSuffix(domains: string[]) {
|
||||
for (let i = 0, len = domains.length; i < len; i++) {
|
||||
this.addDomainSuffix(domains[i]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
addDomainKeyword(keyword: string) {
|
||||
this.domainKeywords.add(keyword);
|
||||
return this;
|
||||
}
|
||||
|
||||
addDomainWildcard(wildcard: string) {
|
||||
this.domainWildcard.add(wildcard);
|
||||
return this;
|
||||
}
|
||||
|
||||
private async addFromDomainsetPromise(source: AsyncIterable<string> | Iterable<string> | string[]) {
|
||||
for await (const line of source) {
|
||||
if (line[0] === '.') {
|
||||
this.addDomainSuffix(line);
|
||||
} else {
|
||||
this.addDomain(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addFromDomainset(source: AsyncIterable<string> | Iterable<string> | string[]) {
|
||||
this.pendingPromise = this.pendingPromise.then(() => this.addFromDomainsetPromise(source));
|
||||
return this;
|
||||
}
|
||||
|
||||
async addFromRuleset(source: AsyncIterable<string> | Iterable<string>) {
|
||||
for await (const line of source) {
|
||||
const [type, value, arg] = line.split(',');
|
||||
switch (type) {
|
||||
case 'DOMAIN':
|
||||
this.addDomain(value);
|
||||
break;
|
||||
case 'DOMAIN-SUFFIX':
|
||||
this.addDomainSuffix(value);
|
||||
break;
|
||||
case 'DOMAIN-KEYWORD':
|
||||
this.addDomainKeyword(value);
|
||||
break;
|
||||
case 'DOMAIN-WILDCARD':
|
||||
this.addDomainWildcard(value);
|
||||
break;
|
||||
case 'IP-CIDR':
|
||||
(arg === 'no-resolve' ? this.ipcidrNoResolve : this.ipcidr).add(value);
|
||||
break;
|
||||
case 'IP-CIDR6':
|
||||
(arg === 'no-resolve' ? this.ipcidr6NoResolve : this.ipcidr6).add(value);
|
||||
break;
|
||||
default:
|
||||
this.otherRules.add(line);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddCIDR4(cidr: string[]) {
|
||||
for (let i = 0, len = cidr.length; i < len; i++) {
|
||||
this.ipcidr.add(cidr[i]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
bulkAddCIDR6(cidr: string[]) {
|
||||
for (let i = 0, len = cidr.length; i < len; i++) {
|
||||
this.ipcidr6.add(cidr[i]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
abstract write(): Promise<void>;
|
||||
}
|
||||
|
||||
export class DomainsetOutput extends RuleOutput {
|
||||
protected type = 'domainset' as const;
|
||||
|
||||
async write() {
|
||||
await this.pendingPromise;
|
||||
|
||||
invariant(this.title, 'Missing title');
|
||||
invariant(this.description, 'Missing description');
|
||||
|
||||
const sorted = sortDomains(this.domainTrie.dump(), this.apexDomainMap, this.subDomainMap);
|
||||
sorted.push('this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
|
||||
|
||||
const surge = sorted;
|
||||
const clash = surgeDomainsetToClashDomainset(sorted);
|
||||
const singbox = RuleOutput.jsonToLines(surgeDomainsetToSingbox(sorted));
|
||||
|
||||
await Promise.all([
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
surge
|
||||
),
|
||||
path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
clash
|
||||
),
|
||||
path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
singbox,
|
||||
path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
|
||||
)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
export class IPListOutput extends RuleOutput {
|
||||
protected type = 'ip' as const;
|
||||
|
||||
constructor(span: Span, id: string, private readonly clashUseRule = true) {
|
||||
super(span, id);
|
||||
}
|
||||
|
||||
async write() {
|
||||
await this.pendingPromise;
|
||||
|
||||
invariant(this.title, 'Missing title');
|
||||
invariant(this.description, 'Missing description');
|
||||
|
||||
const sorted4 = Array.from(this.ipcidr);
|
||||
const sorted6 = Array.from(this.ipcidr6);
|
||||
const merged = appendArrayInPlace(appendArrayInPlace([], sorted4), sorted6);
|
||||
|
||||
const surge = sorted4.map(i => `IP-CIDR,${i}`);
|
||||
appendArrayInPlace(surge, sorted6.map(i => `IP-CIDR6,${i}`));
|
||||
surge.push('DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
|
||||
|
||||
const clash = this.clashUseRule ? surge : merged;
|
||||
const singbox = RuleOutput.jsonToLines(ipCidrListToSingbox(merged));
|
||||
|
||||
await Promise.all([
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
surge
|
||||
),
|
||||
path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
withBannerArray(
|
||||
this.title,
|
||||
this.description,
|
||||
this.date,
|
||||
clash
|
||||
),
|
||||
path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
|
||||
),
|
||||
compareAndWriteFile(
|
||||
this.span,
|
||||
singbox,
|
||||
path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
|
||||
)
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import fs from 'node:fs';
|
||||
import { fastStringArrayJoin, writeFile } from './misc';
|
||||
import { readFileByLine } from './fetch-text-by-line';
|
||||
import stringify from 'json-stringify-pretty-compact';
|
||||
import { ipCidrListToSingbox, surgeDomainsetToSingbox, surgeRulesetToSingbox } from './singbox';
|
||||
import { surgeDomainsetToSingbox, surgeRulesetToSingbox } from './singbox';
|
||||
import { createTrie } from './trie';
|
||||
import { pack, unpackFirst, unpackSecond } from './bitwise';
|
||||
import { asyncWriteToStream } from './async-write-to-stream';
|
||||
@@ -23,8 +23,7 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
|
||||
|
||||
if (index > linesA.length - 1) {
|
||||
if (index === linesA.length && lineB === '') {
|
||||
index--;
|
||||
continue;
|
||||
return true;
|
||||
}
|
||||
// The file becomes smaller
|
||||
return false;
|
||||
@@ -51,7 +50,7 @@ export const fileEqual = async (linesA: string[], source: AsyncIterable<string>)
|
||||
}
|
||||
}
|
||||
|
||||
if (index !== linesA.length - 1) {
|
||||
if (index < linesA.length - 1) {
|
||||
// The file becomes larger
|
||||
return false;
|
||||
}
|
||||
@@ -96,7 +95,7 @@ export async function compareAndWriteFile(span: Span, linesA: string[], filePath
|
||||
});
|
||||
}
|
||||
|
||||
const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
|
||||
export const withBannerArray = (title: string, description: string[] | readonly string[], date: Date, content: string[]) => {
|
||||
return [
|
||||
'#########################################',
|
||||
`# ${title}`,
|
||||
@@ -191,7 +190,7 @@ const MARK = 'this_ruleset_is_made_by_sukkaw.ruleset.skk.moe';
|
||||
export const createRuleset = (
|
||||
parentSpan: Span,
|
||||
title: string, description: string[] | readonly string[], date: Date, content: string[],
|
||||
type: 'ruleset' | 'domainset' | 'ipcidr' | 'ipcidr6',
|
||||
type: 'ruleset' | 'domainset',
|
||||
[surgePath, clashPath, singBoxPath, _clashMrsPath]: readonly [
|
||||
surgePath: string,
|
||||
clashPath: string,
|
||||
@@ -210,12 +209,6 @@ export const createRuleset = (
|
||||
case 'ruleset':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content)];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content.map(i => `IP-CIDR,${i}`))];
|
||||
break;
|
||||
case 'ipcidr6':
|
||||
_surgeContent = [`DOMAIN,${MARK}`, ...processRuleSet(content.map(i => `IP-CIDR6,${i}`))];
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
@@ -232,10 +225,6 @@ export const createRuleset = (
|
||||
case 'ruleset':
|
||||
_clashContent = [`DOMAIN,${MARK}`, ...surgeRulesetToClashClassicalTextRuleset(processRuleSet(content))];
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_clashContent = content;
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
@@ -250,10 +239,6 @@ export const createRuleset = (
|
||||
case 'ruleset':
|
||||
_singBoxContent = surgeRulesetToSingbox([`DOMAIN,${MARK}`, ...processRuleSet(content)]);
|
||||
break;
|
||||
case 'ipcidr':
|
||||
case 'ipcidr6':
|
||||
_singBoxContent = ipCidrListToSingbox(content);
|
||||
break;
|
||||
default:
|
||||
throw new TypeError(`Unknown type: ${type}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user