domainset.ts 4.5 KB

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