domainset.ts 4.4 KB

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