outbound-form-adapter.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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('http preserves top-level settings.headers across wire → form → wire (#5519)', () => {
  167. const headers = { 'X-T5-Auth': '683556433', Host: '153.3.236.22:443' };
  168. const form = rawOutboundToFormValues({
  169. protocol: 'http',
  170. tag: 'h',
  171. settings: { servers: [{ address: 'a', port: 443, users: [] }], headers },
  172. });
  173. expect(form.protocol).toBe('http');
  174. if (form.protocol === 'http') {
  175. expect(form.settings.headers).toEqual(headers);
  176. }
  177. const back = formValuesToWirePayload(form);
  178. expect(back.settings).toMatchObject({ headers });
  179. });
  180. it('http omits headers when empty', () => {
  181. const back = formValuesToWirePayload(rawOutboundToFormValues({
  182. protocol: 'http',
  183. settings: { servers: [{ address: 'a', port: 8080, users: [] }] },
  184. }));
  185. expect(back.settings).not.toHaveProperty('headers');
  186. });
  187. it('wireguard csv-joins address and reserved on read, splits on write', () => {
  188. const wire = {
  189. protocol: 'wireguard',
  190. settings: {
  191. mtu: 1420,
  192. secretKey: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
  193. address: ['10.0.0.1', 'fd00::1'],
  194. peers: [{ publicKey: 'pk', allowedIPs: ['0.0.0.0/0'], endpoint: 'e:51820', preSharedKey: 'psk' }],
  195. reserved: [1, 2, 3],
  196. noKernelTun: false,
  197. },
  198. };
  199. const form = rawOutboundToFormValues(wire);
  200. if (form.protocol === 'wireguard') {
  201. expect(form.settings.address).toBe('10.0.0.1,fd00::1');
  202. expect(form.settings.reserved).toBe('1,2,3');
  203. expect(form.settings.peers[0].psk).toBe('psk');
  204. }
  205. const back = formValuesToWirePayload(form);
  206. expect(back.settings).toMatchObject({
  207. address: ['10.0.0.1', 'fd00::1'],
  208. reserved: [1, 2, 3],
  209. peers: [{ preSharedKey: 'psk' }],
  210. });
  211. });
  212. it('blackhole wraps type into {response:{type}} and omits when empty', () => {
  213. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  214. protocol: 'blackhole',
  215. settings: {},
  216. }));
  217. expect(empty.settings).toEqual({ response: undefined });
  218. const withType = formValuesToWirePayload(rawOutboundToFormValues({
  219. protocol: 'blackhole',
  220. settings: { response: { type: 'http' } },
  221. }));
  222. expect(withType.settings).toEqual({ response: { type: 'http' } });
  223. });
  224. it('dns rules normalize qType numeric strings, split domains, carry rCode', () => {
  225. const wire = {
  226. protocol: 'dns',
  227. settings: {
  228. rewriteNetwork: 'udp',
  229. rewriteAddress: '1.1.1.1',
  230. rewritePort: 53,
  231. rules: [
  232. { action: 'direct', qType: 'A,AAAA', domain: ['example.com', 'ext.org'] },
  233. { action: 'return', qType: 28, domain: 'blocked.com', rCode: 3 },
  234. ],
  235. },
  236. };
  237. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  238. const settings = back.settings as Record<string, unknown>;
  239. const rules = settings.rules as Array<Record<string, unknown>>;
  240. expect(rules[0]).toEqual({ action: 'direct', qType: 'A,AAAA', domain: ['example.com', 'ext.org'] });
  241. expect(rules[1]).toEqual({ action: 'return', qType: 28, domain: ['blocked.com'], rCode: 3 });
  242. });
  243. it('dns rules read the legacy qtype wire key for back-compat', () => {
  244. const wire = {
  245. protocol: 'dns',
  246. settings: { rules: [{ action: 'direct', qtype: 'TXT' }] },
  247. };
  248. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  249. const rules = (back.settings as Record<string, unknown>).rules as Array<Record<string, unknown>>;
  250. expect(rules[0]).toEqual({ action: 'direct', qType: 'TXT' });
  251. });
  252. it('freedom emits domainStrategy/redirect/fragment conditionally', () => {
  253. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  254. protocol: 'freedom',
  255. settings: {},
  256. }));
  257. expect(empty.settings).toEqual({
  258. domainStrategy: undefined,
  259. redirect: undefined,
  260. fragment: undefined,
  261. noises: undefined,
  262. finalRules: undefined,
  263. });
  264. const filled = formValuesToWirePayload(rawOutboundToFormValues({
  265. protocol: 'freedom',
  266. settings: {
  267. domainStrategy: 'UseIPv4',
  268. redirect: '1.1.1.1',
  269. userLevel: 3,
  270. proxyProtocol: 2,
  271. fragment: { packets: 'tlshello', length: '100-200' },
  272. noises: [{ type: 'rand', packet: '10-20', delay: '10-16', applyTo: 'ipv4' }],
  273. },
  274. }));
  275. expect(filled.settings).toMatchObject({
  276. domainStrategy: 'UseIPv4',
  277. redirect: '1.1.1.1',
  278. userLevel: 3,
  279. proxyProtocol: 2,
  280. fragment: { packets: 'tlshello', length: '100-200' },
  281. noises: [{ type: 'rand', packet: '10-20', delay: '10-16', applyTo: 'ipv4' }],
  282. });
  283. });
  284. it('freedom tolerates settings without a fragment object (issue #4686)', () => {
  285. const values = {
  286. protocol: 'freedom',
  287. tag: 'direct',
  288. settings: {
  289. domainStrategy: '',
  290. redirect: '',
  291. proxyProtocol: 0,
  292. noises: [],
  293. finalRules: [
  294. { action: 'block', network: '', port: '', ip: ['geoip:private'], blockDelay: '' },
  295. ],
  296. },
  297. } as unknown as Parameters<typeof formValuesToWirePayload>[0];
  298. expect(() => formValuesToWirePayload(values)).not.toThrow();
  299. const back = formValuesToWirePayload(values);
  300. expect((back.settings as { fragment?: unknown }).fragment).toBeUndefined();
  301. expect((back.settings as { finalRules?: unknown[] }).finalRules).toHaveLength(1);
  302. });
  303. it('freedom omits proxyProtocol when disabled (0)', () => {
  304. const round = formValuesToWirePayload(rawOutboundToFormValues({
  305. protocol: 'freedom',
  306. settings: { proxyProtocol: 0 },
  307. }));
  308. expect((round.settings as { proxyProtocol?: number }).proxyProtocol).toBeUndefined();
  309. });
  310. it('mux is only emitted when enabled AND protocol/network/flow allow it', () => {
  311. // Disabled mux: omitted
  312. const disabled = formValuesToWirePayload(rawOutboundToFormValues({
  313. protocol: 'vless',
  314. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  315. mux: { enabled: false },
  316. }));
  317. expect(disabled).not.toHaveProperty('mux');
  318. // Enabled mux on vless without flow: emitted
  319. const enabled = formValuesToWirePayload(rawOutboundToFormValues({
  320. protocol: 'vless',
  321. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  322. mux: { enabled: true, concurrency: 8, xudpConcurrency: 16, xudpProxyUDP443: 'reject' },
  323. }));
  324. expect(enabled.mux).toMatchObject({ enabled: true });
  325. // Enabled mux on vless with vision flow: gated out
  326. const withFlow = formValuesToWirePayload(rawOutboundToFormValues({
  327. protocol: 'vless',
  328. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: 'xtls-rprx-vision', encryption: 'none' },
  329. mux: { enabled: true },
  330. }));
  331. expect(withFlow).not.toHaveProperty('mux');
  332. // Freedom (non-mux protocol): gated out even if enabled
  333. const freedom = formValuesToWirePayload(rawOutboundToFormValues({
  334. protocol: 'freedom',
  335. settings: {},
  336. mux: { enabled: true },
  337. }));
  338. expect(freedom).not.toHaveProperty('mux');
  339. });
  340. it('hysteria preserves address/port/version literal 2', () => {
  341. const back = formValuesToWirePayload(rawOutboundToFormValues({
  342. protocol: 'hysteria',
  343. settings: { address: 'h.example', port: 8443, version: 2 },
  344. }));
  345. expect(back.settings).toEqual({ address: 'h.example', port: 8443, version: 2 });
  346. });
  347. it('loopback inboundTag round-trips', () => {
  348. const back = formValuesToWirePayload(rawOutboundToFormValues({
  349. protocol: 'loopback',
  350. settings: { inboundTag: 'tagged-inbound' },
  351. }));
  352. expect(back.settings).toEqual({ inboundTag: 'tagged-inbound' });
  353. });
  354. it('loopback omits sniffing when disabled', () => {
  355. const form = rawOutboundToFormValues({
  356. protocol: 'loopback',
  357. settings: { inboundTag: 'tagged-inbound' },
  358. });
  359. if (form.protocol === 'loopback') {
  360. expect(form.settings.sniffing.enabled).toBe(false);
  361. }
  362. const back = formValuesToWirePayload(form);
  363. expect(back.settings).not.toHaveProperty('sniffing');
  364. });
  365. it('loopback round-trips sniffing when enabled', () => {
  366. const wire = {
  367. protocol: 'loopback',
  368. settings: {
  369. inboundTag: 'tagged-inbound',
  370. sniffing: { enabled: true, destOverride: ['tls', 'http'], routeOnly: true },
  371. },
  372. };
  373. const form = rawOutboundToFormValues(wire);
  374. if (form.protocol === 'loopback') {
  375. expect(form.settings.sniffing.enabled).toBe(true);
  376. expect(form.settings.sniffing.destOverride).toEqual(['tls', 'http']);
  377. expect(form.settings.sniffing.routeOnly).toBe(true);
  378. }
  379. const back = formValuesToWirePayload(form);
  380. const sniffing = (back.settings as Record<string, unknown>).sniffing as Record<string, unknown>;
  381. expect(sniffing.enabled).toBe(true);
  382. expect(sniffing.destOverride).toEqual(['tls', 'http']);
  383. expect(sniffing.routeOnly).toBe(true);
  384. });
  385. it('unknown protocol falls back to vless without throwing', () => {
  386. const form = rawOutboundToFormValues({ protocol: 'mysterious', settings: {} });
  387. expect(form.protocol).toBe('vless');
  388. });
  389. });
  390. describe('outbound-form-adapter: xhttp xmux toggle', () => {
  391. const xmuxWire = {
  392. protocol: 'vless',
  393. tag: 'out-xhttp',
  394. settings: {
  395. address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555',
  396. flow: '', encryption: 'none',
  397. },
  398. streamSettings: {
  399. network: 'xhttp',
  400. security: 'none',
  401. xhttpSettings: {
  402. path: '/', host: '', mode: '',
  403. xPaddingBytes: '100-1000', scMaxEachPostBytes: '1000000',
  404. xmux: { maxConcurrency: '11', maxConnections: '1', hMaxRequestTimes: '1', hMaxReusableSecs: '1' },
  405. },
  406. },
  407. };
  408. it('derives enableXmux from a saved xmux object and backfills missing knobs', () => {
  409. const form = rawOutboundToFormValues(xmuxWire);
  410. const stream = form.streamSettings as Record<string, unknown>;
  411. const xhttp = stream.xhttpSettings as Record<string, unknown>;
  412. expect(xhttp.enableXmux).toBe(true);
  413. expect(xhttp.xmux).toMatchObject({
  414. maxConcurrency: '11',
  415. maxConnections: '1',
  416. hMaxRequestTimes: '1',
  417. hMaxReusableSecs: '1',
  418. cMaxReuseTimes: 0,
  419. hKeepAlivePeriod: 0,
  420. });
  421. });
  422. it('round-trips xmux on save, strips enableXmux, and enforces xmux exclusivity', () => {
  423. const back = formValuesToWirePayload(rawOutboundToFormValues(xmuxWire));
  424. const xhttp = (back.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  425. expect(xhttp).not.toHaveProperty('enableXmux');
  426. const xmux = xhttp.xmux as Record<string, unknown>;
  427. // xray-core rejects maxConnections + maxConcurrency together; the
  428. // explicit maxConnections wins and maxConcurrency is dropped.
  429. expect(xmux).not.toHaveProperty('maxConcurrency');
  430. expect(xmux).toMatchObject({ maxConnections: '1', hMaxRequestTimes: '1', hMaxReusableSecs: '1' });
  431. });
  432. it('drops xmux on save when the toggle is off', () => {
  433. const form = rawOutboundToFormValues(xmuxWire);
  434. const xhttp = (form.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  435. xhttp.enableXmux = false;
  436. const back = formValuesToWirePayload(form);
  437. const wireXhttp = (back.streamSettings as Record<string, unknown>).xhttpSettings as Record<string, unknown>;
  438. expect(wireXhttp).not.toHaveProperty('xmux');
  439. });
  440. });
  441. describe('outbound-form-adapter: full optional-block round-trip', () => {
  442. const wire = {
  443. protocol: 'vless',
  444. settings: {
  445. address: '1', port: 443, id: '1', flow: '', encryption: 'none',
  446. reverse: {
  447. tag: '1',
  448. sniffing: {
  449. enabled: true,
  450. destOverride: ['http', 'tls', 'quic', 'fakedns'],
  451. metadataOnly: true,
  452. routeOnly: true,
  453. ipsExcluded: ['1'],
  454. domainsExcluded: ['1'],
  455. },
  456. },
  457. },
  458. tag: '1',
  459. streamSettings: {
  460. network: 'tcp',
  461. 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'] } } } },
  462. security: 'none',
  463. sockopt: { tcpFastOpen: true, customSockopt: [{ type: 'int', level: '6', opt: '1', value: '1' }] },
  464. finalmask: { tcp: [{ type: 'fragment', settings: { packets: '1-3', length: '1', delay: '1', maxSplit: '1' } }] },
  465. },
  466. sendThrough: '1',
  467. mux: { enabled: true, concurrency: 8, xudpConcurrency: 16, xudpProxyUDP443: 'reject' },
  468. };
  469. it('preserves sockopt, finalmask, mux, and reverse excludes', () => {
  470. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  471. const settings = back.settings as Record<string, unknown>;
  472. const sniffing = (settings.reverse as Record<string, unknown>).sniffing as Record<string, unknown>;
  473. expect(sniffing.ipsExcluded).toEqual(['1']);
  474. expect(sniffing.domainsExcluded).toEqual(['1']);
  475. const stream = back.streamSettings as Record<string, unknown>;
  476. expect(stream.sockopt).toMatchObject({ tcpFastOpen: true });
  477. expect((stream.sockopt as Record<string, unknown>).customSockopt).toHaveLength(1);
  478. expect(stream.finalmask).toMatchObject({ tcp: [{ type: 'fragment' }] });
  479. expect(back.mux).toMatchObject({ enabled: true });
  480. });
  481. });