domainset.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { createRetrieKeywordFilter as createKeywordFilter } from 'foxts/retrie';
  2. import { RuleOutput } from './base';
  3. import type { SingboxSourceFormat } from '../singbox';
  4. import { escapeStringRegexp } from 'foxts/escape-string-regexp';
  5. export class DomainsetOutput extends RuleOutput<string[]> {
  6. protected type = 'domainset' as const;
  7. private $surge: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  8. private $clash: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  9. private $singbox_domains: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  10. private $singbox_domains_suffixes: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  11. private $adguardhome: string[] = [];
  12. preprocess() {
  13. const kwfilter = createKeywordFilter(Array.from(this.domainKeywords));
  14. this.domainTrie.dumpWithoutDot((domain, subdomain) => {
  15. if (kwfilter(domain)) {
  16. return;
  17. }
  18. this.$surge.push(subdomain ? '.' + domain : domain);
  19. this.$clash.push(subdomain ? `+.${domain}` : domain);
  20. (subdomain ? this.$singbox_domains_suffixes : this.$singbox_domains).push(domain);
  21. this.$adguardhome.push(subdomain ? `||${domain}^` : `|${domain}^`);
  22. }, true);
  23. return this.$surge;
  24. }
  25. surge(): string[] {
  26. this.runPreprocess();
  27. return this.$surge;
  28. }
  29. clash(): string[] {
  30. this.runPreprocess();
  31. return this.$clash;
  32. }
  33. singbox(): string[] {
  34. this.runPreprocess();
  35. return RuleOutput.jsonToLines({
  36. version: 2,
  37. rules: [{
  38. domain: this.$singbox_domains,
  39. domain_suffix: this.$singbox_domains_suffixes
  40. }]
  41. } satisfies SingboxSourceFormat);
  42. }
  43. mitmSgmodule = undefined;
  44. adguardhome(): string[] {
  45. this.runPreprocess();
  46. // const whitelistArray = sortDomains(Array.from(whitelist));
  47. // for (let i = 0, len = whitelistArray.length; i < len; i++) {
  48. // const domain = whitelistArray[i];
  49. // if (domain[0] === '.') {
  50. // results.push(`@@||${domain.slice(1)}^`);
  51. // } else {
  52. // results.push(`@@|${domain}^`);
  53. // }
  54. // }
  55. for (const wildcard of this.domainWildcard) {
  56. const processed = wildcard.replaceAll('?', '*');
  57. if (processed.startsWith('*.')) {
  58. this.$adguardhome.push(`||${processed.slice(2)}^`);
  59. } else {
  60. this.$adguardhome.push(`|${processed}^`);
  61. }
  62. }
  63. for (const keyword of this.domainKeywords) {
  64. // Use regex to match keyword
  65. this.$adguardhome.push(`/${escapeStringRegexp(keyword)}/`);
  66. }
  67. for (const ipGroup of [this.ipcidr, this.ipcidrNoResolve]) {
  68. for (const ipcidr of ipGroup) {
  69. if (ipcidr.endsWith('/32')) {
  70. this.$adguardhome.push(`||${ipcidr.slice(0, -3)}`);
  71. /* else if (ipcidr.endsWith('.0/24')) {
  72. results.push(`||${ipcidr.slice(0, -6)}.*`);
  73. } */
  74. } else {
  75. this.$adguardhome.push(`||${ipcidr}^`);
  76. }
  77. }
  78. }
  79. for (const ipGroup of [this.ipcidr6, this.ipcidr6NoResolve]) {
  80. for (const ipcidr of ipGroup) {
  81. if (ipcidr.endsWith('/128')) {
  82. this.$adguardhome.push(`||${ipcidr.slice(0, -4)}`);
  83. } else {
  84. this.$adguardhome.push(`||${ipcidr}`);
  85. }
  86. }
  87. }
  88. return this.$adguardhome;
  89. }
  90. }