base.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import { OUTPUT_CLASH_DIR, OUTPUT_MODULES_DIR, OUTPUT_SINGBOX_DIR, OUTPUT_SURGE_DIR } from '../../constants/dir';
  2. import type { Span } from '../../trace';
  3. import { HostnameSmolTrie } from '../trie';
  4. import stringify from 'json-stringify-pretty-compact';
  5. import path from 'node:path';
  6. import { withBannerArray } from '../misc';
  7. import { invariant } from 'foxact/invariant';
  8. import picocolors from 'picocolors';
  9. import fs from 'node:fs';
  10. import { fastStringArrayJoin, writeFile } from '../misc';
  11. import { readFileByLine } from '../fetch-text-by-line';
  12. import { asyncWriteToStream } from '../async-write-to-stream';
  13. export abstract class RuleOutput<TPreprocessed = unknown> {
  14. protected domainTrie = new HostnameSmolTrie(null);
  15. protected domainKeywords = new Set<string>();
  16. protected domainWildcard = new Set<string>();
  17. protected userAgent = new Set<string>();
  18. protected processName = new Set<string>();
  19. protected processPath = new Set<string>();
  20. protected urlRegex = new Set<string>();
  21. protected ipcidr = new Set<string>();
  22. protected ipcidrNoResolve = new Set<string>();
  23. protected ipasn = new Set<string>();
  24. protected ipasnNoResolve = new Set<string>();
  25. protected ipcidr6 = new Set<string>();
  26. protected ipcidr6NoResolve = new Set<string>();
  27. protected geoip = new Set<string>();
  28. protected groipNoResolve = new Set<string>();
  29. protected sourceIpOrCidr = new Set<string>();
  30. protected sourcePort = new Set<string>();
  31. protected destPort = new Set<string>();
  32. protected otherRules: string[] = [];
  33. protected abstract type: 'domainset' | 'non_ip' | 'ip';
  34. private pendingPromise: Promise<void> | null = null;
  35. static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
  36. whitelistDomain = (domain: string) => {
  37. this.domainTrie.whitelist(domain);
  38. return this;
  39. };
  40. static readonly domainWildCardToRegex = (domain: string) => {
  41. let result = '^';
  42. for (let i = 0, len = domain.length; i < len; i++) {
  43. switch (domain[i]) {
  44. case '.':
  45. result += String.raw`\.`;
  46. break;
  47. case '*':
  48. result += '[a-zA-Z0-9-_.]*?';
  49. break;
  50. case '?':
  51. result += '[a-zA-Z0-9-_.]';
  52. break;
  53. default:
  54. result += domain[i];
  55. }
  56. }
  57. result += '$';
  58. return result;
  59. };
  60. constructor(protected readonly span: Span, protected readonly id: string) { }
  61. protected title: string | null = null;
  62. withTitle(title: string) {
  63. this.title = title;
  64. return this;
  65. }
  66. protected description: string[] | readonly string[] | null = null;
  67. withDescription(description: string[] | readonly string[]) {
  68. this.description = description;
  69. return this;
  70. }
  71. protected date = new Date();
  72. withDate(date: Date) {
  73. this.date = date;
  74. return this;
  75. }
  76. addDomain(domain: string) {
  77. this.domainTrie.add(domain);
  78. return this;
  79. }
  80. bulkAddDomain(domains: Array<string | null>) {
  81. let d: string | null;
  82. for (let i = 0, len = domains.length; i < len; i++) {
  83. d = domains[i];
  84. if (d !== null) {
  85. this.addDomain(d);
  86. }
  87. }
  88. return this;
  89. }
  90. addDomainSuffix(domain: string) {
  91. this.domainTrie.add(domain, true);
  92. return this;
  93. }
  94. bulkAddDomainSuffix(domains: string[]) {
  95. for (let i = 0, len = domains.length; i < len; i++) {
  96. this.addDomainSuffix(domains[i]);
  97. }
  98. return this;
  99. }
  100. addDomainKeyword(keyword: string) {
  101. this.domainKeywords.add(keyword);
  102. return this;
  103. }
  104. private async addFromDomainsetPromise(source: AsyncIterable<string> | Iterable<string> | string[]) {
  105. for await (const line of source) {
  106. if (line[0] === '.') {
  107. this.addDomainSuffix(line);
  108. } else {
  109. this.addDomain(line);
  110. }
  111. }
  112. }
  113. addFromDomainset(source: AsyncIterable<string> | Iterable<string> | string[]) {
  114. this.pendingPromise = (this.pendingPromise ||= Promise.resolve()).then(() => this.addFromDomainsetPromise(source));
  115. return this;
  116. }
  117. private async addFromRulesetPromise(source: AsyncIterable<string> | Iterable<string>) {
  118. for await (const line of source) {
  119. const splitted = line.split(',');
  120. const type = splitted[0];
  121. const value = splitted[1];
  122. const arg = splitted[2];
  123. switch (type) {
  124. case 'DOMAIN':
  125. this.addDomain(value);
  126. break;
  127. case 'DOMAIN-SUFFIX':
  128. this.addDomainSuffix(value);
  129. break;
  130. case 'DOMAIN-KEYWORD':
  131. this.addDomainKeyword(value);
  132. break;
  133. case 'DOMAIN-WILDCARD':
  134. this.domainWildcard.add(value);
  135. break;
  136. case 'USER-AGENT':
  137. this.userAgent.add(value);
  138. break;
  139. case 'PROCESS-NAME':
  140. if (value.includes('/') || value.includes('\\')) {
  141. this.processPath.add(value);
  142. } else {
  143. this.processName.add(value);
  144. }
  145. break;
  146. case 'URL-REGEX': {
  147. const [, ...rest] = splitted;
  148. this.urlRegex.add(rest.join(','));
  149. break;
  150. }
  151. case 'IP-CIDR':
  152. (arg === 'no-resolve' ? this.ipcidrNoResolve : this.ipcidr).add(value);
  153. break;
  154. case 'IP-CIDR6':
  155. (arg === 'no-resolve' ? this.ipcidr6NoResolve : this.ipcidr6).add(value);
  156. break;
  157. case 'IP-ASN':
  158. (arg === 'no-resolve' ? this.ipasnNoResolve : this.ipasn).add(value);
  159. break;
  160. case 'GEOIP':
  161. (arg === 'no-resolve' ? this.groipNoResolve : this.geoip).add(value);
  162. break;
  163. case 'SRC-IP':
  164. this.sourceIpOrCidr.add(value);
  165. break;
  166. case 'SRC-PORT':
  167. this.sourcePort.add(value);
  168. break;
  169. case 'DEST-PORT':
  170. this.destPort.add(value);
  171. break;
  172. default:
  173. this.otherRules.push(line);
  174. break;
  175. }
  176. }
  177. }
  178. addFromRuleset(source: AsyncIterable<string> | Iterable<string>) {
  179. this.pendingPromise = (this.pendingPromise ||= Promise.resolve()).then(() => this.addFromRulesetPromise(source));
  180. return this;
  181. }
  182. static readonly ipToCidr = (ip: string, version: 4 | 6 = 4) => {
  183. if (ip.includes('/')) return ip;
  184. if (version === 4) {
  185. return ip + '/32';
  186. }
  187. return ip + '/128';
  188. };
  189. bulkAddCIDR4(cidrs: string[]) {
  190. for (let i = 0, len = cidrs.length; i < len; i++) {
  191. this.ipcidr.add(RuleOutput.ipToCidr(cidrs[i], 4));
  192. }
  193. return this;
  194. }
  195. bulkAddCIDR4NoResolve(cidrs: string[]) {
  196. for (let i = 0, len = cidrs.length; i < len; i++) {
  197. this.ipcidrNoResolve.add(RuleOutput.ipToCidr(cidrs[i], 4));
  198. }
  199. return this;
  200. }
  201. bulkAddCIDR6(cidrs: string[]) {
  202. for (let i = 0, len = cidrs.length; i < len; i++) {
  203. this.ipcidr6.add(RuleOutput.ipToCidr(cidrs[i], 6));
  204. }
  205. return this;
  206. }
  207. bulkAddCIDR6NoResolve(cidrs: string[]) {
  208. for (let i = 0, len = cidrs.length; i < len; i++) {
  209. this.ipcidr6NoResolve.add(RuleOutput.ipToCidr(cidrs[i], 6));
  210. }
  211. return this;
  212. }
  213. protected abstract preprocess(): NonNullable<TPreprocessed>;
  214. async done() {
  215. await this.pendingPromise;
  216. this.pendingPromise = null;
  217. }
  218. private guardPendingPromise() {
  219. // reverse invariant
  220. if (this.pendingPromise !== null) {
  221. console.trace('Pending promise:', this.pendingPromise);
  222. throw new Error('You should call done() before calling this method');
  223. }
  224. }
  225. private $$preprocessed: TPreprocessed | null = null;
  226. get $preprocessed() {
  227. if (this.$$preprocessed === null) {
  228. this.guardPendingPromise();
  229. this.$$preprocessed = this.span.traceChildSync('RuleOutput#preprocess: ' + this.id, () => this.preprocess());
  230. }
  231. return this.$$preprocessed;
  232. }
  233. async writeClash(outputDir?: null | string) {
  234. await this.done();
  235. invariant(this.title, 'Missing title');
  236. invariant(this.description, 'Missing description');
  237. return compareAndWriteFile(
  238. this.span,
  239. withBannerArray(
  240. this.title,
  241. this.description,
  242. this.date,
  243. this.clash()
  244. ),
  245. path.join(outputDir ?? OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  246. );
  247. }
  248. async write(): Promise<void> {
  249. await this.done();
  250. invariant(this.title, 'Missing title');
  251. invariant(this.description, 'Missing description');
  252. const promises = [
  253. compareAndWriteFile(
  254. this.span,
  255. withBannerArray(
  256. this.title,
  257. this.description,
  258. this.date,
  259. this.surge()
  260. ),
  261. path.join(OUTPUT_SURGE_DIR, this.type, this.id + '.conf')
  262. ),
  263. compareAndWriteFile(
  264. this.span,
  265. withBannerArray(
  266. this.title,
  267. this.description,
  268. this.date,
  269. this.clash()
  270. ),
  271. path.join(OUTPUT_CLASH_DIR, this.type, this.id + '.txt')
  272. ),
  273. compareAndWriteFile(
  274. this.span,
  275. this.singbox(),
  276. path.join(OUTPUT_SINGBOX_DIR, this.type, this.id + '.json')
  277. )
  278. ];
  279. if (this.mitmSgmodule) {
  280. const sgmodule = this.mitmSgmodule();
  281. const sgModulePath = this.mitmSgmodulePath ?? path.join(this.type, this.id + '.sgmodule');
  282. if (sgmodule) {
  283. promises.push(
  284. compareAndWriteFile(
  285. this.span,
  286. sgmodule,
  287. path.join(OUTPUT_MODULES_DIR, sgModulePath)
  288. )
  289. );
  290. }
  291. }
  292. await Promise.all(promises);
  293. }
  294. abstract surge(): string[];
  295. abstract clash(): string[];
  296. abstract singbox(): string[];
  297. protected mitmSgmodulePath: string | null = null;
  298. withMitmSgmodulePath(path: string | null) {
  299. if (path) {
  300. this.mitmSgmodulePath = path;
  301. }
  302. return this;
  303. }
  304. abstract mitmSgmodule?(): string[] | null;
  305. }
  306. export async function fileEqual(linesA: string[], source: AsyncIterable<string>): Promise<boolean> {
  307. if (linesA.length === 0) {
  308. return false;
  309. }
  310. let index = -1;
  311. for await (const lineB of source) {
  312. index++;
  313. if (index > linesA.length - 1) {
  314. return (index === linesA.length && lineB === '');
  315. }
  316. const lineA = linesA[index];
  317. if (lineA[0] === '#' && lineB[0] === '#') {
  318. continue;
  319. }
  320. if (
  321. lineA[0] === '/'
  322. && lineA[1] === '/'
  323. && lineB[0] === '/'
  324. && lineB[1] === '/'
  325. && lineA[3] === '#'
  326. && lineB[3] === '#'
  327. ) {
  328. continue;
  329. }
  330. if (lineA !== lineB) {
  331. return false;
  332. }
  333. }
  334. // The file becomes larger
  335. return !(index < linesA.length - 1);
  336. }
  337. export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
  338. let isEqual = true;
  339. const linesALen = linesA.length;
  340. if (fs.existsSync(filePath)) {
  341. isEqual = await fileEqual(linesA, readFileByLine(filePath));
  342. } else {
  343. console.log(`${filePath} does not exists, writing...`);
  344. isEqual = false;
  345. }
  346. if (isEqual) {
  347. console.log(picocolors.gray(picocolors.dim(`same content, bail out writing: ${filePath}`)));
  348. return;
  349. }
  350. await span.traceChildAsync(`writing ${filePath}`, async () => {
  351. // The default highwater mark is normally 16384,
  352. // So we make sure direct write to file if the content is
  353. // most likely less than 500 lines
  354. if (linesALen < 500) {
  355. return writeFile(filePath, fastStringArrayJoin(linesA, '\n') + '\n');
  356. }
  357. const writeStream = fs.createWriteStream(filePath);
  358. for (let i = 0; i < linesALen; i++) {
  359. const p = asyncWriteToStream(writeStream, linesA[i] + '\n');
  360. // eslint-disable-next-line no-await-in-loop -- stream high water mark
  361. if (p) await p;
  362. }
  363. writeStream.end();
  364. });
  365. }