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 'foxts/escape-string-regexp';
  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. private $adguardhome: string[] = [];
  16. preprocess() {
  17. const kwfilter = createKeywordFilter(this.domainKeywords);
  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_suffixes : this.$singbox_domains).push(domain);
  25. if (subdomain) {
  26. this.$adguardhome.push(`||${domain}^`);
  27. } else {
  28. this.$adguardhome.push(`|${domain}^`);
  29. }
  30. }, true);
  31. return this.$surge;
  32. }
  33. surge(): string[] {
  34. this.runPreprocess();
  35. return this.$surge;
  36. }
  37. clash(): string[] {
  38. this.runPreprocess();
  39. return this.$clash;
  40. }
  41. singbox(): string[] {
  42. this.runPreprocess();
  43. return RuleOutput.jsonToLines({
  44. version: 2,
  45. rules: [{
  46. domain: this.$singbox_domains,
  47. domain_suffix: this.$singbox_domains_suffixes
  48. }]
  49. } satisfies SingboxSourceFormat);
  50. }
  51. protected apexDomainMap: Map<string, string> | null = null;
  52. getStatMap() {
  53. this.runPreprocess();
  54. invariant(this.$preprocessed, 'Non dumped yet');
  55. if (!this.apexDomainMap) {
  56. const domainMap = new Map<string, string>();
  57. for (let i = 0, len = this.$preprocessed.length; i < len; i++) {
  58. const cur = this.$preprocessed[i];
  59. if (!domainMap.has(cur)) {
  60. const domain = tldts.getDomain(cur, looseTldtsOpt);
  61. domainMap.set(cur, domain ?? cur);
  62. }
  63. }
  64. this.apexDomainMap = domainMap;
  65. }
  66. return Array.from(this.$preprocessed
  67. .reduce<Map<string, number>>(
  68. (acc, cur) => {
  69. const suffix = this.apexDomainMap!.get(cur);
  70. if (suffix) {
  71. acc.set(suffix, (acc.get(suffix) ?? 0) + 1);
  72. }
  73. return acc;
  74. },
  75. new Map()
  76. )
  77. .entries())
  78. .filter(a => a[1] > 9)
  79. .sort((a, b) => (b[1] - a[1]) || fastStringCompare(a[0], b[0]))
  80. .map(([domain, count]) => `${domain}${' '.repeat(100 - domain.length)}${count}`);
  81. }
  82. mitmSgmodule = undefined;
  83. adguardhome(): string[] {
  84. this.runPreprocess();
  85. // const whitelistArray = sortDomains(Array.from(whitelist));
  86. // for (let i = 0, len = whitelistArray.length; i < len; i++) {
  87. // const domain = whitelistArray[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. this.$adguardhome.push(`||${processed.slice(2)}^`);
  98. } else {
  99. this.$adguardhome.push(`|${processed}^`);
  100. }
  101. }
  102. for (const keyword of this.domainKeywords) {
  103. // Use regex to match keyword
  104. this.$adguardhome.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. this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
  110. /* else if (ipcidr.endsWith('.0/24')) {
  111. results.push(`||${ipcidr.slice(0, -6)}.*`);
  112. } */
  113. } else {
  114. this.$adguardhome.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. this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
  122. } else {
  123. this.$adguardhome.push(`||${ipcidr}`);
  124. }
  125. }
  126. }
  127. return this.$adguardhome;
  128. }
  129. }