model_wireguard_peers_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package model
  2. import (
  3. "encoding/json"
  4. "testing"
  5. )
  6. func wgSettingsParsed(t *testing.T, settings string) map[string]any {
  7. t.Helper()
  8. var parsed map[string]any
  9. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  10. t.Fatalf("unmarshal settings: %v", err)
  11. }
  12. return parsed
  13. }
  14. func TestWireguardClientsToPeers(t *testing.T) {
  15. settings := `{
  16. "secretKey": "c2VydmVyLXNlY3JldC1rZXktYmFzZTY0LTMyYnl0ZXM=",
  17. "mtu": 1420,
  18. "clients": [
  19. {"email": "alice", "enable": true, "publicKey": "cHVi", "allowedIPs": ["10.0.0.2/32"], "preSharedKey": "cHNr", "keepAlive": 25},
  20. {"email": "bob", "enable": false, "publicKey": "cHViMg==", "allowedIPs": ["10.0.0.3/32"]}
  21. ]
  22. }`
  23. out, ok := WireguardClientsToPeers(settings)
  24. if !ok {
  25. t.Fatal("WireguardClientsToPeers returned ok=false, want true")
  26. }
  27. parsed := wgSettingsParsed(t, out)
  28. if _, has := parsed["clients"]; has {
  29. t.Error("clients key must be removed after conversion")
  30. }
  31. if parsed["secretKey"] != "c2VydmVyLXNlY3JldC1rZXktYmFzZTY0LTMyYnl0ZXM=" {
  32. t.Errorf("secretKey not preserved: %v", parsed["secretKey"])
  33. }
  34. peers, ok := parsed["peers"].([]any)
  35. if !ok {
  36. t.Fatalf("peers not an array: %T", parsed["peers"])
  37. }
  38. if len(peers) != 1 {
  39. t.Fatalf("peers length = %d, want 1 (disabled client must be skipped)", len(peers))
  40. }
  41. peer := peers[0].(map[string]any)
  42. if peer["publicKey"] != "cHVi" {
  43. t.Errorf("peer publicKey = %v, want cHVi", peer["publicKey"])
  44. }
  45. if peer["preSharedKey"] != "cHNr" {
  46. t.Errorf("peer preSharedKey = %v, want cHNr", peer["preSharedKey"])
  47. }
  48. if peer["keepAlive"].(float64) != 25 {
  49. t.Errorf("peer keepAlive = %v, want 25", peer["keepAlive"])
  50. }
  51. ips, ok := peer["allowedIPs"].([]any)
  52. if !ok || len(ips) != 1 || ips[0] != "10.0.0.2/32" {
  53. t.Errorf("peer allowedIPs = %v, want [10.0.0.2/32]", peer["allowedIPs"])
  54. }
  55. }
  56. func TestWireguardClientsToPeersIdempotent(t *testing.T) {
  57. withPeers := `{"secretKey": "k", "peers": [{"publicKey": "cHVi"}]}`
  58. if out, ok := WireguardClientsToPeers(withPeers); ok || out != withPeers {
  59. t.Errorf("settings with peers must be a no-op: ok=%v out=%q", ok, out)
  60. }
  61. noClients := `{"secretKey": "k", "mtu": 1420}`
  62. if out, ok := WireguardClientsToPeers(noClients); ok || out != noClients {
  63. t.Errorf("settings without clients must be a no-op: ok=%v out=%q", ok, out)
  64. }
  65. }
  66. func TestGenXrayInboundConfigWireguardConvertsPeers(t *testing.T) {
  67. ib := &Inbound{
  68. Protocol: WireGuard,
  69. Port: 51820,
  70. Tag: "wg-in",
  71. Settings: `{"secretKey": "k", "peers": [], "clients": [{"email": "alice", "enable": true, "publicKey": "cHVi", "allowedIPs": ["10.0.0.2/32"]}]}`,
  72. }
  73. cfg := ib.GenXrayInboundConfig()
  74. parsed := wgSettingsParsed(t, string(cfg.Settings))
  75. if _, has := parsed["clients"]; has {
  76. t.Error("GenXrayInboundConfig left clients in a wireguard inbound")
  77. }
  78. peers, ok := parsed["peers"].([]any)
  79. if !ok || len(peers) != 1 {
  80. t.Fatalf("GenXrayInboundConfig did not emit peers: %v", parsed["peers"])
  81. }
  82. if peers[0].(map[string]any)["publicKey"] != "cHVi" {
  83. t.Errorf("peer publicKey = %v, want cHVi", peers[0].(map[string]any)["publicKey"])
  84. }
  85. }