clash.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import { appendSetElementsToArray } from 'foxts/append-set-elements-to-array';
  2. import { BaseWriteStrategy } from './base';
  3. import { noop } from 'foxts/noop';
  4. import { fastIpVersion, notSupported, withBannerArray } from '../misc';
  5. import { OUTPUT_CLASH_DIR } from '../../constants/dir';
  6. import { appendArrayInPlace } from '../append-array-in-place';
  7. export class ClashDomainSet extends BaseWriteStrategy {
  8. public readonly name = 'clash domainset';
  9. // readonly type = 'domainset';
  10. readonly fileExtension = 'txt';
  11. readonly type = 'domainset';
  12. protected result: string[] = ['this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  13. constructor(public readonly outputDir = OUTPUT_CLASH_DIR) {
  14. super(outputDir);
  15. }
  16. withPadding = withBannerArray;
  17. writeDomain(domain: string): void {
  18. this.result.push(domain);
  19. }
  20. writeDomainSuffix(domain: string): void {
  21. this.result.push('+.' + domain);
  22. }
  23. writeDomainKeywords = noop;
  24. writeDomainWildcards = noop;
  25. writeUserAgents = noop;
  26. writeProcessNames = noop;
  27. writeProcessPaths = noop;
  28. writeUrlRegexes = noop;
  29. writeIpCidrs = noop;
  30. writeIpCidr6s = noop;
  31. writeGeoip = noop;
  32. writeIpAsns = noop;
  33. writeSourceIpCidrs = noop;
  34. writeSourcePorts = noop;
  35. writeDestinationPorts = noop;
  36. writeOtherRules = noop;
  37. }
  38. export class ClashIPSet extends BaseWriteStrategy {
  39. public readonly name = 'clash ipcidr';
  40. // readonly type = 'domainset';
  41. readonly fileExtension = 'txt';
  42. readonly type = 'ip';
  43. protected result: string[] = [];
  44. constructor(public readonly outputDir = OUTPUT_CLASH_DIR) {
  45. super(outputDir);
  46. }
  47. withPadding = withBannerArray;
  48. writeDomain = notSupported('writeDomain');
  49. writeDomainSuffix = notSupported('writeDomainSuffix');
  50. writeDomainKeywords = notSupported('writeDomainKeywords');
  51. writeDomainWildcards = notSupported('writeDomainWildcards');
  52. writeUserAgents = notSupported('writeUserAgents');
  53. writeProcessNames = notSupported('writeProcessNames');
  54. writeProcessPaths = notSupported('writeProcessPaths');
  55. writeUrlRegexes = notSupported('writeUrlRegexes');
  56. writeIpCidrs(ipCidr: string[]): void {
  57. appendArrayInPlace(this.result, ipCidr);
  58. }
  59. writeIpCidr6s(ipCidr6: string[]): void {
  60. appendArrayInPlace(this.result, ipCidr6);
  61. }
  62. writeGeoip = notSupported('writeGeoip');
  63. writeIpAsns = notSupported('writeIpAsns');
  64. writeSourceIpCidrs = notSupported('writeSourceIpCidrs');
  65. writeSourcePorts = notSupported('writeSourcePorts');
  66. writeDestinationPorts = noop;
  67. writeOtherRules = noop;
  68. }
  69. export class ClashClassicRuleSet extends BaseWriteStrategy {
  70. public readonly name: string = 'clash classic ruleset';
  71. readonly fileExtension = 'txt';
  72. protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  73. constructor(public readonly type: 'ip' | 'non_ip' /* | (string & {}) */, public readonly outputDir = OUTPUT_CLASH_DIR) {
  74. super(outputDir);
  75. }
  76. withPadding = withBannerArray;
  77. writeDomain(domain: string): void {
  78. this.result.push('DOMAIN,' + domain);
  79. }
  80. writeDomainSuffix(domain: string): void {
  81. this.result.push('DOMAIN-SUFFIX,' + domain);
  82. }
  83. writeDomainKeywords(keyword: Set<string>): void {
  84. appendSetElementsToArray(this.result, keyword, i => `DOMAIN-KEYWORD,${i}`);
  85. }
  86. writeDomainWildcards(wildcard: Set<string>): void {
  87. appendSetElementsToArray(this.result, wildcard, i => `DOMAIN-REGEX,${ClashClassicRuleSet.domainWildCardToRegex(i)}`);
  88. }
  89. writeUserAgents = noop;
  90. writeProcessNames(processName: Set<string>): void {
  91. appendSetElementsToArray(this.result, processName, i => `PROCESS-NAME,${i}`);
  92. }
  93. writeProcessPaths(processPath: Set<string>): void {
  94. appendSetElementsToArray(this.result, processPath, i => `PROCESS-PATH,${i}`);
  95. }
  96. writeUrlRegexes = noop;
  97. writeIpCidrs(ipCidr: string[], noResolve: boolean): void {
  98. for (let i = 0, len = ipCidr.length; i < len; i++) {
  99. this.result.push(`IP-CIDR,${ipCidr[i]}${noResolve ? ',no-resolve' : ''}`);
  100. }
  101. }
  102. writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void {
  103. for (let i = 0, len = ipCidr6.length; i < len; i++) {
  104. this.result.push(`IP-CIDR6,${ipCidr6[i]}${noResolve ? ',no-resolve' : ''}`);
  105. }
  106. }
  107. writeGeoip(geoip: Set<string>, noResolve: boolean): void {
  108. appendSetElementsToArray(this.result, geoip, i => `GEOIP,${i}${noResolve ? ',no-resolve' : ''}`);
  109. }
  110. writeIpAsns(asns: Set<string>, noResolve: boolean): void {
  111. appendSetElementsToArray(this.result, asns, i => `IP-ASN,${i}${noResolve ? ',no-resolve' : ''}`);
  112. }
  113. writeSourceIpCidrs(sourceIpCidr: string[]): void {
  114. for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
  115. const value = sourceIpCidr[i];
  116. if (value.includes('/')) {
  117. this.result.push(`SRC-IP-CIDR,${value}`);
  118. continue;
  119. }
  120. const v = fastIpVersion(value);
  121. if (v === 4) {
  122. this.result.push(`SRC-IP-CIDR,${value}/32`);
  123. continue;
  124. }
  125. if (v === 6) {
  126. this.result.push(`SRC-IP-CIDR6,${value}/128`);
  127. continue;
  128. }
  129. }
  130. }
  131. writeSourcePorts(port: Set<string>): void {
  132. appendSetElementsToArray(this.result, port, i => `SRC-PORT,${i}`);
  133. }
  134. writeDestinationPorts(port: Set<string>): void {
  135. appendSetElementsToArray(this.result, port, i => `DST-PORT,${i}`);
  136. }
  137. writeOtherRules = noop;
  138. }