node_origin_guid_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/database"
  5. "github.com/mhsanaei/3x-ui/v3/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/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. }