wireguard-client-config.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, it } from 'vitest';
  2. import { buildWireguardClientConfig } from '@/pages/clients/wireguardConfig';
  3. import type { ClientRecord, InboundOption } from '@/hooks/useClients';
  4. const client: ClientRecord = {
  5. email: 'alice',
  6. privateKey: 'QGVlb2dXc1ZTWGw0ZXBzZndsWmtMaUM5MUlNYjBHWFdYbz0=',
  7. allowedIPs: '10.0.0.2/32',
  8. preSharedKey: 'cHNrLXZhbHVlLWZvci13aXJlZ3VhcmQtdGVzdC1jYXNlIQ==',
  9. keepAlive: 25,
  10. inboundIds: [90],
  11. };
  12. const inbound: InboundOption = {
  13. id: 90,
  14. tag: 'in-51820-udp',
  15. remark: 'wg-mc',
  16. protocol: 'wireguard',
  17. port: 51820,
  18. wgPublicKey: 'DGSYIcEKAUkA7HhzGSjxLZuV67BR3LeyU0BMLJzNVHQ=',
  19. wgMtu: 1420,
  20. };
  21. describe('buildWireguardClientConfig', () => {
  22. it('emits the canonical PresharedKey key, not PreSharedKey', () => {
  23. const cfg = buildWireguardClientConfig(client, inbound, 'example.com', '');
  24. expect(cfg).toContain(`PresharedKey = ${client.preSharedKey}`);
  25. expect(cfg).not.toContain('PreSharedKey =');
  26. });
  27. it('defaults DNS to 1.1.1.1, 1.0.0.1 when the inbound sets none', () => {
  28. const cfg = buildWireguardClientConfig(client, inbound, 'example.com', '');
  29. expect(cfg).toContain('DNS = 1.1.1.1, 1.0.0.1');
  30. });
  31. it('uses the inbound DNS override when present', () => {
  32. const cfg = buildWireguardClientConfig(client, { ...inbound, wgDns: '9.9.9.9' }, 'example.com', '');
  33. expect(cfg).toContain('DNS = 9.9.9.9');
  34. expect(cfg).not.toContain('DNS = 1.1.1.1, 1.0.0.1');
  35. });
  36. it('builds the endpoint from host, port, MTU and server public key', () => {
  37. const cfg = buildWireguardClientConfig(client, inbound, 'example.com', '');
  38. expect(cfg).toContain('Endpoint = example.com:51820');
  39. expect(cfg).toContain('MTU = 1420');
  40. expect(cfg).toContain(`PublicKey = ${inbound.wgPublicKey}`);
  41. expect(cfg).toContain('PersistentKeepalive = 25');
  42. });
  43. it('omits the PresharedKey line when the client has no preshared key', () => {
  44. const cfg = buildWireguardClientConfig({ ...client, preSharedKey: undefined }, inbound, 'example.com', '');
  45. expect(cfg).not.toContain('PresharedKey');
  46. });
  47. });