inbound_options_node_address_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. )
  7. // TestGetInboundOptions_NodeAddress verifies that a node-managed inbound carries
  8. // its hosting node's externally reachable address, while this panel's own
  9. // inbounds report an empty NodeAddress. The clients page uses it as the
  10. // WireGuard endpoint host so a copied config points at the node, not the master.
  11. func TestGetInboundOptions_NodeAddress(t *testing.T) {
  12. setupConflictDB(t)
  13. node := &model.Node{Name: "de-fra-1", Address: "node.example.net", Port: 2053, Enable: true}
  14. if err := database.GetDB().Create(node).Error; err != nil {
  15. t.Fatalf("create node: %v", err)
  16. }
  17. nodeInbound := &model.Inbound{
  18. UserId: 1,
  19. Tag: "in-51820-udp",
  20. Enable: true,
  21. Listen: "0.0.0.0",
  22. Port: 51820,
  23. Protocol: model.WireGuard,
  24. Settings: `{"clients":[],"secretKey":"QGVlb2dXc1ZTWGw0ZXBzZndsWmtMaUM5MUlNYjBHWFdYbz0="}`,
  25. NodeID: &node.Id,
  26. }
  27. localInbound := &model.Inbound{
  28. UserId: 1,
  29. Tag: "in-443-tcp",
  30. Enable: true,
  31. Listen: "0.0.0.0",
  32. Port: 443,
  33. Protocol: model.VLESS,
  34. StreamSettings: `{"network":"tcp"}`,
  35. Settings: `{"clients":[]}`,
  36. ShareAddrStrategy: "custom",
  37. ShareAddr: "vpn.example.com",
  38. }
  39. if err := database.GetDB().Create(nodeInbound).Error; err != nil {
  40. t.Fatalf("create node inbound: %v", err)
  41. }
  42. if err := database.GetDB().Create(localInbound).Error; err != nil {
  43. t.Fatalf("create local inbound: %v", err)
  44. }
  45. svc := &InboundService{}
  46. options, err := svc.GetInboundOptions(1)
  47. if err != nil {
  48. t.Fatalf("GetInboundOptions: %v", err)
  49. }
  50. byID := make(map[int]InboundOption, len(options))
  51. for _, o := range options {
  52. byID[o.Id] = o
  53. }
  54. got, ok := byID[nodeInbound.Id]
  55. if !ok {
  56. t.Fatalf("node inbound %d missing from options", nodeInbound.Id)
  57. }
  58. if got.NodeAddress != "node.example.net" {
  59. t.Fatalf("node inbound NodeAddress = %q, want node.example.net", got.NodeAddress)
  60. }
  61. if got.Listen != "0.0.0.0" {
  62. t.Fatalf("node inbound Listen = %q, want 0.0.0.0", got.Listen)
  63. }
  64. if got.ShareAddrStrategy != "" {
  65. t.Fatalf("node inbound ShareAddrStrategy = %q, want empty (the default node strategy is elided so omitempty drops it)", got.ShareAddrStrategy)
  66. }
  67. local, ok := byID[localInbound.Id]
  68. if !ok {
  69. t.Fatalf("local inbound %d missing from options", localInbound.Id)
  70. }
  71. if local.NodeAddress != "" {
  72. t.Fatalf("local inbound NodeAddress = %q, want empty", local.NodeAddress)
  73. }
  74. if local.ShareAddrStrategy != "custom" {
  75. t.Fatalf("local inbound ShareAddrStrategy = %q, want custom", local.ShareAddrStrategy)
  76. }
  77. if local.ShareAddr != "vpn.example.com" {
  78. t.Fatalf("local inbound ShareAddr = %q, want vpn.example.com", local.ShareAddr)
  79. }
  80. }