create-file-new.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import path from 'node:path';
  2. import type { Span } from '../trace';
  3. import { surgeDomainsetToClashDomainset } from './clash';
  4. import { compareAndWriteFile, withBannerArray } from './create-file';
  5. import { ipCidrListToSingbox, surgeDomainsetToSingbox } from './singbox';
  6. import { sortDomains } from './stable-sort-domain';
  7. import { createTrie } from './trie';
  8. import { invariant } from 'foxact/invariant';
  9. import { OUTPUT_CLASH_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../constants/dir';
  10. import stringify from 'json-stringify-pretty-compact';
  11. import { appendArrayInPlace } from './append-array-in-place';
  12. abstract class RuleOutput {
  13. protected domainTrie = createTrie<unknown>(null, true);
  14. protected domainKeywords = new Set<string>();
  15. protected domainWildcard = new Set<string>();
  16. protected ipcidr = new Set<string>();
  17. protected ipcidrNoResolve = new Set<string>();
  18. protected ipcidr6 = new Set<string>();
  19. protected ipcidr6NoResolve = new Set<string>();
  20. protected otherRules = new Set<string>();
  21. protected abstract type: 'domainset' | 'non_ip' | 'ip';
  22. protected pendingPromise = Promise.resolve();
  23. static jsonToLines(this: void, json: unknown): string[] {
  24. return stringify(json).split('\n');
  25. }
  26. constructor(
  27. protected readonly span: Span,
  28. protected readonly id: string
  29. ) {}
  30. protected title: string | null = null;
  31. withTitle(title: string) {
  32. this.title = title;
  33. return this;
  34. }
  35. protected description: string[] | readonly string[] | null = null;
  36. withDescription(description: string[] | readonly string[]) {
  37. this.description = description;
  38. return this;
  39. }
  40. protected date = new Date();
  41. withDate(date: Date) {
  42. this.date = date;
  43. return this;
  44. }
  45. protected apexDomainMap: Map<string, string> | null = null;
  46. protected subDomainMap: Map<string, string> | null = null;
  47. withDomainMap(apexDomainMap: Map<string, string>, subDomainMap: Map<string, string>) {
  48. this.apexDomainMap = apexDomainMap;
  49. this.subDomainMap = subDomainMap;
  50. return this;
  51. }
  52. addDomain(domain: string) {
  53. this.domainTrie.add(domain);
  54. return this;
  55. }
  56. addDomainSuffix(domain: string) {
  57. this.domainTrie.add(domain[0] === '.' ? domain : '.' + domain);
  58. return this;
  59. }
  60. bulkAddDomainSuffix(domains: string[]) {
  61. for (let i = 0, len = domains.length; i < len; i++) {
  62. this.addDomainSuffix(domains[i]);
  63. }
  64. return this;
  65. }
  66. addDomainKeyword(keyword: string) {
  67. this.domainKeywords.add(keyword);
  68. return this;
  69. }
  70. addDomainWildcard(wildcard: string) {
  71. this.domainWildcard.add(wildcard);
  72. return this;
  73. }
  74. private async addFromDomainsetPromise(source: AsyncIterable<string> | Iterable<string> | string[]) {
  75. for await (const line of source) {
  76. if (line[0] === '.') {
  77. this.addDomainSuffix(line);
  78. } else {
  79. this.addDomain(line);
  80. }
  81. }
  82. }
  83. addFromDomainset(source: AsyncIterable<string> | Iterable<string> | string[]) {
  84. this.pendingPromise = this.pendingPromise.then(() => this.addFromDomainsetPromise(source));
  85. return this;
  86. }
  87. async addFromRuleset(source: AsyncIterable<string> | Iterable<string>) {
  88. for await (const line of source) {
  89. const [type, value, arg] = line.split(',');
  90. switch (type) {
  91. case 'DOMAIN':
  92. this.addDomain(value);
  93. break;
  94. case 'DOMAIN-SUFFIX':
  95. this.addDomainSuffix(value);
  96. break;
  97. case 'DOMAIN-KEYWORD':
  98. this.addDomainKeyword(value);
  99. break;
  100. case 'DOMAIN-WILDCARD':
  101. this.addDomainWildcard(value);
  102. break;
  103. case 'IP-CIDR':
  104. (arg === 'no-resolve' ? this.ipcidrNoResolve : this.ipcidr).add(value);
  105. break;
  106. case 'IP-CIDR6':
  107. (arg === 'no-resolve' ? this.ipcidr6NoResolve : this.ipcidr6).add(value);
  108. break;
  109. default:
  110. this.otherRules.add(line);
  111. break;
  112. }
  113. }
  114. return this;
  115. }
  116. bulkAddCIDR4(cidr: string[]) {
  117. for (let i = 0, len = cidr.length; i < len; i++) {
  118. this.ipcidr.add(cidr[i]);
  119. }
  120. return this;
  121. }
  122. bulkAddCIDR6(cidr: string[]) {
  123. for (let i = 0, len = cidr.length; i < len; i++) {
  124. this.ipcidr6.add(cidr[i]);
  125. }
  126. return this;
  127. }
  128. abstract write(): Promise<void>;
  129. }
  130. export class DomainsetOutput extends RuleOutput {
  131. protected type = 'domainset' as const;
  132. async write() {
  133. await this.pendingPromise;
  134. invariant(this.title, 'Missing title');
  135. invariant(this.description, 'Missing description');
  136. const sorted = sortDomains(this.domainTrie.dump(), this.apexDomainMap, this.subDomainMap);
  137. sorted.push('this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
  138. const surge = sorted;
  139. const clash = surgeDomainsetToClashDomainset(sorted);
  140. const singbox = RuleOutput.jsonToLines(surgeDomainsetToSingbox(sorted));
  141. await Promise.all([
  142. compareAndWriteFile(
  143. this.span,
  144. withBannerArray(
  145. this.title,
  146. this.description,
  147. this.date,
  148. surge
  149. ),
  150. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  151. ),
  152. compareAndWriteFile(
  153. this.span,
  154. withBannerArray(
  155. this.title,
  156. this.description,
  157. this.date,
  158. clash
  159. ),
  160. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  161. ),
  162. compareAndWriteFile(
  163. this.span,
  164. singbox,
  165. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  166. )
  167. ]);
  168. }
  169. }
  170. export class IPListOutput extends RuleOutput {
  171. protected type = 'ip' as const;
  172. constructor(span: Span, id: string, private readonly clashUseRule = true) {
  173. super(span, id);
  174. }
  175. async write() {
  176. await this.pendingPromise;
  177. invariant(this.title, 'Missing title');
  178. invariant(this.description, 'Missing description');
  179. const sorted4 = Array.from(this.ipcidr);
  180. const sorted6 = Array.from(this.ipcidr6);
  181. const merged = appendArrayInPlace(appendArrayInPlace([], sorted4), sorted6);
  182. const surge = sorted4.map(i => `IP-CIDR,${i}`);
  183. appendArrayInPlace(surge, sorted6.map(i => `IP-CIDR6,${i}`));
  184. surge.push('DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe');
  185. const clash = this.clashUseRule ? surge : merged;
  186. const singbox = RuleOutput.jsonToLines(ipCidrListToSingbox(merged));
  187. await Promise.all([
  188. compareAndWriteFile(
  189. this.span,
  190. withBannerArray(
  191. this.title,
  192. this.description,
  193. this.date,
  194. surge
  195. ),
  196. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  197. ),
  198. compareAndWriteFile(
  199. this.span,
  200. withBannerArray(
  201. this.title,
  202. this.description,
  203. this.date,
  204. clash
  205. ),
  206. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  207. ),
  208. compareAndWriteFile(
  209. this.span,
  210. singbox,
  211. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  212. )
  213. ]);
  214. }
  215. }