singbox.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import picocolors from 'picocolors';
  2. import { domainWildCardToRegex } from './misc';
  3. import { isProbablyIpv4, isProbablyIpv6 } from './is-fast-ip';
  4. const unsupported = Symbol('unsupported');
  5. // https://sing-box.sagernet.org/configuration/rule-set/source-format/
  6. const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => [key: keyof SingboxHeadlessRule, value: string] | null) | typeof unsupported> = {
  7. DOMAIN: (_1, _2, value) => ['domain', value],
  8. 'DOMAIN-SUFFIX': (_1, _2, value) => ['domain_suffix', value],
  9. 'DOMAIN-KEYWORD': (_1, _2, value) => ['domain_keyword', value],
  10. 'DOMAIN-WILDCARD': (_1, _2, value) => ['domain_regex', domainWildCardToRegex(value)],
  11. GEOIP: unsupported,
  12. 'IP-CIDR': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],
  13. 'IP-CIDR6': (_1, _2, value) => ['ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],
  14. 'IP-ASN': unsupported,
  15. 'SRC-IP': (_1, _2, value) => {
  16. if (value.includes('/')) {
  17. return ['source_ip_cidr', value];
  18. }
  19. if (isProbablyIpv4(value)) {
  20. return ['source_ip_cidr', value + '/32'];
  21. }
  22. if (isProbablyIpv6(value)) {
  23. return ['source_ip_cidr', value + '/128'];
  24. }
  25. return null;
  26. },
  27. 'SRC-IP-CIDR': (_1, _2, value) => ['source_ip_cidr', value.endsWith(',no-resolve') ? value.slice(0, -11) : value],
  28. 'SRC-PORT': (_1, _2, value) => ['source_port', value],
  29. 'DST-PORT': (_1, _2, value) => ['port', value],
  30. 'PROCESS-NAME': (_1, _2, value) => ['process_name', value],
  31. 'PROCESS-PATH': (_1, _2, value) => ['process_path', value],
  32. 'DEST-PORT': (_1, _2, value) => ['port', value],
  33. 'IN-PORT': (_1, _2, value) => ['source_port', value],
  34. 'URL-REGEX': unsupported,
  35. 'USER-AGENT': unsupported
  36. };
  37. interface SingboxHeadlessRule {
  38. domain?: string[],
  39. domain_suffix?: string[],
  40. domain_keyword?: string[],
  41. domain_regex?: string[],
  42. source_ip_cidr?: string[],
  43. ip_cidr?: string[],
  44. source_port?: string[],
  45. source_port_range?: string[],
  46. port?: string[],
  47. port_range?: string[],
  48. process_name?: string[],
  49. process_path?: string[]
  50. }
  51. interface SingboxSourceFormat {
  52. version: 2 | number & {},
  53. rules: SingboxHeadlessRule[]
  54. }
  55. export const surgeRulesetToSingbox = (rules: string[] | Set<string>): SingboxSourceFormat => {
  56. const rule: SingboxHeadlessRule = Array.from(rules).reduce<SingboxHeadlessRule>((acc, cur) => {
  57. let buf = '';
  58. let type = '';
  59. let i = 0;
  60. for (const len = cur.length; i < len; i++) {
  61. if (cur[i] === ',') {
  62. type = buf;
  63. break;
  64. }
  65. buf += cur[i];
  66. }
  67. if (type === '') {
  68. return acc;
  69. }
  70. const value = cur.slice(i + 1);
  71. if (type in PROCESSOR) {
  72. const proc = PROCESSOR[type];
  73. if (proc !== unsupported) {
  74. const r = proc(cur, type, value);
  75. if (r) {
  76. const [k, v] = r;
  77. acc[k] ||= [];
  78. acc[k].push(v);
  79. }
  80. }
  81. } else {
  82. console.log(picocolors.yellow(`[sing-box] unknown rule type: ${type}`), cur);
  83. }
  84. return acc;
  85. }, {});
  86. return {
  87. version: 2,
  88. rules: [rule]
  89. };
  90. };
  91. export const surgeDomainsetToSingbox = (domainset: string[]) => {
  92. const rule = domainset.reduce((acc, cur) => {
  93. if (cur[0] === '.') {
  94. acc.domain_suffix.push(cur.slice(1));
  95. } else {
  96. acc.domain.push(cur);
  97. }
  98. return acc;
  99. }, { domain: [] as string[], domain_suffix: [] as string[] } satisfies SingboxHeadlessRule);
  100. return {
  101. version: 2,
  102. rules: [rule]
  103. };
  104. };
  105. export const ipCidrListToSingbox = (ipCidrList: string[]): SingboxSourceFormat => {
  106. return {
  107. version: 2,
  108. rules: [{
  109. ip_cidr: ipCidrList
  110. }]
  111. };
  112. };