outbound-form-adapter.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import { describe, expect, it } from 'vitest';
  2. import {
  3. formValuesToWirePayload,
  4. rawOutboundToFormValues,
  5. } from '@/lib/xray/outbound-form-adapter';
  6. // Round-trip parity: wire → form → wire should preserve the legacy
  7. // Outbound.fromJson(...).toJson() output shape for each protocol's quirks.
  8. // Spot-checking the cases the modal exercised in v0.x — vmess vnext flatten,
  9. // vless reverse-wrap, wireguard address csv ↔ array, freedom finalRules
  10. // emission, blackhole type wrap, dns rule normalization, mux gating.
  11. describe('outbound-form-adapter: round-trip', () => {
  12. it('vmess flattens vnext to address/port/id/security and re-nests', () => {
  13. const wire = {
  14. protocol: 'vmess',
  15. tag: 'outbound-vmess',
  16. settings: {
  17. vnext: [{
  18. address: '1.2.3.4',
  19. port: 443,
  20. users: [{ id: '11111111-2222-4333-8444-555555555555', security: 'auto' }],
  21. }],
  22. },
  23. };
  24. const form = rawOutboundToFormValues(wire);
  25. expect(form.protocol).toBe('vmess');
  26. if (form.protocol === 'vmess') {
  27. expect(form.settings.address).toBe('1.2.3.4');
  28. expect(form.settings.port).toBe(443);
  29. expect(form.settings.id).toBe('11111111-2222-4333-8444-555555555555');
  30. expect(form.settings.security).toBe('auto');
  31. }
  32. const back = formValuesToWirePayload(form);
  33. expect(back).toMatchObject({
  34. protocol: 'vmess',
  35. tag: 'outbound-vmess',
  36. settings: {
  37. vnext: [{
  38. address: '1.2.3.4',
  39. port: 443,
  40. users: [{ id: '11111111-2222-4333-8444-555555555555', security: 'auto' }],
  41. }],
  42. },
  43. });
  44. });
  45. it('vless preserves flat shape and emits reverse only when reverseTag is set', () => {
  46. const wire = {
  47. protocol: 'vless',
  48. tag: 'out-vless',
  49. settings: {
  50. address: 'srv.example',
  51. port: 8443,
  52. id: '11111111-2222-4333-8444-555555555555',
  53. flow: 'xtls-rprx-vision',
  54. encryption: 'none',
  55. },
  56. };
  57. const form = rawOutboundToFormValues(wire);
  58. expect(form.protocol).toBe('vless');
  59. if (form.protocol === 'vless') {
  60. expect(form.settings.reverseTag).toBe('');
  61. }
  62. const back = formValuesToWirePayload(form);
  63. expect(back.settings).not.toHaveProperty('reverse');
  64. expect(back.settings).toMatchObject({
  65. address: 'srv.example',
  66. port: 8443,
  67. id: '11111111-2222-4333-8444-555555555555',
  68. flow: 'xtls-rprx-vision',
  69. encryption: 'none',
  70. });
  71. });
  72. it('vless preserves a non-none encryption value (post-quantum)', () => {
  73. const enc = 'mlkem768x25519plus.native.0rtt.G3cdPSd1-NnlpTbWNSM5vHsT5VNzWfFzYSKwbUMnV1Y';
  74. const wire = {
  75. protocol: 'vless',
  76. settings: {
  77. address: 'srv',
  78. port: 443,
  79. id: '11111111-2222-4333-8444-555555555555',
  80. flow: '',
  81. encryption: enc,
  82. },
  83. };
  84. const form = rawOutboundToFormValues(wire);
  85. if (form.protocol === 'vless') {
  86. expect(form.settings.encryption).toBe(enc);
  87. }
  88. expect((formValuesToWirePayload(form).settings as Record<string, unknown>).encryption).toBe(enc);
  89. });
  90. it('vless emits reverse + sniffing when reverseTag is set', () => {
  91. const wire = {
  92. protocol: 'vless',
  93. settings: {
  94. address: 'srv',
  95. port: 8443,
  96. id: '11111111-2222-4333-8444-555555555555',
  97. flow: '',
  98. encryption: 'none',
  99. reverse: { tag: 'rev-1', sniffing: { enabled: true, destOverride: ['tls'] } },
  100. },
  101. };
  102. const form = rawOutboundToFormValues(wire);
  103. if (form.protocol === 'vless') {
  104. expect(form.settings.reverseTag).toBe('rev-1');
  105. expect(form.settings.reverseSniffing.enabled).toBe(true);
  106. expect(form.settings.reverseSniffing.destOverride).toEqual(['tls']);
  107. }
  108. const back = formValuesToWirePayload(form);
  109. const settings = back.settings as Record<string, unknown>;
  110. expect(settings.reverse).toMatchObject({ tag: 'rev-1' });
  111. });
  112. it('vless does not emit testpre/testseed unless flow is vision', () => {
  113. const wire = {
  114. protocol: 'vless',
  115. settings: {
  116. address: 'srv', port: 443, id: '11111111-2222-4333-8444-555555555555',
  117. flow: '', encryption: 'none', testpre: 5, testseed: [1, 2, 3, 4],
  118. },
  119. };
  120. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  121. expect(back.settings).not.toHaveProperty('testpre');
  122. expect(back.settings).not.toHaveProperty('testseed');
  123. });
  124. it('trojan flattens servers[0] and re-nests', () => {
  125. const wire = {
  126. protocol: 'trojan',
  127. settings: { servers: [{ address: 's', port: 443, password: 'pw' }] },
  128. };
  129. const form = rawOutboundToFormValues(wire);
  130. if (form.protocol === 'trojan') {
  131. expect(form.settings).toEqual({ address: 's', port: 443, password: 'pw' });
  132. }
  133. expect(formValuesToWirePayload(form).settings).toEqual({
  134. servers: [{ address: 's', port: 443, password: 'pw' }],
  135. });
  136. });
  137. it('shadowsocks preserves uot + UoTVersion', () => {
  138. const wire = {
  139. protocol: 'shadowsocks',
  140. settings: {
  141. servers: [{
  142. address: 's', port: 443, password: 'pw',
  143. method: '2022-blake3-aes-128-gcm', uot: true, UoTVersion: 2,
  144. }],
  145. },
  146. };
  147. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  148. expect(back.settings).toMatchObject({
  149. servers: [{ uot: true, UoTVersion: 2 }],
  150. });
  151. });
  152. it('socks emits users:[] when user is empty, users:[{...}] when set', () => {
  153. const noUser = formValuesToWirePayload(rawOutboundToFormValues({
  154. protocol: 'socks',
  155. settings: { servers: [{ address: 's', port: 1080 }] },
  156. }));
  157. expect(noUser.settings).toMatchObject({ servers: [{ users: [] }] });
  158. const withUser = formValuesToWirePayload(rawOutboundToFormValues({
  159. protocol: 'socks',
  160. settings: { servers: [{ address: 's', port: 1080, users: [{ user: 'u', pass: 'p' }] }] },
  161. }));
  162. expect(withUser.settings).toMatchObject({
  163. servers: [{ users: [{ user: 'u', pass: 'p' }] }],
  164. });
  165. });
  166. it('wireguard csv-joins address and reserved on read, splits on write', () => {
  167. const wire = {
  168. protocol: 'wireguard',
  169. settings: {
  170. mtu: 1420,
  171. secretKey: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
  172. address: ['10.0.0.1', 'fd00::1'],
  173. workers: 2,
  174. peers: [{ publicKey: 'pk', allowedIPs: ['0.0.0.0/0'], endpoint: 'e:51820', preSharedKey: 'psk' }],
  175. reserved: [1, 2, 3],
  176. noKernelTun: false,
  177. },
  178. };
  179. const form = rawOutboundToFormValues(wire);
  180. if (form.protocol === 'wireguard') {
  181. expect(form.settings.address).toBe('10.0.0.1,fd00::1');
  182. expect(form.settings.reserved).toBe('1,2,3');
  183. expect(form.settings.peers[0].psk).toBe('psk');
  184. }
  185. const back = formValuesToWirePayload(form);
  186. expect(back.settings).toMatchObject({
  187. address: ['10.0.0.1', 'fd00::1'],
  188. reserved: [1, 2, 3],
  189. peers: [{ preSharedKey: 'psk' }],
  190. });
  191. });
  192. it('blackhole wraps type into {response:{type}} and omits when empty', () => {
  193. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  194. protocol: 'blackhole',
  195. settings: {},
  196. }));
  197. expect(empty.settings).toEqual({ response: undefined });
  198. const withType = formValuesToWirePayload(rawOutboundToFormValues({
  199. protocol: 'blackhole',
  200. settings: { response: { type: 'http' } },
  201. }));
  202. expect(withType.settings).toEqual({ response: { type: 'http' } });
  203. });
  204. it('dns rules normalize qType numeric strings, split domains, carry rCode', () => {
  205. const wire = {
  206. protocol: 'dns',
  207. settings: {
  208. rewriteNetwork: 'udp',
  209. rewriteAddress: '1.1.1.1',
  210. rewritePort: 53,
  211. rules: [
  212. { action: 'direct', qType: 'A,AAAA', domain: ['example.com', 'ext.org'] },
  213. { action: 'return', qType: 28, domain: 'blocked.com', rCode: 3 },
  214. ],
  215. },
  216. };
  217. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  218. const settings = back.settings as Record<string, unknown>;
  219. const rules = settings.rules as Array<Record<string, unknown>>;
  220. expect(rules[0]).toEqual({ action: 'direct', qType: 'A,AAAA', domain: ['example.com', 'ext.org'] });
  221. expect(rules[1]).toEqual({ action: 'return', qType: 28, domain: ['blocked.com'], rCode: 3 });
  222. });
  223. it('dns rules read the legacy qtype wire key for back-compat', () => {
  224. const wire = {
  225. protocol: 'dns',
  226. settings: { rules: [{ action: 'direct', qtype: 'TXT' }] },
  227. };
  228. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  229. const rules = (back.settings as Record<string, unknown>).rules as Array<Record<string, unknown>>;
  230. expect(rules[0]).toEqual({ action: 'direct', qType: 'TXT' });
  231. });
  232. it('freedom emits domainStrategy/redirect/fragment conditionally', () => {
  233. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  234. protocol: 'freedom',
  235. settings: {},
  236. }));
  237. expect(empty.settings).toEqual({
  238. domainStrategy: undefined,
  239. redirect: undefined,
  240. fragment: undefined,
  241. noises: undefined,
  242. finalRules: undefined,
  243. });
  244. const filled = formValuesToWirePayload(rawOutboundToFormValues({
  245. protocol: 'freedom',
  246. settings: {
  247. domainStrategy: 'UseIPv4',
  248. redirect: '1.1.1.1',
  249. userLevel: 3,
  250. proxyProtocol: 2,
  251. fragment: { packets: 'tlshello', length: '100-200' },
  252. noises: [{ type: 'rand', packet: '10-20', delay: '10-16', applyTo: 'ipv4' }],
  253. },
  254. }));
  255. expect(filled.settings).toMatchObject({
  256. domainStrategy: 'UseIPv4',
  257. redirect: '1.1.1.1',
  258. userLevel: 3,
  259. proxyProtocol: 2,
  260. fragment: { packets: 'tlshello', length: '100-200' },
  261. noises: [{ type: 'rand', packet: '10-20', delay: '10-16', applyTo: 'ipv4' }],
  262. });
  263. });
  264. it('freedom tolerates settings without a fragment object (issue #4686)', () => {
  265. const values = {
  266. protocol: 'freedom',
  267. tag: 'direct',
  268. settings: {
  269. domainStrategy: '',
  270. redirect: '',
  271. proxyProtocol: 0,
  272. noises: [],
  273. finalRules: [
  274. { action: 'block', network: '', port: '', ip: ['geoip:private'], blockDelay: '' },
  275. ],
  276. },
  277. } as unknown as Parameters<typeof formValuesToWirePayload>[0];
  278. expect(() => formValuesToWirePayload(values)).not.toThrow();
  279. const back = formValuesToWirePayload(values);
  280. expect((back.settings as { fragment?: unknown }).fragment).toBeUndefined();
  281. expect((back.settings as { finalRules?: unknown[] }).finalRules).toHaveLength(1);
  282. });
  283. it('freedom omits proxyProtocol when disabled (0)', () => {
  284. const round = formValuesToWirePayload(rawOutboundToFormValues({
  285. protocol: 'freedom',
  286. settings: { proxyProtocol: 0 },
  287. }));
  288. expect((round.settings as { proxyProtocol?: number }).proxyProtocol).toBeUndefined();
  289. });
  290. it('mux is only emitted when enabled AND protocol/network/flow allow it', () => {
  291. // Disabled mux: omitted
  292. const disabled = formValuesToWirePayload(rawOutboundToFormValues({
  293. protocol: 'vless',
  294. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  295. mux: { enabled: false },
  296. }));
  297. expect(disabled).not.toHaveProperty('mux');
  298. // Enabled mux on vless without flow: emitted
  299. const enabled = formValuesToWirePayload(rawOutboundToFormValues({
  300. protocol: 'vless',
  301. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  302. mux: { enabled: true, concurrency: 8, xudpConcurrency: 16, xudpProxyUDP443: 'reject' },
  303. }));
  304. expect(enabled.mux).toMatchObject({ enabled: true });
  305. // Enabled mux on vless with vision flow: gated out
  306. const withFlow = formValuesToWirePayload(rawOutboundToFormValues({
  307. protocol: 'vless',
  308. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: 'xtls-rprx-vision', encryption: 'none' },
  309. mux: { enabled: true },
  310. }));
  311. expect(withFlow).not.toHaveProperty('mux');
  312. // Freedom (non-mux protocol): gated out even if enabled
  313. const freedom = formValuesToWirePayload(rawOutboundToFormValues({
  314. protocol: 'freedom',
  315. settings: {},
  316. mux: { enabled: true },
  317. }));
  318. expect(freedom).not.toHaveProperty('mux');
  319. });
  320. it('hysteria preserves address/port/version literal 2', () => {
  321. const back = formValuesToWirePayload(rawOutboundToFormValues({
  322. protocol: 'hysteria',
  323. settings: { address: 'h.example', port: 8443, version: 2 },
  324. }));
  325. expect(back.settings).toEqual({ address: 'h.example', port: 8443, version: 2 });
  326. });
  327. it('loopback inboundTag round-trips', () => {
  328. const back = formValuesToWirePayload(rawOutboundToFormValues({
  329. protocol: 'loopback',
  330. settings: { inboundTag: 'tagged-inbound' },
  331. }));
  332. expect(back.settings).toEqual({ inboundTag: 'tagged-inbound' });
  333. });
  334. it('unknown protocol falls back to vless without throwing', () => {
  335. const form = rawOutboundToFormValues({ protocol: 'mysterious', settings: {} });
  336. expect(form.protocol).toBe('vless');
  337. });
  338. });
  339. describe('outbound-form-adapter: xhttp xmux toggle', () => {
  340. const xmuxWire = {
  341. protocol: 'vless',
  342. tag: 'out-xhttp',
  343. settings: {
  344. address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555',
  345. flow: '', encryption: 'none',
  346. },
  347. streamSettings: {
  348. network: 'xhttp',
  349. security: 'none',
  350. xhttpSettings: {
  351. path: '/', host: '', mode: '',
  352. xPaddingBytes: '100-1000', scMaxEachPostBytes: '1000000',
  353. xmux: { maxConcurrency: '11', maxConnections: '1', hMaxRequestTimes: '1', hMaxReusableSecs: '1' },
  354. },
  355. },
  356. };
  357. it('derives enableXmux from a saved xmux object and backfills missing knobs', () => {
  358. const form = rawOutboundToFormValues(xmuxWire);
  359. const stream = form.streamSettings as Record<string, unknown>;
  360. const xhttp = stream.xhttpSettings as Record<string, unknown>;
  361. expect(xhttp.enableXmux).toBe(true);
  362. expect(xhttp.xmux).toMatchObject({
  363. maxConcurrency: '11',
  364. maxConnections: '1',
  365. hMaxRequestTimes: '1',
  366. hMaxReusableSecs: '1',
  367. cMaxReuseTimes: 0,
  368. hKeepAlivePeriod: 0,
  369. });
  370. });
  371. it('round-trips xmux on save and strips the UI-only enableXmux flag', () => {
  372. const back = formValuesToWirePayload(rawOutboundToFormValues(xmuxWire));
  373. const xhttp = (back.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  374. expect(xhttp).not.toHaveProperty('enableXmux');
  375. expect(xhttp.xmux).toMatchObject({ maxConcurrency: '11', maxConnections: '1' });
  376. });
  377. it('drops xmux on save when the toggle is off', () => {
  378. const form = rawOutboundToFormValues(xmuxWire);
  379. const xhttp = (form.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  380. xhttp.enableXmux = false;
  381. const back = formValuesToWirePayload(form);
  382. const wireXhttp = (back.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  383. expect(wireXhttp).not.toHaveProperty('xmux');
  384. });
  385. });
  386. describe('outbound-form-adapter: full optional-block round-trip', () => {
  387. const wire = {
  388. protocol: 'vless',
  389. settings: {
  390. address: '1', port: 443, id: '1', flow: '', encryption: 'none',
  391. reverse: {
  392. tag: '1',
  393. sniffing: {
  394. enabled: true,
  395. destOverride: ['http', 'tls', 'quic', 'fakedns'],
  396. metadataOnly: true,
  397. routeOnly: true,
  398. ipsExcluded: ['1'],
  399. domainsExcluded: ['1'],
  400. },
  401. },
  402. },
  403. tag: '1',
  404. streamSettings: {
  405. network: 'tcp',
  406. tcpSettings: { header: { type: 'http', request: { version: '1.1', method: 'GET', path: ['/'], headers: { '1': ['1'] } }, response: { version: '1.1', status: '200', reason: 'OK', headers: { '1': ['1'] } } } },
  407. security: 'none',
  408. sockopt: { tcpFastOpen: true, customSockopt: [{ type: 'int', level: '6', opt: '1', value: '1' }] },
  409. finalmask: { tcp: [{ type: 'fragment', settings: { packets: '1-3', length: '1', delay: '1', maxSplit: '1' } }] },
  410. },
  411. sendThrough: '1',
  412. mux: { enabled: true, concurrency: 8, xudpConcurrency: 16, xudpProxyUDP443: 'reject' },
  413. };
  414. it('preserves sockopt, finalmask, mux, and reverse excludes', () => {
  415. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  416. const settings = back.settings as Record<string, unknown>;
  417. const sniffing = (settings.reverse as Record<string, unknown>).sniffing as Record<string, unknown>;
  418. expect(sniffing.ipsExcluded).toEqual(['1']);
  419. expect(sniffing.domainsExcluded).toEqual(['1']);
  420. const stream = back.streamSettings as Record<string, unknown>;
  421. expect(stream.sockopt).toMatchObject({ tcpFastOpen: true });
  422. expect((stream.sockopt as Record<string, unknown>).customSockopt).toHaveLength(1);
  423. expect(stream.finalmask).toMatchObject({ tcp: [{ type: 'fragment' }] });
  424. expect(back.mux).toMatchObject({ enabled: true });
  425. });
  426. });