inbound-link.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /// <reference types="vite/client" />
  2. import { describe, expect, it } from 'vitest';
  3. import {
  4. genHysteriaLink,
  5. genInboundLinks,
  6. genShadowsocksLink,
  7. genTrojanLink,
  8. genVlessLink,
  9. genVmessLink,
  10. genWireguardConfig,
  11. genWireguardLink,
  12. resolveAddr,
  13. } from '@/lib/xray/inbound-link';
  14. import { InboundSchema } from '@/schemas/api/inbound';
  15. import type { WireguardInboundSettings } from '@/schemas/protocols/inbound/wireguard';
  16. // Snapshot baseline for the share-link generators. Snapshots were locked
  17. // at the close of the legacy class migration — at that point each
  18. // generator was verified byte-equal to the corresponding legacy Inbound
  19. // class method. Future drift past this baseline is a regression.
  20. const fullFixtures = import.meta.glob<unknown>(
  21. './golden/fixtures/inbound-full/*.json',
  22. { eager: true, import: 'default' },
  23. );
  24. function fixtureName(path: string): string {
  25. const file = path.split('/').pop() ?? path;
  26. return file.replace(/\.json$/, '');
  27. }
  28. function fixturesForProtocol(protocol: string): Array<[string, Record<string, unknown>]> {
  29. return Object.entries(fullFixtures)
  30. .filter(([, raw]) => (raw as { protocol?: string }).protocol === protocol)
  31. .map(([path, raw]): [string, Record<string, unknown>] => [fixtureName(path), raw as Record<string, unknown>])
  32. .sort(([a], [b]) => a.localeCompare(b));
  33. }
  34. describe('genVmessLink', () => {
  35. const fixtures = fixturesForProtocol('vmess');
  36. expect(fixtures.length, 'need at least one vmess full-inbound fixture').toBeGreaterThan(0);
  37. for (const [name, raw] of fixtures) {
  38. it(`${name}: byte-stable`, () => {
  39. const typed = InboundSchema.parse(raw);
  40. const settings = (raw as { settings: { clients: Array<{ id: string; security?: string }> } }).settings;
  41. const client = settings.clients[0];
  42. const link = genVmessLink({
  43. inbound: typed,
  44. address: 'example.test',
  45. port: typed.port,
  46. forceTls: 'same',
  47. remark: 'parity-test',
  48. clientId: client.id,
  49. security: client.security as never,
  50. externalProxy: null,
  51. });
  52. expect(link).toMatchSnapshot();
  53. });
  54. }
  55. });
  56. describe('genVlessLink', () => {
  57. const fixtures = fixturesForProtocol('vless');
  58. expect(fixtures.length, 'need at least one vless full-inbound fixture').toBeGreaterThan(0);
  59. for (const [name, raw] of fixtures) {
  60. it(`${name}: byte-stable`, () => {
  61. const typed = InboundSchema.parse(raw);
  62. const settings = (raw as { settings: { clients: Array<{ id: string; flow?: string }> } }).settings;
  63. const client = settings.clients[0];
  64. const link = genVlessLink({
  65. inbound: typed,
  66. address: 'example.test',
  67. port: typed.port,
  68. forceTls: 'same',
  69. remark: 'parity-test',
  70. clientId: client.id,
  71. flow: client.flow as never,
  72. externalProxy: null,
  73. });
  74. expect(link).toMatchSnapshot();
  75. });
  76. }
  77. });
  78. describe('genTrojanLink', () => {
  79. const fixtures = fixturesForProtocol('trojan');
  80. expect(fixtures.length, 'need at least one trojan full-inbound fixture').toBeGreaterThan(0);
  81. for (const [name, raw] of fixtures) {
  82. it(`${name}: byte-stable`, () => {
  83. const typed = InboundSchema.parse(raw);
  84. const settings = (raw as { settings: { clients: Array<{ password: string }> } }).settings;
  85. const client = settings.clients[0];
  86. const link = genTrojanLink({
  87. inbound: typed,
  88. address: 'example.test',
  89. port: typed.port,
  90. forceTls: 'same',
  91. remark: 'parity-test',
  92. clientPassword: client.password,
  93. externalProxy: null,
  94. });
  95. expect(link).toMatchSnapshot();
  96. });
  97. }
  98. });
  99. describe('genHysteriaLink', () => {
  100. const fixtures = fixturesForProtocol('hysteria');
  101. expect(fixtures.length, 'need at least one hysteria full-inbound fixture').toBeGreaterThan(0);
  102. for (const [name, raw] of fixtures) {
  103. it(`${name}: byte-stable`, () => {
  104. const typed = InboundSchema.parse(raw);
  105. const settings = (raw as { settings: { clients: Array<{ auth: string }> } }).settings;
  106. const client = settings.clients[0];
  107. const link = genHysteriaLink({
  108. inbound: typed,
  109. address: 'example.test',
  110. port: typed.port,
  111. remark: 'parity-test',
  112. clientAuth: client.auth,
  113. });
  114. expect(link).toMatchSnapshot();
  115. });
  116. }
  117. });
  118. describe('genWireguardLink + genWireguardConfig', () => {
  119. const fixtures = fixturesForProtocol('wireguard');
  120. expect(fixtures.length, 'need at least one wireguard full-inbound fixture').toBeGreaterThan(0);
  121. for (const [name, raw] of fixtures) {
  122. it(`${name}: byte-stable`, () => {
  123. const typed = InboundSchema.parse(raw);
  124. if (typed.protocol !== 'wireguard') throw new Error('not a wireguard fixture');
  125. // InboundSchema is an intersection of two DUs, so TS can't auto-narrow
  126. // `settings` from `protocol`. The runtime guard above is the real
  127. // check; this cast just helps the type checker.
  128. const settings = typed.settings as WireguardInboundSettings;
  129. const link = genWireguardLink({
  130. settings,
  131. address: 'wg.example.test',
  132. port: typed.port,
  133. remark: 'wg-peer-1',
  134. peerIndex: 0,
  135. });
  136. const config = genWireguardConfig({
  137. settings,
  138. address: 'wg.example.test',
  139. port: typed.port,
  140. remark: 'wg-peer-1',
  141. peerIndex: 0,
  142. });
  143. expect({ link, config }).toMatchSnapshot();
  144. });
  145. }
  146. });
  147. describe('resolveAddr precedence', () => {
  148. const baseInbound = {
  149. listen: '',
  150. port: 443,
  151. protocol: 'vless' as const,
  152. };
  153. it('prefers hostOverride over listen and fallback', () => {
  154. expect(resolveAddr(
  155. { ...baseInbound, listen: '10.0.0.1' } as never,
  156. 'cdn.example.test',
  157. 'fallback.test',
  158. )).toBe('cdn.example.test');
  159. });
  160. it('uses listen when override is empty and listen is explicit', () => {
  161. expect(resolveAddr(
  162. { ...baseInbound, listen: '10.0.0.1' } as never,
  163. '',
  164. 'fallback.test',
  165. )).toBe('10.0.0.1');
  166. });
  167. it('skips listen when it is 0.0.0.0 and falls through to fallbackHostname', () => {
  168. expect(resolveAddr(
  169. { ...baseInbound, listen: '0.0.0.0' } as never,
  170. '',
  171. 'fallback.test',
  172. )).toBe('fallback.test');
  173. });
  174. it('falls through to fallbackHostname when listen is empty', () => {
  175. expect(resolveAddr(
  176. baseInbound as never,
  177. '',
  178. 'fallback.test',
  179. )).toBe('fallback.test');
  180. });
  181. });
  182. describe('genInboundLinks orchestrator', () => {
  183. // Every full-inbound fixture should produce the same \r\n-joined link
  184. // block at this baseline.
  185. const fixtures = Object.entries(fullFixtures)
  186. .map(([path, raw]): [string, Record<string, unknown>] => [fixtureName(path), raw as Record<string, unknown>])
  187. .sort(([a], [b]) => a.localeCompare(b));
  188. for (const [name, raw] of fixtures) {
  189. const protocol = (raw as { protocol?: string }).protocol;
  190. // Skip hysteria2 — the legacy class had no dispatch case at the time
  191. // the baseline was locked, so no snapshot exists. The new orchestrator
  192. // covers it via its own logic and the genHysteriaLink unit test.
  193. if (protocol === 'hysteria2') continue;
  194. it(`${name}: byte-stable`, () => {
  195. const typed = InboundSchema.parse(raw);
  196. const block = genInboundLinks({
  197. inbound: typed,
  198. remark: 'parity-test',
  199. hostOverride: 'override.test',
  200. fallbackHostname: 'fallback.test',
  201. });
  202. expect(block).toMatchSnapshot();
  203. });
  204. }
  205. });
  206. describe('genShadowsocksLink', () => {
  207. const fixtures = fixturesForProtocol('shadowsocks');
  208. expect(fixtures.length, 'need at least one shadowsocks full-inbound fixture').toBeGreaterThan(0);
  209. for (const [name, raw] of fixtures) {
  210. it(`${name}: byte-stable`, () => {
  211. const typed = InboundSchema.parse(raw);
  212. const settings = (raw as { settings: { clients?: Array<{ password: string }> } }).settings;
  213. const client = settings.clients?.[0];
  214. const link = genShadowsocksLink({
  215. inbound: typed,
  216. address: 'example.test',
  217. port: typed.port,
  218. forceTls: 'same',
  219. remark: 'parity-test',
  220. clientPassword: client?.password ?? '',
  221. externalProxy: null,
  222. });
  223. expect(link).toMatchSnapshot();
  224. });
  225. }
  226. });