domainset.ts 4.3 KB

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