client_wireguard_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package service
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
  7. )
  8. func TestAllocateWireguardAddress(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. used []string
  12. base string
  13. want string
  14. err bool
  15. }{
  16. {name: "empty starts at .2", used: nil, base: "10.0.0.0/24", want: "10.0.0.2/32"},
  17. {name: "skips used", used: []string{"10.0.0.2/32"}, base: "10.0.0.0/24", want: "10.0.0.3/32"},
  18. {name: "fills gap", used: []string{"10.0.0.3/32", "10.0.0.4/32"}, base: "10.0.0.0/24", want: "10.0.0.2/32"},
  19. {name: "ignores catch-all", used: []string{"0.0.0.0/0", "::/0"}, base: "10.0.0.0/24", want: "10.0.0.2/32"},
  20. {name: "default base when empty", used: nil, base: "", want: "10.0.0.2/32"},
  21. {name: "full ipv4 scope widens instead of failing", used: []string{"10.9.0.2/32", "10.9.0.3/32"}, base: "10.9.0.0/30", want: "10.9.0.4/32"},
  22. {name: "exhausted ipv6 scope errors", used: []string{"fd00::2/128", "fd00::3/128"}, base: "fd00::/126", err: true},
  23. }
  24. for _, tt := range tests {
  25. t.Run(tt.name, func(t *testing.T) {
  26. got, err := allocateWireguardAddress(tt.used, tt.base)
  27. if tt.err {
  28. if err == nil {
  29. t.Fatalf("expected error, got %q", got)
  30. }
  31. return
  32. }
  33. if err != nil {
  34. t.Fatalf("unexpected error: %v", err)
  35. }
  36. if got != tt.want {
  37. t.Fatalf("got %q, want %q", got, tt.want)
  38. }
  39. })
  40. }
  41. }
  42. func TestDefaultWireguardClientsGeneratesKeypair(t *testing.T) {
  43. clients := []model.Client{{Email: "a@wg"}}
  44. ifaces := []any{map[string]any{"email": "a@wg"}}
  45. if err := defaultWireguardClients(nil, clients, ifaces); err != nil {
  46. t.Fatalf("defaultWireguardClients: %v", err)
  47. }
  48. c := clients[0]
  49. if c.PrivateKey == "" || c.PublicKey == "" {
  50. t.Fatalf("keypair not generated: priv=%q pub=%q", c.PrivateKey, c.PublicKey)
  51. }
  52. if len(c.AllowedIPs) != 1 || c.AllowedIPs[0] != "10.0.0.2/32" {
  53. t.Fatalf("allowedIPs not allocated: %v", c.AllowedIPs)
  54. }
  55. m := ifaces[0].(map[string]any)
  56. if m["privateKey"] != c.PrivateKey || m["publicKey"] != c.PublicKey {
  57. t.Fatalf("interface map not updated: %v", m)
  58. }
  59. }
  60. func TestDefaultWireguardClientsDerivesPublicKey(t *testing.T) {
  61. priv, _, err := wgutil.GenerateWireguardKeypair()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. wantPub, err := wgutil.PublicKeyFromPrivate(priv)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. clients := []model.Client{{Email: "b@wg", PrivateKey: priv}}
  70. ifaces := []any{map[string]any{"email": "b@wg"}}
  71. if err := defaultWireguardClients(nil, clients, ifaces); err != nil {
  72. t.Fatalf("defaultWireguardClients: %v", err)
  73. }
  74. if clients[0].PublicKey != wantPub {
  75. t.Fatalf("derived public key = %q, want %q", clients[0].PublicKey, wantPub)
  76. }
  77. }
  78. func TestDefaultWireguardClientsPreservesProvided(t *testing.T) {
  79. clients := []model.Client{{
  80. Email: "c@wg",
  81. PrivateKey: "keep-priv",
  82. PublicKey: "keep-pub",
  83. AllowedIPs: []string{"10.0.0.50/32"},
  84. }}
  85. ifaces := []any{map[string]any{"email": "c@wg"}}
  86. if err := defaultWireguardClients(nil, clients, ifaces); err != nil {
  87. t.Fatalf("defaultWireguardClients: %v", err)
  88. }
  89. if clients[0].PrivateKey != "keep-priv" || clients[0].PublicKey != "keep-pub" {
  90. t.Fatalf("provided keys were rotated: %+v", clients[0])
  91. }
  92. if clients[0].AllowedIPs[0] != "10.0.0.50/32" {
  93. t.Fatalf("provided allowedIPs changed: %v", clients[0].AllowedIPs)
  94. }
  95. }
  96. func TestWireguardAllocationBase(t *testing.T) {
  97. tests := []struct {
  98. name string
  99. used []string
  100. fallback string
  101. want string
  102. }{
  103. {name: "no peers uses fallback", used: nil, fallback: "10.0.0.0/24", want: "10.0.0.0/24"},
  104. {name: "derives subnet from existing peer", used: []string{"172.16.0.2/32"}, fallback: "10.0.0.0/24", want: "172.16.0.0/24"},
  105. {name: "skips catch-all and ipv6", used: []string{"0.0.0.0/0", "::/0", "fd00::2/128", "192.168.5.7/32"}, fallback: "10.0.0.0/24", want: "192.168.5.0/24"},
  106. }
  107. for _, tt := range tests {
  108. t.Run(tt.name, func(t *testing.T) {
  109. if got := wireguardAllocationBase(tt.used, tt.fallback); got != tt.want {
  110. t.Fatalf("got %q, want %q", got, tt.want)
  111. }
  112. })
  113. }
  114. }
  115. func TestDefaultWireguardClientsHonorsExistingSubnet(t *testing.T) {
  116. existing := []model.Client{{Email: "old@wg", AllowedIPs: []string{"172.16.0.2/32"}}}
  117. clients := []model.Client{{Email: "new@wg"}}
  118. ifaces := []any{map[string]any{"email": "new@wg"}}
  119. if err := defaultWireguardClients(existing, clients, ifaces); err != nil {
  120. t.Fatalf("defaultWireguardClients: %v", err)
  121. }
  122. if got := clients[0].AllowedIPs[0]; got != "172.16.0.3/32" {
  123. t.Fatalf("new client address = %q, want 172.16.0.3/32 in existing subnet", got)
  124. }
  125. }
  126. func TestAllocateWireguardAddressWidensPastFullSlash24(t *testing.T) {
  127. used := make([]string, 0, 254)
  128. for i := 2; i <= 255; i++ {
  129. used = append(used, fmt.Sprintf("10.0.0.%d/32", i))
  130. }
  131. got, err := allocateWireguardAddress(used, "10.0.0.0/24")
  132. if err != nil {
  133. t.Fatalf("allocate with a full /24: %v", err)
  134. }
  135. if got != "10.0.1.0/32" {
  136. t.Fatalf("address after a full /24 = %q, want 10.0.1.0/32", got)
  137. }
  138. used = append(used, got)
  139. next, err := allocateWireguardAddress(used, "10.0.0.0/24")
  140. if err != nil {
  141. t.Fatalf("allocate after widening: %v", err)
  142. }
  143. if next != "10.0.1.1/32" {
  144. t.Fatalf("second widened address = %q, want 10.0.1.1/32", next)
  145. }
  146. }
  147. func TestAllocateWireguardAddressFillsItsOwnSlash24First(t *testing.T) {
  148. got, err := allocateWireguardAddress([]string{"172.16.0.2/32"}, "172.16.0.0/24")
  149. if err != nil {
  150. t.Fatalf("allocateWireguardAddress: %v", err)
  151. }
  152. if got != "172.16.0.3/32" {
  153. t.Fatalf("address = %q, want 172.16.0.3/32 — the inbound's own /24 comes first", got)
  154. }
  155. }
  156. func TestDefaultWireguardClientsAllocatesDistinctIPs(t *testing.T) {
  157. clients := []model.Client{{Email: "x@wg"}, {Email: "y@wg"}}
  158. ifaces := []any{map[string]any{"email": "x@wg"}, map[string]any{"email": "y@wg"}}
  159. if err := defaultWireguardClients(nil, clients, ifaces); err != nil {
  160. t.Fatalf("defaultWireguardClients: %v", err)
  161. }
  162. if clients[0].AllowedIPs[0] == clients[1].AllowedIPs[0] {
  163. t.Fatalf("two clients got the same address: %v", clients[0].AllowedIPs)
  164. }
  165. }
  166. func TestNormalizeWireguardAllowedIPs(t *testing.T) {
  167. tests := []struct {
  168. name string
  169. in []string
  170. want []string
  171. err bool
  172. }{
  173. {name: "cidr passes through", in: []string{"10.0.0.5/32"}, want: []string{"10.0.0.5/32"}},
  174. {name: "bare ipv4 becomes /32", in: []string{"10.0.0.5"}, want: []string{"10.0.0.5/32"}},
  175. {name: "bare ipv6 becomes /128", in: []string{"fd00::5"}, want: []string{"fd00::5/128"}},
  176. {name: "trims and drops empties", in: []string{" 10.0.0.5/32 ", "", " "}, want: []string{"10.0.0.5/32"}},
  177. {name: "dedupes", in: []string{"10.0.0.5/32", "10.0.0.5/32"}, want: []string{"10.0.0.5/32"}},
  178. {name: "routed subnet allowed", in: []string{"10.0.0.5/32", "192.168.1.0/24"}, want: []string{"10.0.0.5/32", "192.168.1.0/24"}},
  179. {name: "garbage rejected", in: []string{"not-an-ip"}, err: true},
  180. {name: "bad prefix rejected", in: []string{"10.0.0.5/99"}, err: true},
  181. }
  182. for _, tt := range tests {
  183. t.Run(tt.name, func(t *testing.T) {
  184. got, err := normalizeWireguardAllowedIPs(tt.in)
  185. if tt.err {
  186. if err == nil {
  187. t.Fatalf("expected error, got %v", got)
  188. }
  189. return
  190. }
  191. if err != nil {
  192. t.Fatalf("unexpected error: %v", err)
  193. }
  194. if len(got) != len(tt.want) {
  195. t.Fatalf("got %v, want %v", got, tt.want)
  196. }
  197. for i := range got {
  198. if got[i] != tt.want[i] {
  199. t.Fatalf("got %v, want %v", got, tt.want)
  200. }
  201. }
  202. })
  203. }
  204. }
  205. func TestDefaultWireguardClientsHonorsAndValidatesSuppliedAllowedIPs(t *testing.T) {
  206. existing := []model.Client{{Email: "old@wg", AllowedIPs: []string{"10.0.0.2/32"}}}
  207. clients := []model.Client{{Email: "c@wg", AllowedIPs: []string{"10.0.0.9"}}}
  208. ifaces := []any{map[string]any{"email": "c@wg"}}
  209. if err := defaultWireguardClients(existing, clients, ifaces); err != nil {
  210. t.Fatalf("defaultWireguardClients: %v", err)
  211. }
  212. if len(clients[0].AllowedIPs) != 1 || clients[0].AllowedIPs[0] != "10.0.0.9/32" {
  213. t.Fatalf("supplied allowedIPs not normalized: %v", clients[0].AllowedIPs)
  214. }
  215. dup := []model.Client{{Email: "d@wg", AllowedIPs: []string{"10.0.0.2/32"}}}
  216. err := defaultWireguardClients(existing, dup, []any{map[string]any{"email": "d@wg"}})
  217. if err == nil {
  218. t.Fatal("duplicate allowedIPs across clients must be rejected")
  219. }
  220. bad := []model.Client{{Email: "e@wg", AllowedIPs: []string{"not-an-ip"}}}
  221. if err := defaultWireguardClients(existing, bad, []any{map[string]any{"email": "e@wg"}}); err == nil {
  222. t.Fatal("invalid allowedIPs entry must be rejected")
  223. }
  224. }