clash.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 'foxts/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. writeProtocols = noop;
  37. writeOtherRules = noop;
  38. }
  39. export class ClashIPSet extends BaseWriteStrategy {
  40. public readonly name = 'clash ipcidr';
  41. // readonly type = 'domainset';
  42. readonly fileExtension = 'txt';
  43. readonly type = 'ip';
  44. protected result: string[] = [];
  45. constructor(public readonly outputDir = OUTPUT_CLASH_DIR) {
  46. super(outputDir);
  47. }
  48. withPadding = withBannerArray;
  49. writeDomain = notSupported('writeDomain');
  50. writeDomainSuffix = notSupported('writeDomainSuffix');
  51. writeDomainKeywords = notSupported('writeDomainKeywords');
  52. writeDomainWildcards = notSupported('writeDomainWildcards');
  53. writeUserAgents = notSupported('writeUserAgents');
  54. writeProcessNames = notSupported('writeProcessNames');
  55. writeProcessPaths = notSupported('writeProcessPaths');
  56. writeUrlRegexes = notSupported('writeUrlRegexes');
  57. writeIpCidrs(ipCidr: string[]): void {
  58. appendArrayInPlace(this.result, ipCidr);
  59. }
  60. writeIpCidr6s(ipCidr6: string[]): void {
  61. appendArrayInPlace(this.result, ipCidr6);
  62. }
  63. writeGeoip = notSupported('writeGeoip');
  64. writeIpAsns = notSupported('writeIpAsns');
  65. writeSourceIpCidrs = notSupported('writeSourceIpCidrs');
  66. writeSourcePorts = notSupported('writeSourcePorts');
  67. writeDestinationPorts = noop;
  68. writeProtocols = noop;
  69. writeOtherRules = noop;
  70. }
  71. export class ClashClassicRuleSet extends BaseWriteStrategy {
  72. public readonly name: string = 'clash classic ruleset';
  73. readonly fileExtension = 'txt';
  74. protected result: string[] = ['DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'];
  75. constructor(public readonly type: 'ip' | 'non_ip' /* | (string & {}) */, public readonly outputDir = OUTPUT_CLASH_DIR) {
  76. super(outputDir);
  77. }
  78. withPadding = withBannerArray;
  79. writeDomain(domain: string): void {
  80. this.result.push('DOMAIN,' + domain);
  81. }
  82. writeDomainSuffix(domain: string): void {
  83. this.result.push('DOMAIN-SUFFIX,' + domain);
  84. }
  85. writeDomainKeywords(keyword: Set<string>): void {
  86. appendSetElementsToArray(this.result, keyword, i => `DOMAIN-KEYWORD,${i}`);
  87. }
  88. writeDomainWildcards(wildcard: Set<string>): void {
  89. appendSetElementsToArray(this.result, wildcard, i => `DOMAIN-REGEX,${ClashClassicRuleSet.domainWildCardToRegex(i)}`);
  90. }
  91. writeUserAgents = noop;
  92. writeProcessNames(processName: Set<string>): void {
  93. appendSetElementsToArray(this.result, processName, i => `PROCESS-NAME,${i}`);
  94. }
  95. writeProcessPaths(processPath: Set<string>): void {
  96. appendSetElementsToArray(this.result, processPath, i => `PROCESS-PATH,${i}`);
  97. }
  98. writeUrlRegexes = noop;
  99. writeIpCidrs(ipCidr: string[], noResolve: boolean): void {
  100. for (let i = 0, len = ipCidr.length; i < len; i++) {
  101. this.result.push(`IP-CIDR,${ipCidr[i]}${noResolve ? ',no-resolve' : ''}`);
  102. }
  103. }
  104. writeIpCidr6s(ipCidr6: string[], noResolve: boolean): void {
  105. for (let i = 0, len = ipCidr6.length; i < len; i++) {
  106. this.result.push(`IP-CIDR6,${ipCidr6[i]}${noResolve ? ',no-resolve' : ''}`);
  107. }
  108. }
  109. writeGeoip(geoip: Set<string>, noResolve: boolean): void {
  110. appendSetElementsToArray(this.result, geoip, i => `GEOIP,${i}${noResolve ? ',no-resolve' : ''}`);
  111. }
  112. writeIpAsns(asns: Set<string>, noResolve: boolean): void {
  113. appendSetElementsToArray(this.result, asns, i => `IP-ASN,${i}${noResolve ? ',no-resolve' : ''}`);
  114. }
  115. writeSourceIpCidrs(sourceIpCidr: string[]): void {
  116. for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
  117. const value = sourceIpCidr[i];
  118. if (value.includes('/')) {
  119. this.result.push(`SRC-IP-CIDR,${value}`);
  120. continue;
  121. }
  122. const v = fastIpVersion(value);
  123. if (v === 4) {
  124. this.result.push(`SRC-IP-CIDR,${value}/32`);
  125. continue;
  126. }
  127. if (v === 6) {
  128. this.result.push(`SRC-IP-CIDR6,${value}/128`);
  129. continue;
  130. }
  131. }
  132. }
  133. writeSourcePorts(port: Set<string>): void {
  134. appendSetElementsToArray(this.result, port, i => `SRC-PORT,${i}`);
  135. }
  136. writeDestinationPorts(port: Set<string>): void {
  137. appendSetElementsToArray(this.result, port, i => `DST-PORT,${i}`);
  138. }
  139. writeProtocols(protocol: Set<string>): void {
  140. // Mihomo only matches UDP/TCP: https://wiki.metacubex.one/en/config/rules/#network
  141. // protocol has already be normalized and will only contain upppercase
  142. if (protocol.has('UDP')) {
  143. this.result.push('NETWORK,UDP');
  144. }
  145. if (protocol.has('TCP')) {
  146. this.result.push('NETWORK,TCP');
  147. }
  148. }
  149. writeOtherRules = noop;
  150. }