domainset.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { invariant } from 'foxts/guard';
  2. import { createAhoCorasick as createKeywordFilter } from 'foxts/ahocorasick';
  3. import { RuleOutput } from './base';
  4. import type { SingboxSourceFormat } from '../singbox';
  5. import * as tldts from 'tldts-experimental';
  6. import { looseTldtsOpt } from '../../constants/loose-tldts-opt';
  7. import { fastStringCompare } from '../misc';
  8. import escapeStringRegexp from 'escape-string-regexp-node';
  9. export class DomainsetOutput extends RuleOutput<string[]> {
  10. protected type = 'domainset' as const;
  11. private $surge: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  12. private $clash: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  13. private $singbox_domains: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  14. private $singbox_domains_suffixes: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  15. preprocess() {
  16. const kwfilter = createKeywordFilter(this.domainKeywords);
  17. const results: string[] = [];
  18. this.domainTrie.dumpWithoutDot((domain, subdomain) => {
  19. if (kwfilter(domain)) {
  20. return;
  21. }
  22. this.$surge.push(subdomain ? '.' + domain : domain);
  23. this.$clash.push(subdomain ? `+.${domain}` : domain);
  24. (subdomain ? this.$singbox_domains : this.$singbox_domains_suffixes).push(domain);
  25. results.push(domain);
  26. }, true);
  27. return results;
  28. }
  29. surge(): string[] {
  30. return this.$surge;
  31. }
  32. clash(): string[] {
  33. return this.$clash;
  34. }
  35. singbox(): string[] {
  36. return RuleOutput.jsonToLines({
  37. version: 2,
  38. rules: [{
  39. domain: this.$singbox_domains,
  40. domain_suffix: this.$singbox_domains_suffixes
  41. }]
  42. } satisfies SingboxSourceFormat);
  43. }
  44. protected apexDomainMap: Map<string, string> | null = null;
  45. getStatMap() {
  46. invariant(this.$preprocessed, 'Non dumped yet');
  47. if (!this.apexDomainMap) {
  48. const domainMap = new Map<string, string>();
  49. for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
  50. const cur = this.$preprocessed[i];
  51. if (!domainMap.has(cur)) {
  52. const domain = tldts.getDomain(cur, looseTldtsOpt);
  53. domainMap.set(cur, domain ?? cur);
  54. }
  55. }
  56. this.apexDomainMap = domainMap;
  57. }
  58. return Array.from(this.$preprocessed
  59. .reduce<Map<string, number>>(
  60. (acc, cur) => {
  61. const suffix = this.apexDomainMap!.get(cur);
  62. if (suffix) {
  63. acc.set(suffix, (acc.get(suffix) ?? 0) + 1);
  64. }
  65. return acc;
  66. },
  67. new Map()
  68. )
  69. .entries())
  70. .filter(a => a[1] > 9)
  71. .sort((a, b) => (b[1] - a[1]) || fastStringCompare(a[0], b[0]))
  72. .map(([domain, count]) => `${domain}${' '.repeat(100 - domain.length)}${count}`);
  73. }
  74. mitmSgmodule = undefined;
  75. adguardhome(): string[] {
  76. const results: string[] = [];
  77. // const whitelistArray = sortDomains(Array.from(whitelist));
  78. // for (let i = 0, len = whitelistArray.length; i < len; i++) {
  79. // const domain = whitelistArray[i];
  80. // if (domain[0] === '.') {
  81. // results.push(`@@||${domain.slice(1)}^`);
  82. // } else {
  83. // results.push(`@@|${domain}^`);
  84. // }
  85. // }
  86. for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
  87. const domain = this.$preprocessed[i];
  88. if (domain[0] === '.') {
  89. results.push(`||${domain.slice(1)}^`);
  90. } else {
  91. results.push(`|${domain}^`);
  92. }
  93. }
  94. for (const wildcard of this.domainWildcard) {
  95. const processed = wildcard.replaceAll('?', '*');
  96. if (processed.startsWith('*.')) {
  97. results.push(`||${processed.slice(2)}^`);
  98. } else {
  99. results.push(`|${processed}^`);
  100. }
  101. }
  102. for (const keyword of this.domainKeywords) {
  103. // Use regex to match keyword
  104. results.push(`/${escapeStringRegexp(keyword)}/`);
  105. }
  106. for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
  107. for (const ipcidr of ipGroup) {
  108. if (ipcidr.endsWith('/32')) {
  109. results.push(`||${ipcidr.slice(0, -3)}`);
  110. /* else if (ipcidr.endsWith('.0/24')) {
  111. results.push(`||${ipcidr.slice(0, -6)}.*`);
  112. } */
  113. } else {
  114. results.push(`||${ipcidr}^`);
  115. }
  116. }
  117. }
  118. for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
  119. for (const ipcidr of ipGroup) {
  120. if (ipcidr.endsWith('/128')) {
  121. results.push(`||${ipcidr.slice(0, -4)}`);
  122. } else {
  123. results.push(`||${ipcidr}`);
  124. }
  125. }
  126. }
  127. return results;
  128. }
  129. }