create-file.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. import path from 'node:path';
  2. import type { Span } from '../trace';
  3. import { surgeDomainsetToClashDomainset, surgeRulesetToClashClassicalTextRuleset } from './clash';
  4. import { ipCidrListToSingbox, surgeDomainsetToSingbox, surgeRulesetToSingbox } from './singbox';
  5. import { buildParseDomainMap, sortDomains } from './stable-sort-domain';
  6. import { createTrie } from './trie';
  7. import { invariant } from 'foxact/invariant';
  8. import { OUTPUT_CLASH_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../constants/dir';
  9. import stringify from 'json-stringify-pretty-compact';
  10. import { appendArrayInPlace } from './append-array-in-place';
  11. import { nullthrow } from 'foxact/nullthrow';
  12. import createKeywordFilter from './aho-corasick';
  13. import picocolors from 'picocolors';
  14. import fs from 'node:fs';
  15. import { fastStringArrayJoin, writeFile } from './misc';
  16. import { readFileByLine } from './fetch-text-by-line';
  17. import { asyncWriteToStream } from './async-write-to-stream';
  18. const defaultSortTypeOrder = Symbol('defaultSortTypeOrder');
  19. const sortTypeOrder: Record<string | typeof defaultSortTypeOrder, number> = {
  20. DOMAIN: 1,
  21. 'DOMAIN-SUFFIX': 2,
  22. 'DOMAIN-KEYWORD': 10,
  23. // experimental domain wildcard support
  24. 'DOMAIN-WILDCARD': 20,
  25. 'DOMAIN-REGEX': 21,
  26. 'USER-AGENT': 30,
  27. 'PROCESS-NAME': 40,
  28. [defaultSortTypeOrder]: 50, // default sort order for unknown type
  29. 'URL-REGEX': 100,
  30. AND: 300,
  31. OR: 300,
  32. 'IP-CIDR': 400,
  33. 'IP-CIDR6': 400
  34. };
  35. abstract class RuleOutput {
  36. protected domainTrie = createTrie<unknown>(null, true);
  37. protected domainKeywords = new Set<string>();
  38. protected domainWildcard = new Set<string>();
  39. protected ipcidr = new Set<string>();
  40. protected ipcidrNoResolve = new Set<string>();
  41. protected ipcidr6 = new Set<string>();
  42. protected ipcidr6NoResolve = new Set<string>();
  43. protected otherRules: Array<[raw: string, orderWeight: number]> = [];
  44. protected abstract type: 'domainset' | 'non_ip' | 'ip';
  45. protected pendingPromise = Promise.resolve();
  46. static jsonToLines(this: void, json: unknown): string[] {
  47. return stringify(json).split('\n');
  48. }
  49. constructor(
  50. protected readonly span: Span,
  51. protected readonly id: string
  52. ) {}
  53. protected title: string | null = null;
  54. withTitle(title: string) {
  55. this.title = title;
  56. return this;
  57. }
  58. protected description: string[] | readonly string[] | null = null;
  59. withDescription(description: string[] | readonly string[]) {
  60. this.description = description;
  61. return this;
  62. }
  63. protected date = new Date();
  64. withDate(date: Date) {
  65. this.date = date;
  66. return this;
  67. }
  68. protected apexDomainMap: Map<string, string> | null = null;
  69. protected subDomainMap: Map<string, string> | null = null;
  70. withDomainMap(apexDomainMap: Map<string, string>, subDomainMap: Map<string, string>) {
  71. this.apexDomainMap = apexDomainMap;
  72. this.subDomainMap = subDomainMap;
  73. return this;
  74. }
  75. addDomain(domain: string) {
  76. this.domainTrie.add(domain);
  77. return this;
  78. }
  79. addDomainSuffix(domain: string) {
  80. this.domainTrie.add(domain[0] === '.' ? domain : '.' + domain);
  81. return this;
  82. }
  83. bulkAddDomainSuffix(domains: string[]) {
  84. for (let i = 0, len = domains.length; i < len; i++) {
  85. this.addDomainSuffix(domains[i]);
  86. }
  87. return this;
  88. }
  89. addDomainKeyword(keyword: string) {
  90. this.domainKeywords.add(keyword);
  91. return this;
  92. }
  93. addDomainWildcard(wildcard: string) {
  94. this.domainWildcard.add(wildcard);
  95. return this;
  96. }
  97. private async addFromDomainsetPromise(source: AsyncIterable<string> | Iterable<string> | string[]) {
  98. for await (const line of source) {
  99. if (line[0] === '.') {
  100. this.addDomainSuffix(line);
  101. } else {
  102. this.addDomain(line);
  103. }
  104. }
  105. }
  106. addFromDomainset(source: AsyncIterable<string> | Iterable<string> | string[]) {
  107. this.pendingPromise = this.pendingPromise.then(() => this.addFromDomainsetPromise(source));
  108. return this;
  109. }
  110. private async addFromRulesetPromise(source: AsyncIterable<string> | Iterable<string>) {
  111. for await (const line of source) {
  112. const splitted = line.split(',');
  113. const type = splitted[0];
  114. const value = splitted[1];
  115. const arg = splitted[2];
  116. switch (type) {
  117. case 'DOMAIN':
  118. this.addDomain(value);
  119. break;
  120. case 'DOMAIN-SUFFIX':
  121. this.addDomainSuffix(value);
  122. break;
  123. case 'DOMAIN-KEYWORD':
  124. this.addDomainKeyword(value);
  125. break;
  126. case 'DOMAIN-WILDCARD':
  127. this.addDomainWildcard(value);
  128. break;
  129. case 'IP-CIDR':
  130. (arg === 'no-resolve' ? this.ipcidrNoResolve : this.ipcidr).add(value);
  131. break;
  132. case 'IP-CIDR6':
  133. (arg === 'no-resolve' ? this.ipcidr6NoResolve : this.ipcidr6).add(value);
  134. break;
  135. default:
  136. this.otherRules.push([line, type in sortTypeOrder ? sortTypeOrder[type] : sortTypeOrder[defaultSortTypeOrder]]);
  137. break;
  138. }
  139. }
  140. }
  141. addFromRuleset(source: AsyncIterable<string> | Iterable<string>) {
  142. this.pendingPromise = this.pendingPromise.then(() => this.addFromRulesetPromise(source));
  143. return this;
  144. }
  145. bulkAddCIDR4(cidr: string[]) {
  146. for (let i = 0, len = cidr.length; i < len; i++) {
  147. this.ipcidr.add(cidr[i]);
  148. }
  149. return this;
  150. }
  151. bulkAddCIDR4NoResolve(cidr: string[]) {
  152. for (let i = 0, len = cidr.length; i < len; i++) {
  153. this.ipcidrNoResolve.add(cidr[i]);
  154. }
  155. return this;
  156. }
  157. bulkAddCIDR6(cidr: string[]) {
  158. for (let i = 0, len = cidr.length; i < len; i++) {
  159. this.ipcidr6.add(cidr[i]);
  160. }
  161. return this;
  162. }
  163. bulkAddCIDR6NoResolve(cidr: string[]) {
  164. for (let i = 0, len = cidr.length; i < len; i++) {
  165. this.ipcidr6NoResolve.add(cidr[i]);
  166. }
  167. return this;
  168. }
  169. abstract write(): Promise<void>;
  170. }
  171. export class DomainsetOutput extends RuleOutput {
  172. protected type = 'domainset' as const;
  173. private $dumped: string[] | null = null;
  174. get dumped() {
  175. if (!this.$dumped) {
  176. const kwfilter = createKeywordFilter(this.domainKeywords);
  177. const results: string[] = [];
  178. const dumped = this.domainTrie.dump();
  179. for (let i = 0, len = dumped.length; i < len; i++) {
  180. const domain = dumped[i];
  181. if (!kwfilter(domain)) {
  182. results.push(domain);
  183. }
  184. }
  185. this.$dumped = results;
  186. }
  187. return this.$dumped;
  188. }
  189. calcDomainMap() {
  190. if (!this.apexDomainMap || !this.subDomainMap) {
  191. const { domainMap, subdomainMap } = buildParseDomainMap(this.dumped);
  192. this.apexDomainMap = domainMap;
  193. this.subDomainMap = subdomainMap;
  194. }
  195. }
  196. async write() {
  197. await this.pendingPromise;
  198. invariant(this.title, 'Missing title');
  199. invariant(this.description, 'Missing description');
  200. const sorted = sortDomains(this.dumped, this.apexDomainMap, this.subDomainMap);
  201. sorted.push('this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
  202. const surge = sorted;
  203. const clash = surgeDomainsetToClashDomainset(sorted);
  204. // TODO: Implement singbox directly using data
  205. const singbox = RuleOutput.jsonToLines(surgeDomainsetToSingbox(sorted));
  206. await Promise.all([
  207. compareAndWriteFile(
  208. this.span,
  209. withBannerArray(
  210. this.title,
  211. this.description,
  212. this.date,
  213. surge
  214. ),
  215. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  216. ),
  217. compareAndWriteFile(
  218. this.span,
  219. withBannerArray(
  220. this.title,
  221. this.description,
  222. this.date,
  223. clash
  224. ),
  225. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  226. ),
  227. compareAndWriteFile(
  228. this.span,
  229. singbox,
  230. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  231. )
  232. ]);
  233. }
  234. getStatMap() {
  235. invariant(this.dumped, 'Non dumped yet');
  236. invariant(this.apexDomainMap, 'Missing apex domain map');
  237. return Array.from(
  238. (
  239. nullthrow(this.dumped, 'Non dumped yet').reduce<Map<string, number>>((acc, cur) => {
  240. const suffix = this.apexDomainMap!.get(cur);
  241. if (suffix) {
  242. acc.set(suffix, (acc.get(suffix) ?? 0) + 1);
  243. }
  244. return acc;
  245. }, new Map())
  246. ).entries()
  247. )
  248. .filter(a => a[1] > 9)
  249. .sort(
  250. (a, b) => (b[1] - a[1]) || a[0].localeCompare(b[0])
  251. )
  252. .map(([domain, count]) => `${domain}${' '.repeat(100 - domain.length)}${count}`);
  253. }
  254. }
  255. export class IPListOutput extends RuleOutput {
  256. protected type = 'ip' as const;
  257. constructor(span: Span, id: string, private readonly clashUseRule = true) {
  258. super(span, id);
  259. }
  260. async write() {
  261. await this.pendingPromise;
  262. invariant(this.title, 'Missing title');
  263. invariant(this.description, 'Missing description');
  264. const sorted4 = Array.from(this.ipcidr);
  265. const sorted6 = Array.from(this.ipcidr6);
  266. const merged = appendArrayInPlace(appendArrayInPlace([], sorted4), sorted6);
  267. const surge = sorted4.map(i => `IP-CIDR,${i}`);
  268. appendArrayInPlace(surge, sorted6.map(i => `IP-CIDR6,${i}`));
  269. surge.push('DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
  270. const clash = this.clashUseRule ? surge : merged;
  271. // TODO: Implement singbox directly using data
  272. const singbox = RuleOutput.jsonToLines(ipCidrListToSingbox(merged));
  273. await Promise.all([
  274. compareAndWriteFile(
  275. this.span,
  276. withBannerArray(
  277. this.title,
  278. this.description,
  279. this.date,
  280. surge
  281. ),
  282. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  283. ),
  284. compareAndWriteFile(
  285. this.span,
  286. withBannerArray(
  287. this.title,
  288. this.description,
  289. this.date,
  290. clash
  291. ),
  292. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  293. ),
  294. compareAndWriteFile(
  295. this.span,
  296. singbox,
  297. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  298. )
  299. ]);
  300. }
  301. }
  302. export class RulesetOutput extends RuleOutput {
  303. constructor(span: Span, id: string, protected type: 'non_ip' | 'ip') {
  304. super(span, id);
  305. }
  306. async write() {
  307. await this.pendingPromise;
  308. invariant(this.title, 'Missing title');
  309. invariant(this.description, 'Missing description');
  310. const results: string[] = [
  311. 'DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'
  312. ];
  313. const kwfilter = createKeywordFilter(this.domainKeywords);
  314. const sortedDomains = sortDomains(this.domainTrie.dump(), this.apexDomainMap, this.subDomainMap);
  315. for (let i = 0, len = sortedDomains.length; i < len; i++) {
  316. const domain = sortedDomains[i];
  317. if (kwfilter(domain)) {
  318. continue;
  319. }
  320. if (domain[0] === '.') {
  321. results.push(`DOMAIN-SUFFIX,${domain.slice(1)}`);
  322. } else {
  323. results.push(`DOMAIN,${domain}`);
  324. }
  325. }
  326. for (const keyword of this.domainKeywords) {
  327. results.push(`DOMAIN-KEYWORD,${keyword}`);
  328. }
  329. for (const wildcard of this.domainWildcard) {
  330. results.push(`DOMAIN-WILDCARD,${wildcard}`);
  331. }
  332. const sortedRules = this.otherRules.sort((a, b) => a[1] - b[1]);
  333. for (let i = 0, len = sortedRules.length; i < len; i++) {
  334. results.push(sortedRules[i][0]);
  335. }
  336. this.ipcidr.forEach(cidr => results.push(`IP-CIDR,${cidr}`));
  337. this.ipcidrNoResolve.forEach(cidr => results.push(`IP-CIDR,${cidr},no-resolve`));
  338. this.ipcidr6.forEach(cidr => results.push(`IP-CIDR6,${cidr}`));
  339. this.ipcidr6NoResolve.forEach(cidr => results.push(`IP-CIDR6,${cidr},no-resolve`));
  340. const surge = results;
  341. const clash = surgeRulesetToClashClassicalTextRuleset(results);
  342. // TODO: Implement singbox directly using data
  343. const singbox = RuleOutput.jsonToLines(surgeRulesetToSingbox(results));
  344. await Promise.all([
  345. compareAndWriteFile(
  346. this.span,
  347. withBannerArray(
  348. this.title,
  349. this.description,
  350. this.date,
  351. surge
  352. ),
  353. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  354. ),
  355. compareAndWriteFile(
  356. this.span,
  357. withBannerArray(
  358. this.title,
  359. this.description,
  360. this.date,
  361. clash
  362. ),
  363. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  364. ),
  365. compareAndWriteFile(
  366. this.span,
  367. singbox,
  368. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  369. )
  370. ]);
  371. }
  372. }
  373. function withBannerArray(title: string, description: string[] | readonly string[], date: Date, content: string[]) {
  374. return [
  375. '#########################################',
  376. `# ${title}`,
  377. `# Last Updated: ${date.toISOString()}`,
  378. `# Size: ${content.length}`,
  379. ...description.map(line => (line ? `# ${line}` : '#')),
  380. '#########################################',
  381. ...content,
  382. '################## EOF ##################'
  383. ];
  384. };
  385. export const fileEqual = async (linesA: string[], source: AsyncIterable<string>): Promise<boolean> => {
  386. if (linesA.length === 0) {
  387. return false;
  388. }
  389. let index = -1;
  390. for await (const lineB of source) {
  391. index++;
  392. if (index > linesA.length - 1) {
  393. if (index === linesA.length && lineB === '') {
  394. return true;
  395. }
  396. // The file becomes smaller
  397. return false;
  398. }
  399. const lineA = linesA[index];
  400. if (lineA[0] === '#' && lineB[0] === '#') {
  401. continue;
  402. }
  403. if (
  404. lineA[0] === '/'
  405. && lineA[1] === '/'
  406. && lineB[0] === '/'
  407. && lineB[1] === '/'
  408. && lineA[3] === '#'
  409. && lineB[3] === '#'
  410. ) {
  411. continue;
  412. }
  413. if (lineA !== lineB) {
  414. return false;
  415. }
  416. }
  417. if (index < linesA.length - 1) {
  418. // The file becomes larger
  419. return false;
  420. }
  421. return true;
  422. };
  423. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  424. let isEqual = true;
  425. const linesALen = linesA.length;
  426. if (fs.existsSync(filePath)) {
  427. isEqual = await fileEqual(linesA, readFileByLine(filePath));
  428. } else {
  429. console.log(`${filePath} does not exists, writing...`);
  430. isEqual = false;
  431. }
  432. if (isEqual) {
  433. console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
  434. return;
  435. }
  436. await span.traceChildAsync(`writing ${filePath}`, async () => {
  437. // The default highwater mark is normally 16384,
  438. // So we make sure direct write to file if the content is
  439. // most likely less than 500 lines
  440. if (linesALen < 500) {
  441. return writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n');
  442. }
  443. const writeStream = fs.createWriteStream(filePath);
  444. for (let i = 0; i < linesALen; i++) {
  445. const p = asyncWriteToStream(writeStream, linesA[i] + '\n');
  446. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  447. if (p) await p;
  448. }
  449. await asyncWriteToStream(writeStream, '\n');
  450. writeStream.end();
  451. });
  452. }