1
0

subscription.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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('trojan uses servers[] with a password and no method', () => {
  113. const trojan: SubClient = {
  114. protocol: 'trojan',
  115. remark: 'T',
  116. address: 'b',
  117. port: 443,
  118. password: 'pw',
  119. network: 'tcp',
  120. security: 'tls',
  121. };
  122. const cfg = JSON.parse(buildJsonSubscription([trojan]));
  123. const server = cfg.outbounds[0].settings.servers[0];
  124. expect(server.password).toBe('pw');
  125. expect('method' in server).toBe(false);
  126. });
  127. it('shadowsocks servers[] include a method', () => {
  128. const ss: SubClient = {
  129. protocol: 'ss',
  130. remark: 'S',
  131. address: 'c',
  132. port: 8388,
  133. password: 'pw',
  134. method: 'aes-256-gcm',
  135. };
  136. const cfg = JSON.parse(buildJsonSubscription([ss]));
  137. expect(cfg.outbounds[0].protocol).toBe('shadowsocks');
  138. expect(cfg.outbounds[0].settings.servers[0].method).toBe('aes-256-gcm');
  139. });
  140. it('emits an array for multiple clients', () => {
  141. const json = buildJsonSubscription([vlessClient, { ...vlessClient, remark: 'HK-02' }]);
  142. expect(Array.isArray(JSON.parse(json))).toBe(true);
  143. });
  144. it('returns empty for no clients', () => {
  145. expect(buildJsonSubscription([])).toBe('');
  146. });
  147. });