singbox.ts 3.7 KB

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