clash.ts 5.6 KB

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