outbound-form-adapter.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 emits reverse + sniffing when reverseTag is set', () => {
  73. const wire = {
  74. protocol: 'vless',
  75. settings: {
  76. address: 'srv',
  77. port: 8443,
  78. id: '11111111-2222-4333-8444-555555555555',
  79. flow: '',
  80. encryption: 'none',
  81. reverse: { tag: 'rev-1', sniffing: { enabled: true, destOverride: ['tls'] } },
  82. },
  83. };
  84. const form = rawOutboundToFormValues(wire);
  85. if (form.protocol === 'vless') {
  86. expect(form.settings.reverseTag).toBe('rev-1');
  87. expect(form.settings.reverseSniffing.enabled).toBe(true);
  88. expect(form.settings.reverseSniffing.destOverride).toEqual(['tls']);
  89. }
  90. const back = formValuesToWirePayload(form);
  91. const settings = back.settings as Record<string, unknown>;
  92. expect(settings.reverse).toMatchObject({ tag: 'rev-1' });
  93. });
  94. it('vless does not emit testpre/testseed unless flow is vision', () => {
  95. const wire = {
  96. protocol: 'vless',
  97. settings: {
  98. address: 'srv', port: 443, id: '11111111-2222-4333-8444-555555555555',
  99. flow: '', encryption: 'none', testpre: 5, testseed: [1, 2, 3, 4],
  100. },
  101. };
  102. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  103. expect(back.settings).not.toHaveProperty('testpre');
  104. expect(back.settings).not.toHaveProperty('testseed');
  105. });
  106. it('trojan flattens servers[0] and re-nests', () => {
  107. const wire = {
  108. protocol: 'trojan',
  109. settings: { servers: [{ address: 's', port: 443, password: 'pw' }] },
  110. };
  111. const form = rawOutboundToFormValues(wire);
  112. if (form.protocol === 'trojan') {
  113. expect(form.settings).toEqual({ address: 's', port: 443, password: 'pw' });
  114. }
  115. expect(formValuesToWirePayload(form).settings).toEqual({
  116. servers: [{ address: 's', port: 443, password: 'pw' }],
  117. });
  118. });
  119. it('shadowsocks preserves uot + UoTVersion', () => {
  120. const wire = {
  121. protocol: 'shadowsocks',
  122. settings: {
  123. servers: [{
  124. address: 's', port: 443, password: 'pw',
  125. method: '2022-blake3-aes-128-gcm', uot: true, UoTVersion: 2,
  126. }],
  127. },
  128. };
  129. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  130. expect(back.settings).toMatchObject({
  131. servers: [{ uot: true, UoTVersion: 2 }],
  132. });
  133. });
  134. it('socks emits users:[] when user is empty, users:[{...}] when set', () => {
  135. const noUser = formValuesToWirePayload(rawOutboundToFormValues({
  136. protocol: 'socks',
  137. settings: { servers: [{ address: 's', port: 1080 }] },
  138. }));
  139. expect(noUser.settings).toMatchObject({ servers: [{ users: [] }] });
  140. const withUser = formValuesToWirePayload(rawOutboundToFormValues({
  141. protocol: 'socks',
  142. settings: { servers: [{ address: 's', port: 1080, users: [{ user: 'u', pass: 'p' }] }] },
  143. }));
  144. expect(withUser.settings).toMatchObject({
  145. servers: [{ users: [{ user: 'u', pass: 'p' }] }],
  146. });
  147. });
  148. it('wireguard csv-joins address and reserved on read, splits on write', () => {
  149. const wire = {
  150. protocol: 'wireguard',
  151. settings: {
  152. mtu: 1420,
  153. secretKey: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=',
  154. address: ['10.0.0.1', 'fd00::1'],
  155. workers: 2,
  156. peers: [{ publicKey: 'pk', allowedIPs: ['0.0.0.0/0'], endpoint: 'e:51820', preSharedKey: 'psk' }],
  157. reserved: [1, 2, 3],
  158. noKernelTun: false,
  159. },
  160. };
  161. const form = rawOutboundToFormValues(wire);
  162. if (form.protocol === 'wireguard') {
  163. expect(form.settings.address).toBe('10.0.0.1,fd00::1');
  164. expect(form.settings.reserved).toBe('1,2,3');
  165. expect(form.settings.peers[0].psk).toBe('psk');
  166. }
  167. const back = formValuesToWirePayload(form);
  168. expect(back.settings).toMatchObject({
  169. address: ['10.0.0.1', 'fd00::1'],
  170. reserved: [1, 2, 3],
  171. peers: [{ preSharedKey: 'psk' }],
  172. });
  173. });
  174. it('blackhole wraps type into {response:{type}} and omits when empty', () => {
  175. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  176. protocol: 'blackhole',
  177. settings: {},
  178. }));
  179. expect(empty.settings).toEqual({ response: undefined });
  180. const withType = formValuesToWirePayload(rawOutboundToFormValues({
  181. protocol: 'blackhole',
  182. settings: { response: { type: 'http' } },
  183. }));
  184. expect(withType.settings).toEqual({ response: { type: 'http' } });
  185. });
  186. it('dns rules normalize qtype numeric strings and split domains', () => {
  187. const wire = {
  188. protocol: 'dns',
  189. settings: {
  190. rewriteNetwork: 'udp',
  191. rewriteAddress: '1.1.1.1',
  192. rewritePort: 53,
  193. rules: [
  194. { action: 'direct', qtype: 'A,AAAA', domain: ['example.com', 'ext.org'] },
  195. { action: 'reject', qtype: 28, domain: 'blocked.com' },
  196. ],
  197. },
  198. };
  199. const back = formValuesToWirePayload(rawOutboundToFormValues(wire));
  200. const settings = back.settings as Record<string, unknown>;
  201. const rules = settings.rules as Array<Record<string, unknown>>;
  202. expect(rules[0]).toEqual({ action: 'direct', qtype: 'A,AAAA', domain: ['example.com', 'ext.org'] });
  203. expect(rules[1]).toEqual({ action: 'reject', qtype: 28, domain: ['blocked.com'] });
  204. });
  205. it('freedom emits domainStrategy/redirect/fragment conditionally', () => {
  206. const empty = formValuesToWirePayload(rawOutboundToFormValues({
  207. protocol: 'freedom',
  208. settings: {},
  209. }));
  210. expect(empty.settings).toEqual({
  211. domainStrategy: undefined,
  212. redirect: undefined,
  213. fragment: undefined,
  214. noises: undefined,
  215. finalRules: undefined,
  216. });
  217. const filled = formValuesToWirePayload(rawOutboundToFormValues({
  218. protocol: 'freedom',
  219. settings: {
  220. domainStrategy: 'UseIPv4',
  221. redirect: '1.1.1.1',
  222. fragment: { packets: 'tlshello', length: '100-200' },
  223. },
  224. }));
  225. expect(filled.settings).toMatchObject({
  226. domainStrategy: 'UseIPv4',
  227. redirect: '1.1.1.1',
  228. fragment: { packets: 'tlshello', length: '100-200' },
  229. });
  230. });
  231. it('mux is only emitted when enabled AND protocol/network/flow allow it', () => {
  232. // Disabled mux: omitted
  233. const disabled = formValuesToWirePayload(rawOutboundToFormValues({
  234. protocol: 'vless',
  235. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  236. mux: { enabled: false },
  237. }));
  238. expect(disabled).not.toHaveProperty('mux');
  239. // Enabled mux on vless without flow: emitted
  240. const enabled = formValuesToWirePayload(rawOutboundToFormValues({
  241. protocol: 'vless',
  242. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: '', encryption: 'none' },
  243. mux: { enabled: true, concurrency: 8, xudpConcurrency: 16, xudpProxyUDP443: 'reject' },
  244. }));
  245. expect(enabled.mux).toMatchObject({ enabled: true });
  246. // Enabled mux on vless with vision flow: gated out
  247. const withFlow = formValuesToWirePayload(rawOutboundToFormValues({
  248. protocol: 'vless',
  249. settings: { address: 's', port: 443, id: '11111111-2222-4333-8444-555555555555', flow: 'xtls-rprx-vision', encryption: 'none' },
  250. mux: { enabled: true },
  251. }));
  252. expect(withFlow).not.toHaveProperty('mux');
  253. // Freedom (non-mux protocol): gated out even if enabled
  254. const freedom = formValuesToWirePayload(rawOutboundToFormValues({
  255. protocol: 'freedom',
  256. settings: {},
  257. mux: { enabled: true },
  258. }));
  259. expect(freedom).not.toHaveProperty('mux');
  260. });
  261. it('hysteria preserves address/port/version literal 2', () => {
  262. const back = formValuesToWirePayload(rawOutboundToFormValues({
  263. protocol: 'hysteria',
  264. settings: { address: 'h.example', port: 8443, version: 2 },
  265. }));
  266. expect(back.settings).toEqual({ address: 'h.example', port: 8443, version: 2 });
  267. });
  268. it('loopback inboundTag round-trips', () => {
  269. const back = formValuesToWirePayload(rawOutboundToFormValues({
  270. protocol: 'loopback',
  271. settings: { inboundTag: 'tagged-inbound' },
  272. }));
  273. expect(back.settings).toEqual({ inboundTag: 'tagged-inbound' });
  274. });
  275. it('unknown protocol falls back to vless without throwing', () => {
  276. const form = rawOutboundToFormValues({ protocol: 'mysterious', settings: {} });
  277. expect(form.protocol).toBe('vless');
  278. });
  279. });