node_origin_guid_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package service
  2. import (
  3. "slices"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  8. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. func assertStringSet(t *testing.T, label string, got, want []string) {
  11. t.Helper()
  12. g := append([]string(nil), got...)
  13. w := append([]string(nil), want...)
  14. slices.Sort(g)
  15. slices.Sort(w)
  16. if !slices.Equal(g, w) {
  17. t.Fatalf("%s = %v, want %v", label, got, want)
  18. }
  19. }
  20. // #4983: a synced inbound's OriginNodeGuid must point at the panel that
  21. // physically hosts it. A node's own local inbound (empty origin in its
  22. // snapshot) is attributed to the node's own GUID; an inbound the node forwards
  23. // from its own sub-node (non-empty origin) keeps that deeper GUID across the
  24. // hop — so a chained Node1->Node2->Node3 attributes Node3's inbounds to Node3.
  25. func TestSetRemoteTraffic_AttributesOriginNodeGuid(t *testing.T) {
  26. setupConflictDB(t)
  27. db := database.GetDB()
  28. const nodeID = 1
  29. if err := db.Create(&model.Node{
  30. Id: nodeID,
  31. Name: "node2",
  32. Address: "10.0.0.2",
  33. Port: 2053,
  34. ApiToken: "t",
  35. Guid: "node2-guid",
  36. }).Error; err != nil {
  37. t.Fatalf("create node: %v", err)
  38. }
  39. snap := &runtime.TrafficSnapshot{
  40. Inbounds: []*model.Inbound{
  41. { // node2's own local inbound — reports no origin
  42. Tag: "in-443-tcp",
  43. Enable: true,
  44. Port: 443,
  45. Protocol: model.VLESS,
  46. Settings: `{"clients":[]}`,
  47. },
  48. { // forwarded from node2's sub-node (node3) — carries node3's guid
  49. Tag: "in-8443-tcp",
  50. Enable: true,
  51. Port: 8443,
  52. Protocol: model.VLESS,
  53. Settings: `{"clients":[]}`,
  54. OriginNodeGuid: "node3-guid",
  55. },
  56. },
  57. }
  58. svc := InboundService{}
  59. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  60. t.Fatalf("setRemoteTrafficLocked: %v", err)
  61. }
  62. origin := func(tag string) string {
  63. var ib model.Inbound
  64. if err := db.Where("tag = ?", tag).First(&ib).Error; err != nil {
  65. t.Fatalf("load inbound %q: %v", tag, err)
  66. }
  67. return ib.OriginNodeGuid
  68. }
  69. if og := origin("in-443-tcp"); og != "node2-guid" {
  70. t.Fatalf("local inbound origin = %q, want node2-guid (the node's own GUID)", og)
  71. }
  72. if og := origin("in-8443-tcp"); og != "node3-guid" {
  73. t.Fatalf("forwarded inbound origin = %q, want node3-guid (kept across the hop)", og)
  74. }
  75. }
  76. // A cloned node reports its OWN inbound with its own (duplicated) panelGuid as
  77. // the origin. That must be remapped to the node-unique key, not stored verbatim
  78. // — otherwise origin_node_guid keeps the shared GUID while online is keyed by
  79. // the node-unique key, and the inbound page reads an empty bucket (shows
  80. // offline). A genuinely forwarded sub-node GUID is still kept across the hop.
  81. func TestSetRemoteTraffic_RemapsClonedNodeOwnGuidOrigin(t *testing.T) {
  82. setupConflictDB(t)
  83. db := database.GetDB()
  84. // Two nodes share one panelGuid (cloned servers).
  85. for _, n := range []*model.Node{
  86. {Id: 1, Name: "a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup"},
  87. {Id: 2, Name: "b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup"},
  88. } {
  89. if err := db.Create(n).Error; err != nil {
  90. t.Fatalf("create node %s: %v", n.Name, err)
  91. }
  92. }
  93. snap := &runtime.TrafficSnapshot{
  94. Inbounds: []*model.Inbound{
  95. { // node 1's OWN inbound, reporting its own (shared) panelGuid as origin
  96. Tag: "own-443-tcp",
  97. Enable: true,
  98. Port: 443,
  99. Protocol: model.VLESS,
  100. Settings: `{"clients":[]}`,
  101. OriginNodeGuid: "dup",
  102. },
  103. { // forwarded from a sub-node with a distinct guid — kept across the hop
  104. Tag: "fwd-8443-tcp",
  105. Enable: true,
  106. Port: 8443,
  107. Protocol: model.VLESS,
  108. Settings: `{"clients":[]}`,
  109. OriginNodeGuid: "child-guid",
  110. },
  111. },
  112. }
  113. svc := InboundService{}
  114. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  115. t.Fatalf("setRemoteTrafficLocked: %v", err)
  116. }
  117. origin := func(tag string) string {
  118. var ib model.Inbound
  119. if err := db.Where("tag = ?", tag).First(&ib).Error; err != nil {
  120. t.Fatalf("load inbound %q: %v", tag, err)
  121. }
  122. return ib.OriginNodeGuid
  123. }
  124. if og := origin("own-443-tcp"); og != "node:1" {
  125. t.Fatalf("cloned node's own inbound origin = %q, want node:1 (remapped from shared GUID)", og)
  126. }
  127. if og := origin("fwd-8443-tcp"); og != "child-guid" {
  128. t.Fatalf("forwarded inbound origin = %q, want child-guid (kept across the hop)", og)
  129. }
  130. }
  131. func TestSetRemoteTraffic_RemapsActiveInboundTreeAndCentralTags(t *testing.T) {
  132. setupConflictDB(t)
  133. db := database.GetDB()
  134. previousProcess, previousResult := xrayState.snapshot()
  135. process := xray.NewTestProcess(nil, "")
  136. xrayState.replace(process)
  137. t.Cleanup(func() {
  138. xrayState.mu.Lock()
  139. xrayState.process = previousProcess
  140. xrayState.result = previousResult
  141. xrayState.mu.Unlock()
  142. })
  143. // Force the remote inbound to be adopted with an n1- prefix on the master:
  144. // the active-inbound tree still arrives with the node-local tag.
  145. if err := db.Create(&model.Inbound{
  146. Tag: "shared-tag", Enable: true, Port: 1000, Protocol: model.VLESS, Settings: `{"clients":[]}`,
  147. }).Error; err != nil {
  148. t.Fatalf("create local conflicting inbound: %v", err)
  149. }
  150. // Two cloned nodes share the same panelGuid, so the node's own active tags
  151. // must be keyed by node:1 instead of the duplicated GUID.
  152. for _, n := range []*model.Node{
  153. {Id: 1, Name: "a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "dup"},
  154. {Id: 2, Name: "b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "dup"},
  155. } {
  156. if err := db.Create(n).Error; err != nil {
  157. t.Fatalf("create node %s: %v", n.Name, err)
  158. }
  159. }
  160. snap := &runtime.TrafficSnapshot{
  161. Inbounds: []*model.Inbound{{
  162. Tag: "shared-tag",
  163. Enable: true,
  164. Port: 8443,
  165. Protocol: model.VLESS,
  166. Settings: `{"clients":[]}`,
  167. }},
  168. ActiveInboundTree: map[string][]string{
  169. "dup": {"shared-tag"},
  170. },
  171. }
  172. svc := InboundService{}
  173. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  174. t.Fatalf("setRemoteTrafficLocked: %v", err)
  175. }
  176. merged := process.GetMergedActiveInboundTrees()
  177. assertStringSet(t, "active node:1", merged["node:1"], []string{"n1-shared-tag"})
  178. if _, ok := merged["dup"]; ok {
  179. t.Fatalf("cloned active-inbound subtree must not stay under shared GUID: %v", merged)
  180. }
  181. }
  182. func TestSetRemoteTraffic_NormalizesForwardedActiveInboundSubtreeTags(t *testing.T) {
  183. setupConflictDB(t)
  184. db := database.GetDB()
  185. previousProcess, previousResult := xrayState.snapshot()
  186. process := xray.NewTestProcess(nil, "")
  187. xrayState.replace(process)
  188. t.Cleanup(func() {
  189. xrayState.mu.Lock()
  190. xrayState.process = previousProcess
  191. xrayState.result = previousResult
  192. xrayState.mu.Unlock()
  193. })
  194. for _, tag := range []string{"own-tag", "child-tag"} {
  195. if err := db.Create(&model.Inbound{
  196. Tag: tag, Enable: true, Port: 1000, Protocol: model.VLESS, Settings: `{"clients":[]}`,
  197. }).Error; err != nil {
  198. t.Fatalf("create local conflicting inbound %q: %v", tag, err)
  199. }
  200. }
  201. if err := db.Create(&model.Node{
  202. Id: 1, Name: "node2", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "node2-guid",
  203. }).Error; err != nil {
  204. t.Fatalf("create node: %v", err)
  205. }
  206. snap := &runtime.TrafficSnapshot{
  207. Inbounds: []*model.Inbound{
  208. {
  209. Tag: "own-tag",
  210. Enable: true,
  211. Port: 8443,
  212. Protocol: model.VLESS,
  213. Settings: `{"clients":[]}`,
  214. },
  215. {
  216. Tag: "child-tag",
  217. Enable: true,
  218. Port: 9443,
  219. Protocol: model.VLESS,
  220. Settings: `{"clients":[]}`,
  221. OriginNodeGuid: "child-guid",
  222. },
  223. },
  224. ActiveInboundTree: map[string][]string{
  225. "node2-guid": {"own-tag"},
  226. "child-guid": {"child-tag"},
  227. },
  228. }
  229. svc := InboundService{}
  230. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  231. t.Fatalf("setRemoteTrafficLocked: %v", err)
  232. }
  233. merged := process.GetMergedActiveInboundTrees()
  234. assertStringSet(t, "direct node active tags", merged["node2-guid"], []string{"n1-own-tag"})
  235. assertStringSet(t, "forwarded child active tags", merged["child-guid"], []string{"n1-child-tag"})
  236. }
  237. func TestSetRemoteTraffic_DropsForeignActiveInboundGuid(t *testing.T) {
  238. setupConflictDB(t)
  239. db := database.GetDB()
  240. previousProcess, previousResult := xrayState.snapshot()
  241. process := xray.NewTestProcess(nil, "")
  242. xrayState.replace(process)
  243. t.Cleanup(func() {
  244. xrayState.mu.Lock()
  245. xrayState.process = previousProcess
  246. xrayState.result = previousResult
  247. xrayState.mu.Unlock()
  248. })
  249. for _, n := range []*model.Node{
  250. {Id: 1, Name: "node-a", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "node-a-guid"},
  251. {Id: 2, Name: "node-b", Address: "10.0.0.2", Port: 2053, ApiToken: "t", Guid: "node-b-guid"},
  252. } {
  253. if err := db.Create(n).Error; err != nil {
  254. t.Fatalf("create node %s: %v", n.Name, err)
  255. }
  256. }
  257. snap := &runtime.TrafficSnapshot{
  258. Inbounds: []*model.Inbound{{
  259. Tag: "own-tag",
  260. Enable: true,
  261. Port: 8443,
  262. Protocol: model.VLESS,
  263. Settings: `{"clients":[]}`,
  264. }},
  265. ActiveInboundTree: map[string][]string{
  266. "node-a-guid": {"own-tag"},
  267. "node-b-guid": {"foreign-tag"},
  268. },
  269. }
  270. svc := InboundService{}
  271. if _, err := svc.setRemoteTrafficLocked(1, snap, false); err != nil {
  272. t.Fatalf("setRemoteTrafficLocked: %v", err)
  273. }
  274. merged := process.GetMergedActiveInboundTrees()
  275. assertStringSet(t, "own active tags", merged["node-a-guid"], []string{"own-tag"})
  276. if _, ok := merged["node-b-guid"]; ok {
  277. t.Fatalf("foreign active-inbound subtree should be ignored: %v", merged)
  278. }
  279. }
  280. // A node mid-restart can return an empty inbound list with success=true. The
  281. // sync must NOT treat that as "delete all my inbounds" — otherwise a blip wipes
  282. // the node's central inbounds and every client on them (what happened to the
  283. // Germany node: 0 clients but still online).
  284. func TestSetRemoteTraffic_EmptySnapshotKeepsCentralInbounds(t *testing.T) {
  285. setupConflictDB(t)
  286. db := database.GetDB()
  287. const nodeID = 1
  288. if err := db.Create(&model.Node{
  289. Id: nodeID, Name: "n", Address: "10.0.0.1", Port: 2053, ApiToken: "t", Guid: "g",
  290. }).Error; err != nil {
  291. t.Fatalf("create node: %v", err)
  292. }
  293. nidPtr := nodeID
  294. if err := db.Create(&model.Inbound{
  295. UserId: 1, NodeID: &nidPtr, Tag: "remote-in", Enable: true,
  296. Port: 443, Protocol: model.VLESS, Settings: `{"clients":[]}`,
  297. }).Error; err != nil {
  298. t.Fatalf("create central inbound: %v", err)
  299. }
  300. // Empty snapshot — the node reported no inbounds this cycle.
  301. svc := InboundService{}
  302. if _, err := svc.setRemoteTrafficLocked(nodeID, &runtime.TrafficSnapshot{}, false); err != nil {
  303. t.Fatalf("setRemoteTrafficLocked: %v", err)
  304. }
  305. var count int64
  306. if err := db.Model(&model.Inbound{}).Where("tag = ?", "remote-in").Count(&count).Error; err != nil {
  307. t.Fatalf("count inbounds: %v", err)
  308. }
  309. if count != 1 {
  310. t.Fatalf("empty snapshot must not delete the central inbound; got count = %d", count)
  311. }
  312. }
  313. func TestSetRemoteTraffic_PreservesLocalShareAddressStrategy(t *testing.T) {
  314. setupConflictDB(t)
  315. db := database.GetDB()
  316. const nodeID = 1
  317. if err := db.Create(&model.Node{
  318. Id: nodeID,
  319. Name: "node2",
  320. Address: "10.0.0.2",
  321. Port: 2053,
  322. ApiToken: "t",
  323. Guid: "node2-guid",
  324. }).Error; err != nil {
  325. t.Fatalf("create node: %v", err)
  326. }
  327. nodeIDPtr := nodeID
  328. if err := db.Create(&model.Inbound{
  329. UserId: 1,
  330. NodeID: &nodeIDPtr,
  331. Tag: "remote-in",
  332. Enable: true,
  333. Port: 443,
  334. Protocol: model.VLESS,
  335. Settings: `{"clients":[]}`,
  336. ShareAddrStrategy: "custom",
  337. ShareAddr: "edge.example.com",
  338. }).Error; err != nil {
  339. t.Fatalf("create central inbound: %v", err)
  340. }
  341. snap := &runtime.TrafficSnapshot{
  342. Inbounds: []*model.Inbound{{
  343. Tag: "remote-in",
  344. Enable: true,
  345. Port: 8443,
  346. Protocol: model.VLESS,
  347. Settings: `{"clients":[]}`,
  348. }},
  349. }
  350. svc := InboundService{}
  351. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  352. t.Fatalf("setRemoteTrafficLocked: %v", err)
  353. }
  354. var ib model.Inbound
  355. if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
  356. t.Fatalf("load inbound: %v", err)
  357. }
  358. if ib.ShareAddrStrategy != "custom" || ib.ShareAddr != "edge.example.com" {
  359. t.Fatalf("share address fields were overwritten: strategy=%q addr=%q", ib.ShareAddrStrategy, ib.ShareAddr)
  360. }
  361. if ib.Port != 8443 {
  362. t.Fatalf("sync should still update regular remote fields; port = %d, want 8443", ib.Port)
  363. }
  364. }
  365. func TestSetRemoteTraffic_DefaultsShareAddressFieldsForNewCentralInbound(t *testing.T) {
  366. setupConflictDB(t)
  367. db := database.GetDB()
  368. const nodeID = 1
  369. if err := db.Create(&model.Node{
  370. Id: nodeID,
  371. Name: "node2",
  372. Address: "10.0.0.2",
  373. Port: 2053,
  374. ApiToken: "t",
  375. Guid: "node2-guid",
  376. }).Error; err != nil {
  377. t.Fatalf("create node: %v", err)
  378. }
  379. snap := &runtime.TrafficSnapshot{
  380. Inbounds: []*model.Inbound{{
  381. Tag: "remote-in",
  382. Enable: true,
  383. Port: 8443,
  384. Protocol: model.VLESS,
  385. Settings: `{"clients":[]}`,
  386. ShareAddrStrategy: "custom",
  387. ShareAddr: "remote.example.com",
  388. }},
  389. }
  390. svc := InboundService{}
  391. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  392. t.Fatalf("setRemoteTrafficLocked: %v", err)
  393. }
  394. var ib model.Inbound
  395. if err := db.Where("tag = ?", "remote-in").First(&ib).Error; err != nil {
  396. t.Fatalf("load inbound: %v", err)
  397. }
  398. if ib.ShareAddrStrategy != "node" || ib.ShareAddr != "" {
  399. t.Fatalf("new central inbound share fields = (%q, %q), want (node, empty)", ib.ShareAddrStrategy, ib.ShareAddr)
  400. }
  401. }