singbox.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { BaseWriteStrategy } from './base';
  2. import { appendArrayInPlace } from 'foxts/append-array-in-place';
  3. import { noop } from 'foxts/noop';
  4. import { withIdentityContent } from '../misc';
  5. import { fastIpVersion } from 'foxts/fast-ip-version';
  6. import stringify from 'json-stringify-pretty-compact';
  7. import { OUTPUT_SINGBOX_DIR } from '../../constants/dir';
  8. import { MARKER_DOMAIN } from '../../constants/description';
  9. interface SingboxHeadlessRule {
  10. domain: string[],
  11. domain_suffix: string[],
  12. domain_keyword?: string[],
  13. domain_regex?: string[],
  14. source_ip_cidr?: string[],
  15. ip_cidr?: string[],
  16. source_port?: number[],
  17. source_port_range?: string[],
  18. port?: number[],
  19. port_range?: string[],
  20. process_name?: string[],
  21. process_path?: string[],
  22. network?: string[]
  23. }
  24. export interface SingboxSourceFormat {
  25. version: 2 | number & {},
  26. rules: SingboxHeadlessRule[]
  27. }
  28. export class SingboxSource extends BaseWriteStrategy {
  29. public readonly name = 'singbox';
  30. readonly fileExtension = 'json';
  31. static readonly jsonToLines = (json: unknown): string[] => stringify(json).split('\n');
  32. private singbox: SingboxHeadlessRule = {
  33. domain: [MARKER_DOMAIN],
  34. domain_suffix: [MARKER_DOMAIN]
  35. };
  36. protected get result() {
  37. return SingboxSource.jsonToLines({
  38. version: 2,
  39. rules: [this.singbox]
  40. });
  41. }
  42. constructor(
  43. /** Since sing-box only have one format that does not reflect type, we need to specify it */
  44. public type: 'domainset' | 'non_ip' | 'ip' /* | (string & {}) */,
  45. public readonly outputDir = OUTPUT_SINGBOX_DIR
  46. ) {
  47. super(outputDir);
  48. }
  49. withPadding = withIdentityContent;
  50. writeDomain(domain: string): void {
  51. this.singbox.domain.push(domain);
  52. }
  53. writeDomainSuffix(domain: string): void {
  54. this.singbox.domain_suffix.push(domain);
  55. }
  56. writeDomainKeywords(keyword: Set<string>): void {
  57. appendArrayInPlace(
  58. this.singbox.domain_keyword ??= [],
  59. Array.from(keyword)
  60. );
  61. }
  62. writeDomainWildcard(wildcard: string): void {
  63. this.singbox.domain_regex ??= [];
  64. this.singbox.domain_regex.push(SingboxSource.domainWildCardToRegex(wildcard));
  65. }
  66. writeUserAgents = noop;
  67. writeProcessNames(processName: Set<string>): void {
  68. appendArrayInPlace(
  69. this.singbox.process_name ??= [],
  70. Array.from(processName)
  71. );
  72. }
  73. writeProcessPaths(processPath: Set<string>): void {
  74. appendArrayInPlace(
  75. this.singbox.process_path ??= [],
  76. Array.from(processPath)
  77. );
  78. }
  79. writeUrlRegexes = noop;
  80. writeIpCidrs(ipCidr: string[]): void {
  81. appendArrayInPlace(
  82. this.singbox.ip_cidr ??= [],
  83. ipCidr
  84. );
  85. }
  86. writeIpCidr6s(ipCidr6: string[]): void {
  87. appendArrayInPlace(
  88. this.singbox.ip_cidr ??= [],
  89. ipCidr6
  90. );
  91. }
  92. writeGeoip = noop;
  93. writeIpAsns = noop;
  94. writeSourceIpCidrs(sourceIpCidr: string[]): void {
  95. this.singbox.source_ip_cidr ??= [];
  96. for (let i = 0, len = sourceIpCidr.length; i < len; i++) {
  97. const value = sourceIpCidr[i];
  98. if (value.includes('/')) {
  99. this.singbox.source_ip_cidr.push(value);
  100. continue;
  101. }
  102. const v = fastIpVersion(value);
  103. if (v === 4) {
  104. this.singbox.source_ip_cidr.push(`${value}/32`);
  105. continue;
  106. }
  107. if (v === 6) {
  108. this.singbox.source_ip_cidr.push(`${value}/128`);
  109. continue;
  110. }
  111. }
  112. }
  113. writeSourcePorts(port: Set<string>): void {
  114. this.singbox.source_port ??= [];
  115. for (const i of port) {
  116. const tmp = Number(i);
  117. if (!Number.isNaN(tmp)) {
  118. this.singbox.source_port.push(tmp);
  119. }
  120. }
  121. }
  122. writeDestinationPorts(port: Set<string>): void {
  123. this.singbox.port ??= [];
  124. for (const i of port) {
  125. const tmp = Number(i);
  126. if (!Number.isNaN(tmp)) {
  127. this.singbox.port.push(tmp);
  128. }
  129. }
  130. }
  131. writeProtocols(protocol: Set<string>): void {
  132. this.singbox.network ??= [];
  133. // protocol has already be normalized and will only be uppercase
  134. if (protocol.has('UDP')) {
  135. this.singbox.network.push('udp');
  136. }
  137. if (protocol.has('TCP')) {
  138. this.singbox.network.push('tcp');
  139. }
  140. }
  141. writeOtherRules = noop;
  142. }