subscription.test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { describe, it, expect } from 'vitest';
  2. import {
  3. buildSubscriptionUrls,
  4. buildShareLinks,
  5. buildBase64Subscription,
  6. buildJsonSubscription,
  7. type SubClient,
  8. type SubUrlInput,
  9. } from './subscription';
  10. import { base64ToText } from './base64';
  11. import { parseLink } from './links';
  12. describe('buildSubscriptionUrls', () => {
  13. const base: SubUrlInput = {
  14. scheme: 'http',
  15. host: 'sub.example.com',
  16. port: 2096,
  17. subPath: '/sub/',
  18. jsonPath: '/json/',
  19. subId: 'ABC',
  20. };
  21. it('builds the sub and json URLs with the port', () => {
  22. const u = buildSubscriptionUrls(base);
  23. expect(u.base64).toBe('http://sub.example.com:2096/sub/ABC');
  24. expect(u.json).toBe('http://sub.example.com:2096/json/ABC');
  25. });
  26. it('omits the port behind a reverse proxy', () => {
  27. const u = buildSubscriptionUrls({
  28. ...base,
  29. scheme: 'https',
  30. host: 'example.com',
  31. subPath: '/sub-xxx/',
  32. behindProxy: true,
  33. });
  34. expect(u.base64).toBe('https://example.com/sub-xxx/ABC');
  35. expect(u.json).toBe('https://example.com/json/ABC');
  36. });
  37. it('normalizes a path missing its slashes', () => {
  38. const u = buildSubscriptionUrls({ ...base, subPath: 'sub' });
  39. expect(u.base64).toBe('http://sub.example.com:2096/sub/ABC');
  40. });
  41. it('returns empty strings for an empty subId', () => {
  42. const u = buildSubscriptionUrls({ ...base, subId: '' });
  43. expect(u.base64).toBe('');
  44. expect(u.json).toBe('');
  45. });
  46. });
  47. const vlessClient: SubClient = {
  48. protocol: 'vless',
  49. remark: 'HK-01',
  50. address: 'a.example.com',
  51. port: 443,
  52. id: '11111111-2222-3333-4444-555555555555',
  53. flow: 'xtls-rprx-vision',
  54. network: 'tcp',
  55. security: 'reality',
  56. sni: 'www.microsoft.com',
  57. publicKey: 'PBK',
  58. shortId: 'ab',
  59. fingerprint: 'chrome',
  60. };
  61. describe('buildShareLinks', () => {
  62. it('builds a vless link that round-trips through parseLink', () => {
  63. const [link] = buildShareLinks([vlessClient]);
  64. const parsed = parseLink(link);
  65. expect(parsed.protocol).toBe('vless');
  66. expect(parsed.address).toBe('a.example.com');
  67. expect(parsed.port).toBe(443);
  68. expect(parsed.credential).toBe('11111111-2222-3333-4444-555555555555');
  69. expect(parsed.params.security).toBe('reality');
  70. expect(parsed.params['support-x25519mlkem768']).toBe('true');
  71. expect(parsed.name).toBe('HK-01');
  72. });
  73. });
  74. describe('buildBase64Subscription', () => {
  75. it('decodes back to the newline-joined links', () => {
  76. const trojan: SubClient = {
  77. protocol: 'trojan',
  78. remark: 'T',
  79. address: 'b.example.com',
  80. port: 443,
  81. password: 'pw',
  82. network: 'ws',
  83. security: 'tls',
  84. path: '/x',
  85. sni: 'b.example.com',
  86. };
  87. const body = buildBase64Subscription([vlessClient, trojan]);
  88. const decoded = base64ToText(body);
  89. expect(decoded.split('\n')).toEqual(buildShareLinks([vlessClient, trojan]));
  90. });
  91. it('returns empty for no clients', () => {
  92. expect(buildBase64Subscription([])).toBe('');
  93. });
  94. });
  95. describe('buildJsonSubscription', () => {
  96. it('emits a single object for one client with a FLAT proxy outbound (settings.id, no vnext)', () => {
  97. const cfg = JSON.parse(buildJsonSubscription([vlessClient]));
  98. expect(Array.isArray(cfg)).toBe(false);
  99. const proxy = cfg.outbounds[0];
  100. expect(proxy.protocol).toBe('vless');
  101. expect(proxy.tag).toBe('proxy');
  102. expect(proxy.settings.id).toBe(vlessClient.id);
  103. expect('vnext' in proxy.settings).toBe(false);
  104. expect(proxy.settings.level).toBe(8);
  105. expect(proxy.settings.flow).toBe('xtls-rprx-vision');
  106. // the default.json skeleton outbounds are preserved after the proxy
  107. expect(cfg.outbounds.some((o: { tag: string }) => o.tag === 'direct')).toBe(true);
  108. expect(cfg.outbounds.some((o: { tag: string }) => o.tag === 'block')).toBe(true);
  109. // sockopt is stripped from JSON-sub streamSettings
  110. expect(JSON.stringify(proxy.streamSettings)).not.toContain('sockopt');
  111. expect(cfg.remarks).toBe('HK-01');
  112. });
  113. it('keeps the Mihomo-only ML-KEM hint out of the Xray realitySettings', () => {
  114. const cfg = JSON.parse(buildJsonSubscription([vlessClient]));
  115. expect(cfg.outbounds[0].streamSettings.realitySettings.publicKey).toBe(vlessClient.publicKey);
  116. expect(cfg.outbounds[0].streamSettings.realitySettings).not.toHaveProperty(
  117. 'supportX25519Mlkem768',
  118. );
  119. });
  120. it('uses the iOS-compatible SOCKS inbound while preserving the mixed tag and HTTP inbound', () => {
  121. const cfg = JSON.parse(buildJsonSubscription([vlessClient]));
  122. const socks = cfg.inbounds.find((inbound: { port: number }) => inbound.port === 10808);
  123. const http = cfg.inbounds.find((inbound: { port: number }) => inbound.port === 10809);
  124. expect(socks).toMatchObject({
  125. listen: '127.0.0.1',
  126. protocol: 'socks',
  127. tag: 'mixed',
  128. settings: { udp: true },
  129. });
  130. expect(http).toMatchObject({ listen: '127.0.0.1', protocol: 'http' });
  131. });
  132. it('trojan uses servers[] with a password and no method', () => {
  133. const trojan: SubClient = {
  134. protocol: 'trojan',
  135. remark: 'T',
  136. address: 'b',
  137. port: 443,
  138. password: 'pw',
  139. network: 'tcp',
  140. security: 'tls',
  141. };
  142. const cfg = JSON.parse(buildJsonSubscription([trojan]));
  143. const server = cfg.outbounds[0].settings.servers[0];
  144. expect(server.password).toBe('pw');
  145. expect('method' in server).toBe(false);
  146. });
  147. it('shadowsocks servers[] include a method', () => {
  148. const ss: SubClient = {
  149. protocol: 'ss',
  150. remark: 'S',
  151. address: 'c',
  152. port: 8388,
  153. password: 'pw',
  154. method: 'aes-256-gcm',
  155. };
  156. const cfg = JSON.parse(buildJsonSubscription([ss]));
  157. expect(cfg.outbounds[0].protocol).toBe('shadowsocks');
  158. expect(cfg.outbounds[0].settings.servers[0].method).toBe('aes-256-gcm');
  159. });
  160. it('emits an array for multiple clients', () => {
  161. const json = buildJsonSubscription([vlessClient, { ...vlessClient, remark: 'HK-02' }]);
  162. expect(Array.isArray(JSON.parse(json))).toBe(true);
  163. });
  164. it('returns empty for no clients', () => {
  165. expect(buildJsonSubscription([])).toBe('');
  166. });
  167. });