node_origin_guid_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  7. )
  8. // #4983: a synced inbound's OriginNodeGuid must point at the panel that
  9. // physically hosts it. A node's own local inbound (empty origin in its
  10. // snapshot) is attributed to the node's own GUID; an inbound the node forwards
  11. // from its own sub-node (non-empty origin) keeps that deeper GUID across the
  12. // hop — so a chained Node1->Node2->Node3 attributes Node3's inbounds to Node3.
  13. func TestSetRemoteTraffic_AttributesOriginNodeGuid(t *testing.T) {
  14. setupConflictDB(t)
  15. db := database.GetDB()
  16. const nodeID = 1
  17. if err := db.Create(&model.Node{
  18. Id: nodeID,
  19. Name: "node2",
  20. Address: "10.0.0.2",
  21. Port: 2053,
  22. ApiToken: "t",
  23. Guid: "node2-guid",
  24. }).Error; err != nil {
  25. t.Fatalf("create node: %v", err)
  26. }
  27. snap := &runtime.TrafficSnapshot{
  28. Inbounds: []*model.Inbound{
  29. { // node2's own local inbound — reports no origin
  30. Tag: "in-443-tcp",
  31. Enable: true,
  32. Port: 443,
  33. Protocol: model.VLESS,
  34. Settings: `{"clients":[]}`,
  35. },
  36. { // forwarded from node2's sub-node (node3) — carries node3's guid
  37. Tag: "in-8443-tcp",
  38. Enable: true,
  39. Port: 8443,
  40. Protocol: model.VLESS,
  41. Settings: `{"clients":[]}`,
  42. OriginNodeGuid: "node3-guid",
  43. },
  44. },
  45. }
  46. svc := InboundService{}
  47. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  48. t.Fatalf("setRemoteTrafficLocked: %v", err)
  49. }
  50. origin := func(tag string) string {
  51. var ib model.Inbound
  52. if err := db.Where("tag = ?", tag).First(&ib).Error; err != nil {
  53. t.Fatalf("load inbound %q: %v", tag, err)
  54. }
  55. return ib.OriginNodeGuid
  56. }
  57. if og := origin("in-443-tcp"); og != "node2-guid" {
  58. t.Fatalf("local inbound origin = %q, want node2-guid (the node's own GUID)", og)
  59. }
  60. if og := origin("in-8443-tcp"); og != "node3-guid" {
  61. t.Fatalf("forwarded inbound origin = %q, want node3-guid (kept across the hop)", og)
  62. }
  63. }
  64. func TestSetRemoteTraffic_PreservesLocalShareAddressStrategy(t *testing.T) {
  65. setupConflictDB(t)
  66. db := database.GetDB()
  67. const nodeID = 1
  68. if err := db.Create(&model.Node{
  69. Id: nodeID,
  70. Name: "node2",
  71. Address: "10.0.0.2",
  72. Port: 2053,
  73. ApiToken: "t",
  74. Guid: "node2-guid",
  75. }).Error; err != nil {
  76. t.Fatalf("create node: %v", err)
  77. }
  78. nodeIDPtr := nodeID
  79. if err := db.Create(&model.Inbound{
  80. UserId: 1,
  81. NodeID: &nodeIDPtr,
  82. Tag: "remote-in",
  83. Enable: true,
  84. Port: 443,
  85. Protocol: model.VLESS,
  86. Settings: `{"clients":[]}`,
  87. ShareAddrStrategy: "custom",
  88. ShareAddr: "edge.example.com",
  89. }).Error; err != nil {
  90. t.Fatalf("create central inbound: %v", err)
  91. }
  92. snap := &runtime.TrafficSnapshot{
  93. Inbounds: []*model.Inbound{{
  94. Tag: "remote-in",
  95. Enable: true,
  96. Port: 8443,
  97. Protocol: model.VLESS,
  98. Settings: `{"clients":[]}`,
  99. }},
  100. }
  101. svc := InboundService{}
  102. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  103. t.Fatalf("setRemoteTrafficLocked: %v", err)
  104. }
  105. var ib model.Inbound
  106. if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
  107. t.Fatalf("load inbound: %v", err)
  108. }
  109. if ib.ShareAddrStrategy != "custom" || ib.ShareAddr != "edge.example.com" {
  110. t.Fatalf("share address fields were overwritten: strategy=%q addr=%q", ib.ShareAddrStrategy, ib.ShareAddr)
  111. }
  112. if ib.Port != 8443 {
  113. t.Fatalf("sync should still update regular remote fields; port = %d, want 8443", ib.Port)
  114. }
  115. }
  116. func TestSetRemoteTraffic_DefaultsShareAddressFieldsForNewCentralInbound(t *testing.T) {
  117. setupConflictDB(t)
  118. db := database.GetDB()
  119. const nodeID = 1
  120. if err := db.Create(&model.Node{
  121. Id: nodeID,
  122. Name: "node2",
  123. Address: "10.0.0.2",
  124. Port: 2053,
  125. ApiToken: "t",
  126. Guid: "node2-guid",
  127. }).Error; err != nil {
  128. t.Fatalf("create node: %v", err)
  129. }
  130. snap := &runtime.TrafficSnapshot{
  131. Inbounds: []*model.Inbound{{
  132. Tag: "remote-in",
  133. Enable: true,
  134. Port: 8443,
  135. Protocol: model.VLESS,
  136. Settings: `{"clients":[]}`,
  137. ShareAddrStrategy: "custom",
  138. ShareAddr: "remote.example.com",
  139. }},
  140. }
  141. svc := InboundService{}
  142. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  143. t.Fatalf("setRemoteTrafficLocked: %v", err)
  144. }
  145. var ib model.Inbound
  146. if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
  147. t.Fatalf("load inbound: %v", err)
  148. }
  149. if ib.ShareAddrStrategy != "node" || ib.ShareAddr != "" {
  150. t.Fatalf("new central inbound share fields = (%q, %q), want (node, empty)", ib.ShareAddrStrategy, ib.ShareAddr)
  151. }
  152. }