subscription.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.name).toBe('HK-01');
  71. });
  72. });
  73. describe('buildBase64Subscription', () => {
  74. it('decodes back to the newline-joined links', () => {
  75. const trojan: SubClient = {
  76. protocol: 'trojan',
  77. remark: 'T',
  78. address: 'b.example.com',
  79. port: 443,
  80. password: 'pw',
  81. network: 'ws',
  82. security: 'tls',
  83. path: '/x',
  84. sni: 'b.example.com',
  85. };
  86. const body = buildBase64Subscription([vlessClient, trojan]);
  87. const decoded = base64ToText(body);
  88. expect(decoded.split('\n')).toEqual(buildShareLinks([vlessClient, trojan]));
  89. });
  90. it('returns empty for no clients', () => {
  91. expect(buildBase64Subscription([])).toBe('');
  92. });
  93. });
  94. describe('buildJsonSubscription', () => {
  95. it('emits a single object for one client with a FLAT proxy outbound (settings.id, no vnext)', () => {
  96. const cfg = JSON.parse(buildJsonSubscription([vlessClient]));
  97. expect(Array.isArray(cfg)).toBe(false);
  98. const proxy = cfg.outbounds[0];
  99. expect(proxy.protocol).toBe('vless');
  100. expect(proxy.tag).toBe('proxy');
  101. expect(proxy.settings.id).toBe(vlessClient.id);
  102. expect('vnext' in proxy.settings).toBe(false);
  103. expect(proxy.settings.level).toBe(8);
  104. expect(proxy.settings.flow).toBe('xtls-rprx-vision');
  105. // the default.json skeleton outbounds are preserved after the proxy
  106. expect(cfg.outbounds.some((o: { tag: string }) => o.tag === 'direct')).toBe(true);
  107. expect(cfg.outbounds.some((o: { tag: string }) => o.tag === 'block')).toBe(true);
  108. // sockopt is stripped from JSON-sub streamSettings
  109. expect(JSON.stringify(proxy.streamSettings)).not.toContain('sockopt');
  110. expect(cfg.remarks).toBe('HK-01');
  111. });
  112. it('uses the iOS-compatible SOCKS inbound while preserving the mixed tag and HTTP inbound', () => {
  113. const cfg = JSON.parse(buildJsonSubscription([vlessClient]));
  114. const socks = cfg.inbounds.find((inbound: { port: number }) => inbound.port === 10808);
  115. const http = cfg.inbounds.find((inbound: { port: number }) => inbound.port === 10809);
  116. expect(socks).toMatchObject({
  117. protocol: 'socks',
  118. tag: 'mixed',
  119. settings: { udp: true },
  120. });
  121. expect(http).toMatchObject({ protocol: 'http' });
  122. });
  123. it('trojan uses servers[] with a password and no method', () => {
  124. const trojan: SubClient = {
  125. protocol: 'trojan',
  126. remark: 'T',
  127. address: 'b',
  128. port: 443,
  129. password: 'pw',
  130. network: 'tcp',
  131. security: 'tls',
  132. };
  133. const cfg = JSON.parse(buildJsonSubscription([trojan]));
  134. const server = cfg.outbounds[0].settings.servers[0];
  135. expect(server.password).toBe('pw');
  136. expect('method' in server).toBe(false);
  137. });
  138. it('shadowsocks servers[] include a method', () => {
  139. const ss: SubClient = {
  140. protocol: 'ss',
  141. remark: 'S',
  142. address: 'c',
  143. port: 8388,
  144. password: 'pw',
  145. method: 'aes-256-gcm',
  146. };
  147. const cfg = JSON.parse(buildJsonSubscription([ss]));
  148. expect(cfg.outbounds[0].protocol).toBe('shadowsocks');
  149. expect(cfg.outbounds[0].settings.servers[0].method).toBe('aes-256-gcm');
  150. });
  151. it('emits an array for multiple clients', () => {
  152. const json = buildJsonSubscription([vlessClient, { ...vlessClient, remark: 'HK-02' }]);
  153. expect(Array.isArray(JSON.parse(json))).toBe(true);
  154. });
  155. it('returns empty for no clients', () => {
  156. expect(buildJsonSubscription([])).toBe('');
  157. });
  158. });