outbound-link-parser.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. parseOutboundLink,
  4. parseShadowsocksLink,
  5. parseTrojanLink,
  6. parseVlessLink,
  7. parseVmessLink,
  8. parseHysteria2Link,
  9. parseWireguardLink,
  10. } from '@/lib/xray/outbound-link-parser';
  11. import { Base64 } from '@/utils';
  12. // Focused acceptance tests for the share-link parsers — one happy-path
  13. // case per protocol family, plus a few common edge cases. The parsers
  14. // produce wire-shape outbound rows; the modal hands them to
  15. // rawOutboundToFormValues to seed Form.useForm.
  16. describe('parseVmessLink', () => {
  17. it('parses a vmess:// link with ws + tls', () => {
  18. const json = {
  19. v: '2', ps: 'imported-vmess', add: '1.2.3.4', port: 8443,
  20. id: '11111111-2222-4333-8444-555555555555', aid: 0, scy: 'auto',
  21. net: 'ws', host: 'example.com', path: '/ws',
  22. tls: 'tls', sni: 'example.com', fp: 'chrome', alpn: 'h2,http/1.1',
  23. };
  24. const link = `vmess://${Base64.encode(JSON.stringify(json))}`;
  25. const out = parseVmessLink(link);
  26. expect(out).not.toBeNull();
  27. expect(out?.protocol).toBe('vmess');
  28. expect(out?.tag).toBe('imported-vmess');
  29. const settings = out?.settings as { vnext: Array<{ address: string; port: number; users: Array<{ id: string; security: string }> }> };
  30. expect(settings.vnext[0].address).toBe('1.2.3.4');
  31. expect(settings.vnext[0].port).toBe(8443);
  32. expect(settings.vnext[0].users[0].id).toBe('11111111-2222-4333-8444-555555555555');
  33. const stream = out?.streamSettings as Record<string, unknown>;
  34. expect(stream.network).toBe('ws');
  35. expect(stream.security).toBe('tls');
  36. expect((stream.wsSettings as Record<string, unknown>).path).toBe('/ws');
  37. expect((stream.tlsSettings as Record<string, unknown>).serverName).toBe('example.com');
  38. expect((stream.tlsSettings as Record<string, unknown>).alpn).toEqual(['h2', 'http/1.1']);
  39. });
  40. it('returns null for non-vmess links', () => {
  41. expect(parseVmessLink('vless://x@y:1')).toBeNull();
  42. });
  43. it('returns null for malformed base64', () => {
  44. expect(parseVmessLink('vmess://!!!not-base64!!!')).toBeNull();
  45. });
  46. });
  47. describe('parseVmessLink — XHTTP advanced fields', () => {
  48. it('round-trips xhttp knobs from the vmess JSON', () => {
  49. const json = {
  50. v: '2', ps: 'imported-xhttp', add: '1.2.3.4', port: 443,
  51. id: '11111111-2222-4333-8444-555555555555', aid: 0, scy: 'auto',
  52. net: 'xhttp', host: 'edge.example', path: '/sp', mode: 'stream-up',
  53. xPaddingBytes: '500-1500',
  54. scMaxEachPostBytes: '2000000',
  55. scMinPostsIntervalMs: '60',
  56. uplinkChunkSize: 8192,
  57. noGRPCHeader: true,
  58. tls: 'tls', sni: 'edge.example',
  59. };
  60. const link = `vmess://${Base64.encode(JSON.stringify(json))}`;
  61. const out = parseVmessLink(link);
  62. const stream = out?.streamSettings as Record<string, unknown>;
  63. const xhttp = stream.xhttpSettings as Record<string, unknown>;
  64. expect(xhttp.host).toBe('edge.example');
  65. expect(xhttp.path).toBe('/sp');
  66. expect(xhttp.mode).toBe('stream-up');
  67. expect(xhttp.xPaddingBytes).toBe('500-1500');
  68. expect(xhttp.scMaxEachPostBytes).toBe('2000000');
  69. expect(xhttp.scMinPostsIntervalMs).toBe('60');
  70. expect(xhttp.uplinkChunkSize).toBe(8192);
  71. expect(xhttp.noGRPCHeader).toBe(true);
  72. });
  73. it('round-trips xhttp padding-obfs knobs from the vmess JSON', () => {
  74. const json = {
  75. v: '2', ps: 'imported-pad', add: '1.2.3.4', port: 443,
  76. id: '11111111-2222-4333-8444-555555555555', aid: 0, scy: 'auto',
  77. net: 'xhttp', host: 'edge.example', path: '/sp',
  78. xPaddingObfsMode: true,
  79. xPaddingKey: 'secret-key',
  80. xPaddingHeader: 'X-Pad',
  81. xPaddingPlacement: 'header',
  82. xPaddingMethod: 'random',
  83. sessionKey: 'X-Session',
  84. seqKey: 'X-Seq',
  85. noSSEHeader: true,
  86. scMaxBufferedPosts: 50,
  87. tls: 'tls',
  88. };
  89. const link = `vmess://${Base64.encode(JSON.stringify(json))}`;
  90. const out = parseVmessLink(link);
  91. const xhttp = (out?.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  92. expect(xhttp.xPaddingObfsMode).toBe(true);
  93. expect(xhttp.xPaddingKey).toBe('secret-key');
  94. expect(xhttp.xPaddingHeader).toBe('X-Pad');
  95. expect(xhttp.xPaddingPlacement).toBe('header');
  96. expect(xhttp.xPaddingMethod).toBe('random');
  97. expect(xhttp.sessionKey).toBe('X-Session');
  98. expect(xhttp.seqKey).toBe('X-Seq');
  99. expect(xhttp.noSSEHeader).toBe(true);
  100. expect(xhttp.scMaxBufferedPosts).toBe(50);
  101. });
  102. });
  103. describe('parseVlessLink — XHTTP advanced fields', () => {
  104. it('round-trips xhttp knobs from URL query params', () => {
  105. const link
  106. = 'vless://[email protected]:443'
  107. + '?type=xhttp&security=tls&host=edge.example&path=%2Fsp&mode=stream-up'
  108. + '&xPaddingBytes=500-1500&scMaxEachPostBytes=2000000'
  109. + '&scMinPostsIntervalMs=60&uplinkChunkSize=8192&noGRPCHeader=true'
  110. + '#imported-xhttp';
  111. const out = parseVlessLink(link);
  112. const stream = out?.streamSettings as Record<string, unknown>;
  113. const xhttp = stream.xhttpSettings as Record<string, unknown>;
  114. expect(xhttp.host).toBe('edge.example');
  115. expect(xhttp.path).toBe('/sp');
  116. expect(xhttp.mode).toBe('stream-up');
  117. expect(xhttp.xPaddingBytes).toBe('500-1500');
  118. expect(xhttp.scMaxEachPostBytes).toBe('2000000');
  119. expect(xhttp.scMinPostsIntervalMs).toBe('60');
  120. expect(xhttp.uplinkChunkSize).toBe(8192);
  121. expect(xhttp.noGRPCHeader).toBe(true);
  122. });
  123. it('round-trips xhttp padding-obfs knobs from URL query params', () => {
  124. const link
  125. = 'vless://[email protected]:443'
  126. + '?type=xhttp&security=tls&host=edge.example&path=%2Fsp'
  127. + '&xPaddingObfsMode=true&xPaddingKey=secret-key&xPaddingHeader=X-Pad'
  128. + '&xPaddingPlacement=header&xPaddingMethod=random'
  129. + '&sessionKey=X-Session&seqKey=X-Seq&noSSEHeader=true'
  130. + '&scMaxBufferedPosts=50'
  131. + '#imported-pad';
  132. const out = parseVlessLink(link);
  133. const xhttp = (out?.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  134. expect(xhttp.xPaddingObfsMode).toBe(true);
  135. expect(xhttp.xPaddingKey).toBe('secret-key');
  136. expect(xhttp.xPaddingHeader).toBe('X-Pad');
  137. expect(xhttp.xPaddingPlacement).toBe('header');
  138. expect(xhttp.xPaddingMethod).toBe('random');
  139. expect(xhttp.sessionKey).toBe('X-Session');
  140. expect(xhttp.seqKey).toBe('X-Seq');
  141. expect(xhttp.noSSEHeader).toBe(true);
  142. expect(xhttp.scMaxBufferedPosts).toBe(50);
  143. });
  144. });
  145. describe('parseVlessLink', () => {
  146. it('parses a vless:// link with reality', () => {
  147. const link
  148. = 'vless://[email protected]:443'
  149. + '?type=tcp&security=reality&pbk=pubkey&sid=abcd&fp=chrome&sni=cloudflare.com&flow=xtls-rprx-vision'
  150. + '#imported-vless';
  151. const out = parseVlessLink(link);
  152. expect(out?.protocol).toBe('vless');
  153. expect(out?.tag).toBe('imported-vless');
  154. const settings = out?.settings as { id: string; flow: string; address: string; port: number };
  155. expect(settings.id).toBe('11111111-2222-4333-8444-555555555555');
  156. expect(settings.address).toBe('srv.example');
  157. expect(settings.port).toBe(443);
  158. expect(settings.flow).toBe('xtls-rprx-vision');
  159. const stream = out?.streamSettings as Record<string, unknown>;
  160. expect(stream.security).toBe('reality');
  161. const reality = stream.realitySettings as Record<string, unknown>;
  162. expect(reality.publicKey).toBe('pubkey');
  163. expect(reality.shortId).toBe('abcd');
  164. expect(reality.serverName).toBe('cloudflare.com');
  165. });
  166. it('parses encryption + pqv (post-quantum) into settings and mldsa65Verify', () => {
  167. const enc = 'mlkem768x25519plus.native.0rtt.G3cdPSd1-NnlpTbWNSM5vHsT5VNzWfFzYSKwbUMnV1Y';
  168. const pqv = 'GIsemxbGPjDRH1ONfmoGlVkJ4etNuLmYDvzpjmFFreDLd8WjoJxJ4Fmt_NQJaC6';
  169. const link
  170. = 'vless://9406c224-8ac6-4675-ae0b-f93785959418@localhost:1121'
  171. + `?encryption=${enc}&pqv=${pqv}`
  172. + '&security=reality&sid=29cf418813d5bac7&sni=aws.amazon.com'
  173. + '&pbk=aQaGBOT2hMfXWebYtjADoOVUrP8qZRdwXVap7nrId0I&fp=chrome&spx=%2FOUTjB7xHRiP4zBP&type=tcp'
  174. + '#giqssbgmo9';
  175. const out = parseVlessLink(link);
  176. const settings = out?.settings as { encryption: string };
  177. expect(settings.encryption).toBe(enc);
  178. const reality = (out?.streamSettings as Record<string, unknown>).realitySettings as Record<string, unknown>;
  179. expect(reality.mldsa65Verify).toBe(pqv);
  180. expect(reality.publicKey).toBe('aQaGBOT2hMfXWebYtjADoOVUrP8qZRdwXVap7nrId0I');
  181. });
  182. });
  183. describe('parseTrojanLink', () => {
  184. it('parses a trojan:// link with ws + tls', () => {
  185. const link = 'trojan://[email protected]:8443?type=ws&security=tls&host=example.com&path=/tj&sni=example.com#imported-trojan';
  186. const out = parseTrojanLink(link);
  187. expect(out?.protocol).toBe('trojan');
  188. const settings = out?.settings as { servers: Array<{ address: string; port: number; password: string }> };
  189. expect(settings.servers[0].address).toBe('srv.example');
  190. expect(settings.servers[0].port).toBe(8443);
  191. expect(settings.servers[0].password).toBe('secret-pw');
  192. const stream = out?.streamSettings as Record<string, unknown>;
  193. expect(stream.network).toBe('ws');
  194. expect((stream.wsSettings as Record<string, unknown>).path).toBe('/tj');
  195. });
  196. });
  197. describe('parseShadowsocksLink', () => {
  198. it('parses the modern userinfo@host:port form', () => {
  199. // ss://base64(method:password)@host:port#remark
  200. const userinfo = Base64.encode('2022-blake3-aes-128-gcm:supersecret');
  201. const link = `ss://${userinfo}@1.2.3.4:8388#imported-ss`;
  202. const out = parseShadowsocksLink(link);
  203. expect(out?.protocol).toBe('shadowsocks');
  204. expect(out?.tag).toBe('imported-ss');
  205. const settings = out?.settings as { servers: Array<{ address: string; port: number; method: string; password: string }> };
  206. expect(settings.servers[0].address).toBe('1.2.3.4');
  207. expect(settings.servers[0].port).toBe(8388);
  208. expect(settings.servers[0].method).toBe('2022-blake3-aes-128-gcm');
  209. expect(settings.servers[0].password).toBe('supersecret');
  210. });
  211. it('keeps the port when the link carries a query string (2022 two-key password)', () => {
  212. const link = 'ss://MjAyMi1ibGFrZTMtYWVzLTI1Ni1nY206LzhsdFZKaU90azE2QmhKZG9WZVRmSkNNUEJlRGhjcmkycTN0dzU1OUZvYz06YUhuTTB6ZnpFaTdRejc5dzlxNWFFWWVQVnpDU0wxaHV4RnZXZFB6OFZHST0@localhost:30757?type=tcp#pahf4urt53';
  213. const out = parseShadowsocksLink(link);
  214. expect(out?.protocol).toBe('shadowsocks');
  215. expect(out?.tag).toBe('pahf4urt53');
  216. const settings = out?.settings as { servers: Array<{ address: string; port: number; method: string; password: string }> };
  217. expect(settings.servers[0].address).toBe('localhost');
  218. expect(settings.servers[0].port).toBe(30757);
  219. expect(settings.servers[0].method).toBe('2022-blake3-aes-256-gcm');
  220. expect(settings.servers[0].password).toBe('/8ltVJiOtk16BhJdoVeTfJCMPBeDhcri2q3tw559Foc=:aHnM0zfzEi7Qz79w9q5aEYePVzCSL1huxFvWdPz8VGI=');
  221. });
  222. it('parses the legacy base64-of-whole form', () => {
  223. // ss://base64(method:password@host:port)#remark
  224. const inner = Base64.encode('aes-256-gcm:[email protected]:1080');
  225. const link = `ss://${inner}#imported-legacy`;
  226. const out = parseShadowsocksLink(link);
  227. const settings = out?.settings as { servers: Array<{ address: string; port: number; method: string; password: string }> };
  228. expect(settings.servers[0].address).toBe('10.0.0.1');
  229. expect(settings.servers[0].port).toBe(1080);
  230. expect(settings.servers[0].method).toBe('aes-256-gcm');
  231. expect(settings.servers[0].password).toBe('legacypw');
  232. });
  233. });
  234. describe('parseHysteria2Link', () => {
  235. it('parses a hysteria2:// link with sni', () => {
  236. const link = 'hysteria2://[email protected]:443?sni=example.com#imported-hy2';
  237. const out = parseHysteria2Link(link);
  238. expect(out?.protocol).toBe('hysteria');
  239. expect(out?.tag).toBe('imported-hy2');
  240. const settings = out?.settings as { address: string; port: number; version: number };
  241. expect(settings.address).toBe('srv.example');
  242. expect(settings.port).toBe(443);
  243. expect(settings.version).toBe(2);
  244. const stream = out?.streamSettings as Record<string, unknown>;
  245. const hys = stream.hysteriaSettings as Record<string, unknown>;
  246. expect(hys.auth).toBe('auth-secret');
  247. expect((stream.tlsSettings as Record<string, unknown>).serverName).toBe('example.com');
  248. });
  249. it('also accepts hy2:// alias', () => {
  250. const out = parseHysteria2Link('hy2://auth@srv:443?sni=example.com');
  251. expect(out?.protocol).toBe('hysteria');
  252. });
  253. it('parses alpn, fingerprint and the salamander UDP mask (fm) — #4760', () => {
  254. const link = 'hysteria2://[email protected]:8443?'
  255. + 'alpn=h2%2Chttp%2F1.1&'
  256. + 'fm=%7B%22udp%22%3A%5B%7B%22settings%22%3A%7B%22password%22%3A%22ftwfgb9655hh2mgo%22%7D%2C%22type%22%3A%22salamander%22%7D%5D%7D&'
  257. + 'fp=chrome&obfs=salamander&obfs-password=655hh2mgo&security=tls&sni=news.domain.org'
  258. + '#hy2-ej596ty350qs';
  259. const out = parseHysteria2Link(link);
  260. expect(out).not.toBeNull();
  261. const stream = out!.streamSettings as Record<string, unknown>;
  262. const tls = stream.tlsSettings as Record<string, unknown>;
  263. expect(tls.alpn).toEqual(['h2', 'http/1.1']);
  264. expect(tls.fingerprint).toBe('chrome');
  265. expect(tls.serverName).toBe('news.domain.org');
  266. const finalmask = stream.finalmask as Record<string, unknown>;
  267. expect(finalmask).toBeDefined();
  268. const udp = finalmask.udp as Array<Record<string, unknown>>;
  269. expect(udp[0].type).toBe('salamander');
  270. expect((udp[0].settings as Record<string, unknown>).password).toBe('ftwfgb9655hh2mgo');
  271. });
  272. it('round-trips the salamander packetSize (Gecko) under fm', () => {
  273. const fm = encodeURIComponent(JSON.stringify({
  274. udp: [{ type: 'salamander', settings: { password: 'ftwfgb9655hh2mgo', packetSize: '100-200' } }],
  275. }));
  276. const link = `hysteria2://[email protected]:8443?security=tls&sni=news.domain.org&fm=${fm}#hy2-gecko`;
  277. const out = parseHysteria2Link(link);
  278. expect(out).not.toBeNull();
  279. const finalmask = (out!.streamSettings as Record<string, unknown>).finalmask as Record<string, unknown>;
  280. const udp = finalmask.udp as Array<Record<string, unknown>>;
  281. const settings = udp[0].settings as Record<string, unknown>;
  282. expect(udp[0].type).toBe('salamander');
  283. expect(settings.password).toBe('ftwfgb9655hh2mgo');
  284. expect(settings.packetSize).toBe('100-200');
  285. });
  286. it('round-trips the realm tlsConfig under fm', () => {
  287. const fm = encodeURIComponent(JSON.stringify({
  288. udp: [{
  289. type: 'realm',
  290. settings: {
  291. url: 'realm://[email protected]/my-realm',
  292. stunServers: ['stun.l.google.com:19302'],
  293. tlsConfig: { serverName: 'example.com', alpn: ['h3'], fingerprint: 'chrome', allowInsecure: false },
  294. },
  295. }],
  296. }));
  297. const link = `hysteria2://auth@srv:443?security=tls&sni=srv&fm=${fm}#hy2-realm`;
  298. const out = parseHysteria2Link(link);
  299. expect(out).not.toBeNull();
  300. const finalmask = (out!.streamSettings as Record<string, unknown>).finalmask as Record<string, unknown>;
  301. const udp = finalmask.udp as Array<Record<string, unknown>>;
  302. const settings = udp[0].settings as Record<string, unknown>;
  303. expect(udp[0].type).toBe('realm');
  304. expect(settings.url).toBe('realm://[email protected]/my-realm');
  305. const tlsConfig = settings.tlsConfig as Record<string, unknown>;
  306. expect(tlsConfig.serverName).toBe('example.com');
  307. expect(tlsConfig.alpn).toEqual(['h3']);
  308. expect(tlsConfig.fingerprint).toBe('chrome');
  309. });
  310. it('defaults alpn to h3 when the link omits it', () => {
  311. const out = parseHysteria2Link('hysteria2://auth@srv:443?sni=example.com');
  312. const tls = (out!.streamSettings as Record<string, unknown>).tlsSettings as Record<string, unknown>;
  313. expect(tls.alpn).toEqual(['h3']);
  314. });
  315. });
  316. describe('parseVlessLink — extra / fm / x_padding_bytes (B20)', () => {
  317. it('round-trips a real inbound-generated link with extra+fm+reality+xhttp', () => {
  318. // Real user-reported link — bundled xhttp knobs via `extra` JSON,
  319. // full finalmask via `fm` JSON, reality auth, snake_case
  320. // x_padding_bytes alias. All three parse-paths must combine.
  321. const link = 'vless://b622ac2f-f155-47db-a3b2-b64e8d7f6342@localhost:37723?'
  322. + 'encryption=none&'
  323. + 'extra=%7B%22scMaxEachPostBytes%22%3A%221000000%22%2C%22scMinPostsIntervalMs%22%3A%2230%22%2C%22xPaddingBytes%22%3A%22100-1000%22%7D&'
  324. + 'fm=%7B%22quicParams%22%3A%7B%22congestion%22%3A%22bbr%22%2C%22maxIdleTimeout%22%3A30%2C%22udpHop%22%3A%7B%22interval%22%3A%225-10%22%2C%22ports%22%3A%2220000-50000%22%7D%7D%7D&'
  325. + 'fp=chrome&host=&mode=auto&path=%2F&'
  326. + 'pbk=nJw4k4CPf5jf64V8nnDwWa8iClDnUvQ1lCI4iKzfJ0o&'
  327. + 'security=reality&sid=14ebccc4d3&sni=aws.amazon.com&'
  328. + 'spx=%2F97L2FjycXEwrE67&type=xhttp&x_padding_bytes=100-1000'
  329. + '#sda-8ud3us6rt';
  330. const parsed = parseVlessLink(link);
  331. expect(parsed).not.toBeNull();
  332. expect(parsed!.tag).toBe('sda-8ud3us6rt');
  333. const stream = parsed!.streamSettings as Record<string, unknown>;
  334. expect(stream.network).toBe('xhttp');
  335. expect(stream.security).toBe('reality');
  336. const xhttp = stream.xhttpSettings as Record<string, unknown>;
  337. expect(xhttp.xPaddingBytes).toBe('100-1000');
  338. expect(xhttp.scMaxEachPostBytes).toBe('1000000');
  339. expect(xhttp.scMinPostsIntervalMs).toBe('30');
  340. const reality = stream.realitySettings as Record<string, unknown>;
  341. expect(reality.publicKey).toBe('nJw4k4CPf5jf64V8nnDwWa8iClDnUvQ1lCI4iKzfJ0o');
  342. expect(reality.shortId).toBe('14ebccc4d3');
  343. expect(reality.spiderX).toBe('/97L2FjycXEwrE67');
  344. expect(reality.serverName).toBe('aws.amazon.com');
  345. const finalmask = stream.finalmask as Record<string, unknown>;
  346. expect(finalmask).toBeDefined();
  347. const quicParams = finalmask.quicParams as Record<string, unknown>;
  348. expect(quicParams.congestion).toBe('bbr');
  349. expect(quicParams.maxIdleTimeout).toBe(30);
  350. expect((quicParams.udpHop as Record<string, unknown>).interval).toBe('5-10');
  351. expect((quicParams.udpHop as Record<string, unknown>).ports).toBe('20000-50000');
  352. });
  353. it('falls back to x_padding_bytes when extra has no xPaddingBytes', () => {
  354. const link = 'vless://u@h:1?type=xhttp&security=none&path=%2F&host=&mode=auto&x_padding_bytes=200-2000#t';
  355. const parsed = parseVlessLink(link);
  356. const xhttp = (parsed!.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  357. expect(xhttp.xPaddingBytes).toBe('200-2000');
  358. });
  359. it('extra takes precedence — camelCase wins over snake_case alias', () => {
  360. const link = 'vless://u@h:1?type=xhttp&security=none&path=%2F&host=&mode=auto'
  361. + '&xPaddingBytes=900-9000&x_padding_bytes=100-1000#t';
  362. const parsed = parseVlessLink(link);
  363. const xhttp = (parsed!.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  364. expect(xhttp.xPaddingBytes).toBe('900-9000');
  365. });
  366. it('extracts the nested xmux object from the extra JSON blob', () => {
  367. // The inbound link bundles xmux into `extra` as a nested object
  368. // (sub/service.go). It must survive import so the outbound form's
  369. // XMUX sub-form populates rather than silently dropping it (#5353).
  370. const extra = encodeURIComponent(JSON.stringify({
  371. xmux: { maxConcurrency: '8-16', hMaxRequestTimes: '700-1000' },
  372. }));
  373. const link = 'vless://u@h:1?type=xhttp&security=none&path=%2F&host=&mode=auto'
  374. + '&extra=' + extra + '#t';
  375. const parsed = parseVlessLink(link);
  376. const xhttp = (parsed!.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  377. const xmux = xhttp.xmux as Record<string, unknown>;
  378. expect(xmux).toBeDefined();
  379. expect(xmux.maxConcurrency).toBe('8-16');
  380. expect(xmux.hMaxRequestTimes).toBe('700-1000');
  381. });
  382. it('ignores malformed extra JSON without breaking the rest of the link', () => {
  383. const link = 'vless://u@h:1?type=xhttp&security=none&path=%2F&host=&mode=auto'
  384. + '&extra=not-json&fp=chrome#t';
  385. const parsed = parseVlessLink(link);
  386. expect(parsed).not.toBeNull();
  387. const stream = parsed!.streamSettings as Record<string, unknown>;
  388. expect((stream.xhttpSettings as Record<string, unknown>).mode).toBe('auto');
  389. });
  390. it('round-trips ech and pcs from a TLS vless link', () => {
  391. const ech = 'AFb+DQBSAAAgACAL7gYwrvaSFCIEs34G3SkfpuIbjMuYQxAiJsPK1oO7cwAkAAEAAQABAAIAAQADAAIAAQACAAIAAgADAAMAAQADAAIAAwADAAMxMjMAAA==';
  392. const pcs = '6fbc15ba46dfed152ad6c8d2129dd774707dd667a9ab4965476fa0f79ba82670';
  393. const link = 'vless://e3d307ae-c074-4aa3-af08-4f9e0f1d298b@localhost:15282?'
  394. + 'alpn=h3&ech=' + encodeURIComponent(ech) + '&encryption=none&fp=firefox&host=&'
  395. + 'mode=packet-up&path=%2F&pcs=' + pcs + '&security=tls&sni=123&type=xhttp#i5sboxj07w';
  396. const parsed = parseVlessLink(link);
  397. expect(parsed).not.toBeNull();
  398. const tls = (parsed!.streamSettings as Record<string, unknown>).tlsSettings as Record<string, unknown>;
  399. expect(tls.echConfigList).toBe(ech);
  400. expect(tls.pinnedPeerCertSha256).toBe(pcs);
  401. expect(tls.serverName).toBe('123');
  402. expect(tls.fingerprint).toBe('firefox');
  403. });
  404. });
  405. describe('parseWireguardLink', () => {
  406. it('parses a wireguard:// link with percent-encoded secret and publickey', () => {
  407. const link = 'wireguard://IKeuy2+BNspvMffiC47z16seLIGxGtbDIYiZcbh9C1U%3D@localhost:22824'
  408. + '?publickey=3CnNsCy74TOlupjaii%2BRFp%2FgDMk5vvUuFD0SNZ%2FGl2s%3D'
  409. + '&address=10.0.0.2%2F32&mtu=1420#-1';
  410. const out = parseWireguardLink(link);
  411. expect(out?.protocol).toBe('wireguard');
  412. expect(out?.tag).toBe('-1');
  413. const settings = out?.settings as {
  414. secretKey: string; address: string[]; mtu: number;
  415. peers: Array<{ publicKey: string; endpoint: string; allowedIPs: string[] }>;
  416. };
  417. expect(settings.secretKey).toBe('IKeuy2+BNspvMffiC47z16seLIGxGtbDIYiZcbh9C1U=');
  418. expect(settings.address).toEqual(['10.0.0.2/32']);
  419. expect(settings.mtu).toBe(1420);
  420. expect(settings.peers[0].publicKey).toBe('3CnNsCy74TOlupjaii+RFp/gDMk5vvUuFD0SNZ/Gl2s=');
  421. expect(settings.peers[0].endpoint).toBe('localhost:22824');
  422. expect(settings.peers[0].allowedIPs).toEqual(['0.0.0.0/0', '::/0']);
  423. });
  424. it('parses reserved, presharedkey and keepalive aliases', () => {
  425. const link = 'wireguard://[email protected]:51820'
  426. + '?publickey=peerpub&address=10.0.0.2/32,fd00::2/128'
  427. + '&reserved=1,2,3&presharedkey=psk-secret&persistentkeepalive=25'
  428. + '&allowedips=0.0.0.0/0#wg-peer';
  429. const out = parseWireguardLink(link);
  430. const settings = out?.settings as {
  431. reserved: number[];
  432. peers: Array<{ preSharedKey: string; keepAlive: number; allowedIPs: string[] }>;
  433. address: string[];
  434. };
  435. expect(settings.address).toEqual(['10.0.0.2/32', 'fd00::2/128']);
  436. expect(settings.reserved).toEqual([1, 2, 3]);
  437. expect(settings.peers[0].preSharedKey).toBe('psk-secret');
  438. expect(settings.peers[0].keepAlive).toBe(25);
  439. expect(settings.peers[0].allowedIPs).toEqual(['0.0.0.0/0']);
  440. });
  441. it('returns null for non-wireguard links', () => {
  442. expect(parseWireguardLink('vless://x@y:1')).toBeNull();
  443. });
  444. });
  445. describe('parseOutboundLink dispatcher', () => {
  446. it('dispatches vmess via base64 JSON', () => {
  447. const json = { v: '2', ps: 'x', add: '1.1.1.1', port: 443, id: '11111111-2222-4333-8444-555555555555', net: 'tcp', tls: 'none' };
  448. const link = `vmess://${Base64.encode(JSON.stringify(json))}`;
  449. expect(parseOutboundLink(link)?.protocol).toBe('vmess');
  450. });
  451. it('dispatches vless via URL', () => {
  452. expect(parseOutboundLink('vless://uuid@host:443?type=tcp&security=none')?.protocol).toBe('vless');
  453. });
  454. it('dispatches wireguard via URL', () => {
  455. expect(parseOutboundLink('wireguard://pk@host:22824?publickey=pub&address=10.0.0.2/32')?.protocol).toBe('wireguard');
  456. });
  457. it('returns null for an unknown scheme', () => {
  458. expect(parseOutboundLink('socks5://user:pass@host:1080')).toBeNull();
  459. });
  460. it('returns null for empty input', () => {
  461. expect(parseOutboundLink('')).toBeNull();
  462. expect(parseOutboundLink(' ')).toBeNull();
  463. });
  464. });