1
0

client_amneziawg_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. // AmneziaWG's own kernel interface Address is exactly the configured
  9. // subnet (unlike WireGuard's Xray-native inbound), so allocation for it must
  10. // never widen past that subnet -- an address from outside it would be
  11. // silently unroutable. See PR #6105 Finding 12.
  12. func TestAllocateWireguardAddress_AmneziaWGNeverWidens(t *testing.T) {
  13. used := make([]string, 0, 254)
  14. for i := 2; i <= 255; i++ {
  15. used = append(used, fmt.Sprintf("10.8.1.%d/32", i))
  16. }
  17. if _, err := allocateWireguardAddress(used, "10.8.1.0/24", false); err == nil {
  18. t.Fatal("a full AmneziaWG /24 must fail loudly instead of allocating an address outside the interface's own subnet")
  19. }
  20. }
  21. func TestAllocateWireguardAddress_AmneziaWGFillsItsOwnSubnetNormally(t *testing.T) {
  22. got, err := allocateWireguardAddress([]string{"10.8.1.2/32"}, "10.8.1.0/24", false)
  23. if err != nil {
  24. t.Fatalf("allocateWireguardAddress: %v", err)
  25. }
  26. if got != "10.8.1.3/32" {
  27. t.Fatalf("address = %q, want 10.8.1.3/32", got)
  28. }
  29. }
  30. const amneziawgClientTestSettings = `{"server":{"subnetIp":"10.8.1.0","subnetCidr":24}}`
  31. func TestDefaultAmneziaWGSubnetBases(t *testing.T) {
  32. v4, v6, err := defaultAmneziaWGSubnetBases(amneziawgClientTestSettings)
  33. if err != nil {
  34. t.Fatalf("defaultAmneziaWGSubnetBases: %v", err)
  35. }
  36. if v4 != "10.8.1.0/24" {
  37. t.Fatalf("v4Base = %q, want 10.8.1.0/24", v4)
  38. }
  39. if v6 != "" {
  40. t.Fatalf("v6Base = %q, want empty when IPv6 is not enabled", v6)
  41. }
  42. }
  43. func TestDefaultAmneziaWGSubnetBasesIncludesIPv6WhenEnabled(t *testing.T) {
  44. settings := `{"server":{"subnetIp":"10.8.1.0","subnetCidr":24,"ipv6Enabled":true,"ipv6Subnet":"fd00::/64"}}`
  45. v4, v6, err := defaultAmneziaWGSubnetBases(settings)
  46. if err != nil {
  47. t.Fatalf("defaultAmneziaWGSubnetBases: %v", err)
  48. }
  49. if v4 != "10.8.1.0/24" || v6 != "fd00::/64" {
  50. t.Fatalf("got v4=%q v6=%q", v4, v6)
  51. }
  52. }
  53. func TestDefaultAmneziaWGSubnetBasesRejectsMissingServer(t *testing.T) {
  54. if _, _, err := defaultAmneziaWGSubnetBases(`{}`); err == nil {
  55. t.Fatal("expected an error when the settings have no server block")
  56. }
  57. }
  58. func TestDefaultAmneziaWGClientsGeneratesKeypairAndAllocatesFromOwnSubnet(t *testing.T) {
  59. clients := []model.Client{{Email: "a@awg"}}
  60. ifaces := []any{map[string]any{"email": "a@awg"}}
  61. if err := defaultAmneziaWGClients(amneziawgClientTestSettings, nil, clients, ifaces, nil); err != nil {
  62. t.Fatalf("defaultAmneziaWGClients: %v", err)
  63. }
  64. c := clients[0]
  65. if c.PrivateKey == "" || c.PublicKey == "" {
  66. t.Fatalf("keypair not generated: priv=%q pub=%q", c.PrivateKey, c.PublicKey)
  67. }
  68. if len(c.AllowedIPs) != 1 || c.AllowedIPs[0] != "10.8.1.2/32" {
  69. t.Fatalf("allowedIPs not allocated from the inbound's own subnet: %v", c.AllowedIPs)
  70. }
  71. }
  72. func TestDefaultAmneziaWGClientsPreservesProvided(t *testing.T) {
  73. clients := []model.Client{{
  74. Email: "b@awg",
  75. PrivateKey: "keep-priv",
  76. PublicKey: "keep-pub",
  77. AllowedIPs: []string{"10.8.1.50/32"},
  78. }}
  79. ifaces := []any{map[string]any{"email": "b@awg"}}
  80. if err := defaultAmneziaWGClients(amneziawgClientTestSettings, nil, clients, ifaces, nil); err != nil {
  81. t.Fatalf("defaultAmneziaWGClients: %v", err)
  82. }
  83. if clients[0].PrivateKey != "keep-priv" || clients[0].PublicKey != "keep-pub" {
  84. t.Fatalf("provided keys were rotated: %+v", clients[0])
  85. }
  86. if clients[0].AllowedIPs[0] != "10.8.1.50/32" {
  87. t.Fatalf("provided allowedIPs changed: %v", clients[0].AllowedIPs)
  88. }
  89. }
  90. func TestDefaultAmneziaWGClientsRejectsSameInboundDuplicate(t *testing.T) {
  91. existing := []model.Client{{Email: "old@awg", AllowedIPs: []string{"10.8.1.9/32"}}}
  92. dup := []model.Client{{Email: "new@awg", AllowedIPs: []string{"10.8.1.9/32"}}}
  93. err := defaultAmneziaWGClients(amneziawgClientTestSettings, existing, dup, []any{map[string]any{"email": "new@awg"}}, nil)
  94. if err == nil {
  95. t.Fatal("duplicate allowedIPs on the same inbound must be rejected")
  96. }
  97. }
  98. // The exact real-world scenario that motivated crossInboundUsed: a WireGuard
  99. // client and an AmneziaWG peer given the same address by habit. The
  100. // collision must be caught even though the two live on different inbounds
  101. // and neither appears in the other's own "existing" client list, and the
  102. // error should name the other inbound so an admin isn't left guessing.
  103. func TestDefaultAmneziaWGClientsRejectsCrossInboundDuplicate(t *testing.T) {
  104. crossUsed := map[string]string{"10.8.1.21/32": "inbound 'wg' (#12)"}
  105. dup := []model.Client{{Email: "c@awg", AllowedIPs: []string{"10.8.1.21/32"}}}
  106. err := defaultAmneziaWGClients(amneziawgClientTestSettings, nil, dup, []any{map[string]any{"email": "c@awg"}}, crossUsed)
  107. if err == nil {
  108. t.Fatal("allowedIPs already used on another inbound must be rejected")
  109. }
  110. if !strings.Contains(err.Error(), "inbound 'wg' (#12)") {
  111. t.Fatalf("error should name the other inbound holding the address, got: %v", err)
  112. }
  113. }
  114. func TestDefaultAmneziaWGClientsAutoAllocateSkipsCrossInboundUsed(t *testing.T) {
  115. crossUsed := map[string]string{"10.8.1.2/32": "inbound 'other-awg' (#3)"}
  116. clients := []model.Client{{Email: "d@awg"}}
  117. ifaces := []any{map[string]any{"email": "d@awg"}}
  118. if err := defaultAmneziaWGClients(amneziawgClientTestSettings, nil, clients, ifaces, crossUsed); err != nil {
  119. t.Fatalf("defaultAmneziaWGClients: %v", err)
  120. }
  121. if clients[0].AllowedIPs[0] != "10.8.1.3/32" {
  122. t.Fatalf("auto-allocation should skip the cross-inbound-used .2 and pick .3, got %v", clients[0].AllowedIPs)
  123. }
  124. }
  125. // Unlike WireGuard's allocation base (inferred from existing peers with a
  126. // fallback), AmneziaWG's base always comes from the inbound's own configured
  127. // subnet -- so this is really confirming crossInboundUsed can never change
  128. // which subnet is used, only which addresses within it are free.
  129. func TestDefaultAmneziaWGClientsCrossInboundUsedDoesNotChangeBase(t *testing.T) {
  130. crossUsed := map[string]string{"192.168.99.5/32": "inbound 'unrelated' (#99)"}
  131. clients := []model.Client{{Email: "e@awg"}}
  132. ifaces := []any{map[string]any{"email": "e@awg"}}
  133. if err := defaultAmneziaWGClients(amneziawgClientTestSettings, nil, clients, ifaces, crossUsed); err != nil {
  134. t.Fatalf("defaultAmneziaWGClients: %v", err)
  135. }
  136. if got := clients[0].AllowedIPs[0]; got != "10.8.1.2/32" {
  137. t.Fatalf("base subnet must stay the inbound's own 10.8.1.0/24; got %v", got)
  138. }
  139. }