instance_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package amneziawg
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. func mkInboundSettings(t *testing.T, server *ServerSettings, clients []model.Client) string {
  8. t.Helper()
  9. bs, err := json.Marshal(InboundSettings{Server: server, Clients: clients})
  10. if err != nil {
  11. t.Fatalf("marshal settings: %v", err)
  12. }
  13. return string(bs)
  14. }
  15. func validServer() *ServerSettings {
  16. return &ServerSettings{
  17. PrivateKey: "serverPriv",
  18. PublicKey: "serverPub",
  19. SubnetIP: "10.8.1.0",
  20. SubnetCIDR: 24,
  21. }
  22. }
  23. func TestInstanceFromInboundParsesEnabledPeers(t *testing.T) {
  24. settings := mkInboundSettings(t, validServer(), []model.Client{
  25. {Email: "a@x", Enable: true, PublicKey: "pubA", PreSharedKey: "pskA", AllowedIPs: []string{"10.8.1.2/32"}},
  26. {Email: "b@x", Enable: false, PublicKey: "pubB", AllowedIPs: []string{"10.8.1.3/32"}},
  27. {Email: "c@x", Enable: true, PublicKey: "", AllowedIPs: []string{"10.8.1.4/32"}}, // no key: skipped
  28. {Email: "d@x", Enable: true, PublicKey: "pubD", AllowedIPs: nil}, // no address: skipped
  29. })
  30. ib := &model.Inbound{Id: 7, Tag: "awg-tag", Protocol: model.AmneziaWG, Port: 51820, Settings: settings}
  31. inst, ok := InstanceFromInbound(ib)
  32. if !ok {
  33. t.Fatal("expected a usable instance")
  34. }
  35. if inst.Id != 7 || inst.Tag != "awg-tag" || inst.ListenPort != 51820 {
  36. t.Fatalf("instance identity not carried over: %+v", inst)
  37. }
  38. if inst.InterfaceName != "awg7" {
  39. t.Fatalf("InterfaceName = %q, want awg7", inst.InterfaceName)
  40. }
  41. if len(inst.Address) != 1 || inst.Address[0] != "10.8.1.1/24" {
  42. t.Fatalf("Address = %v, want [10.8.1.1/24]", inst.Address)
  43. }
  44. if len(inst.Peers) != 1 {
  45. t.Fatalf("Peers = %+v, want exactly 1 (only a@x qualifies)", inst.Peers)
  46. }
  47. p := inst.Peers[0]
  48. if p.Email != "a@x" || p.PublicKey != "pubA" || p.PresharedKey != "pskA" || len(p.AllowedIPs) != 1 || p.AllowedIPs[0] != "10.8.1.2/32" {
  49. t.Fatalf("peer mismatch: %+v", p)
  50. }
  51. }
  52. func TestInstanceFromInboundCopiesAWG30Fields(t *testing.T) {
  53. server := validServer()
  54. server.S1, server.S2, server.S3, server.S4 = 20, 20, 20, 20
  55. server.HeaderProtectionKey = "some-header-protection-key"
  56. server.ContentPaddingAddition = "50-100"
  57. settings := mkInboundSettings(t, server, []model.Client{
  58. {Email: "a@x", Enable: true, PublicKey: "pubA", AllowedIPs: []string{"10.8.1.2/32"}},
  59. })
  60. ib := &model.Inbound{Id: 7, Protocol: model.AmneziaWG, Port: 51820, Settings: settings}
  61. inst, ok := InstanceFromInbound(ib)
  62. if !ok {
  63. t.Fatal("expected a usable instance")
  64. }
  65. if inst.Obfuscation.HeaderProtectionKey != "some-header-protection-key" {
  66. t.Fatalf("HeaderProtectionKey = %q, want it copied from ServerSettings", inst.Obfuscation.HeaderProtectionKey)
  67. }
  68. if inst.Obfuscation.ContentPaddingAddition != "50-100" {
  69. t.Fatalf("ContentPaddingAddition = %q, want it copied from ServerSettings", inst.Obfuscation.ContentPaddingAddition)
  70. }
  71. }
  72. func TestInstanceFromInboundRejectsWrongProtocol(t *testing.T) {
  73. settings := mkInboundSettings(t, validServer(), []model.Client{
  74. {Email: "a@x", Enable: true, PublicKey: "pubA", AllowedIPs: []string{"10.8.1.2/32"}},
  75. })
  76. ib := &model.Inbound{Id: 1, Protocol: model.VLESS, Settings: settings}
  77. if _, ok := InstanceFromInbound(ib); ok {
  78. t.Fatal("non-AmneziaWG inbound must be rejected")
  79. }
  80. }
  81. func TestInstanceFromInboundRejectsNil(t *testing.T) {
  82. if _, ok := InstanceFromInbound(nil); ok {
  83. t.Fatal("nil inbound must be rejected")
  84. }
  85. }
  86. func TestInstanceFromInboundRejectsMissingServer(t *testing.T) {
  87. ib := &model.Inbound{Id: 1, Protocol: model.AmneziaWG, Settings: `{"clients":[]}`}
  88. if _, ok := InstanceFromInbound(ib); ok {
  89. t.Fatal("settings with no server block must be rejected")
  90. }
  91. }
  92. func TestInstanceFromInboundRejectsUnparseableSettings(t *testing.T) {
  93. ib := &model.Inbound{Id: 1, Protocol: model.AmneziaWG, Settings: `not json`}
  94. if _, ok := InstanceFromInbound(ib); ok {
  95. t.Fatal("unparseable settings must be rejected")
  96. }
  97. }
  98. func TestInstanceFromInboundEmptyWhenNoEnabledPeers(t *testing.T) {
  99. settings := mkInboundSettings(t, validServer(), []model.Client{
  100. {Email: "a@x", Enable: false, PublicKey: "pubA", AllowedIPs: []string{"10.8.1.2/32"}},
  101. })
  102. ib := &model.Inbound{Id: 1, Protocol: model.AmneziaWG, Settings: settings}
  103. if _, ok := InstanceFromInbound(ib); ok {
  104. t.Fatal("an inbound with zero enabled peers must be skipped, like mtproto.InstanceFromInbound")
  105. }
  106. }
  107. func TestServerAddress(t *testing.T) {
  108. cases := []struct {
  109. subnet string
  110. cidr int
  111. want string
  112. }{
  113. {"10.8.1.0", 24, "10.8.1.1/24"},
  114. {"10.8.1.0", 0, "10.8.1.1/24"}, // cidr <= 0 defaults to /24
  115. {"10.8.1.5", 24, "10.8.1.1/24"}, // non-network base: must not collide with peer allocation starting at .2
  116. {"10.8.1.254", 24, "10.8.1.1/24"},
  117. {"192.168.5.10", 32, "192.168.5.10/32"}, // /32 has no host bits: used as-is
  118. }
  119. for _, c := range cases {
  120. if got := serverAddress(c.subnet, c.cidr); got != c.want {
  121. t.Errorf("serverAddress(%q, %d) = %q, want %q", c.subnet, c.cidr, got, c.want)
  122. }
  123. }
  124. }
  125. func TestInterfaceNameForID(t *testing.T) {
  126. if got := interfaceNameForID(42); got != "awg42" {
  127. t.Errorf("interfaceNameForID(42) = %q, want awg42", got)
  128. }
  129. }
  130. func TestFirstIPv4(t *testing.T) {
  131. cases := []struct {
  132. name string
  133. ips []string
  134. want string
  135. }{
  136. {"single v4 CIDR", []string{"10.8.1.2/32"}, "10.8.1.2"},
  137. {"bare v4 address, no mask", []string{"10.8.1.2"}, "10.8.1.2"},
  138. {"v6 first, v4 second", []string{"fd86:ea04:1115::2/128", "10.8.1.2/32"}, "10.8.1.2"},
  139. {"v4-only among several", []string{"10.8.1.2/32", "10.8.1.3/32"}, "10.8.1.2"},
  140. {"v6 only", []string{"fd86:ea04:1115::2/128"}, ""},
  141. {"empty input", nil, ""},
  142. {"unparseable entries skipped", []string{"not-an-ip", "10.8.1.2/32"}, "10.8.1.2"},
  143. }
  144. for _, c := range cases {
  145. if got := FirstIPv4(c.ips); got != c.want {
  146. t.Errorf("%s: FirstIPv4(%v) = %q, want %q", c.name, c.ips, got, c.want)
  147. }
  148. }
  149. }
  150. func TestFirstIPv6(t *testing.T) {
  151. cases := []struct {
  152. name string
  153. ips []string
  154. want string
  155. }{
  156. {"single v6 CIDR", []string{"fd86:ea04:1115::2/128"}, "fd86:ea04:1115::2"},
  157. {"bare v6 address, no mask", []string{"fd86:ea04:1115::2"}, "fd86:ea04:1115::2"},
  158. {"v4 first, v6 second", []string{"10.8.1.2/32", "fd86:ea04:1115::2/128"}, "fd86:ea04:1115::2"},
  159. {"only first of two v6 entries returned", []string{"fd86:ea04:1115::2/128", "fd86:ea04:1115::3/128"}, "fd86:ea04:1115::2"},
  160. {"v4 only", []string{"10.8.1.2/32"}, ""},
  161. {"empty input", nil, ""},
  162. {"unparseable entries skipped", []string{"not-an-ip", "fd86:ea04:1115::2/128"}, "fd86:ea04:1115::2"},
  163. {"v4-mapped v6 is not a real v6 identity", []string{"::ffff:10.8.1.2/128"}, ""},
  164. }
  165. for _, c := range cases {
  166. if got := FirstIPv6(c.ips); got != c.want {
  167. t.Errorf("%s: FirstIPv6(%v) = %q, want %q", c.name, c.ips, got, c.want)
  168. }
  169. }
  170. }