client_group_node_sync_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package service
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  9. )
  10. func TestSetRemoteTraffic_PreservesPanelLocalGroupAndComment(t *testing.T) {
  11. dbDir := t.TempDir()
  12. t.Setenv("XUI_DB_FOLDER", dbDir)
  13. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  14. t.Fatalf("InitDB: %v", err)
  15. }
  16. t.Cleanup(func() { _ = database.CloseDB() })
  17. db := database.GetDB()
  18. const nodeID = 1
  19. const email = "[email protected]"
  20. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c003"
  21. const wantGroup = "vip"
  22. const wantComment = "renewed manually"
  23. id := nodeID
  24. central := &model.Inbound{
  25. UserId: 1,
  26. NodeID: &id,
  27. Tag: "n1-vless",
  28. Enable: true,
  29. Port: 20001,
  30. Protocol: model.VLESS,
  31. Settings: `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true,"group":"` + wantGroup + `","comment":"` + wantComment + `"}]}`,
  32. }
  33. if err := db.Create(central).Error; err != nil {
  34. t.Fatalf("create node inbound: %v", err)
  35. }
  36. if err := db.Create(&model.ClientRecord{
  37. Email: email,
  38. UUID: uid,
  39. Enable: true,
  40. Group: wantGroup,
  41. Comment: wantComment,
  42. }).Error; err != nil {
  43. t.Fatalf("create client record: %v", err)
  44. }
  45. snap := &runtime.TrafficSnapshot{
  46. Inbounds: []*model.Inbound{
  47. {
  48. Tag: "n1-vless",
  49. Enable: true,
  50. Port: 20001,
  51. Protocol: model.VLESS,
  52. Settings: `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true}]}`,
  53. },
  54. },
  55. }
  56. svc := InboundService{}
  57. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  58. t.Fatalf("setRemoteTrafficLocked: %v", err)
  59. }
  60. var row model.ClientRecord
  61. if err := db.Where("email = ?", email).First(&row).Error; err != nil {
  62. t.Fatalf("lookup client row after sync: %v", err)
  63. }
  64. if row.Group != wantGroup {
  65. t.Errorf("group was wiped by node snapshot sync: got %q, want %q", row.Group, wantGroup)
  66. }
  67. if row.Comment != wantComment {
  68. t.Errorf("comment was wiped by node snapshot sync: got %q, want %q", row.Comment, wantComment)
  69. }
  70. }
  71. func TestSyncInbound_KeepsGroupWhenIncomingEmpty(t *testing.T) {
  72. dbDir := t.TempDir()
  73. t.Setenv("XUI_DB_FOLDER", dbDir)
  74. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  75. t.Fatalf("InitDB: %v", err)
  76. }
  77. t.Cleanup(func() { _ = database.CloseDB() })
  78. db := database.GetDB()
  79. ib := &model.Inbound{Tag: "vless-grp", Enable: true, Port: 20002, Protocol: model.VLESS}
  80. if err := db.Create(ib).Error; err != nil {
  81. t.Fatalf("create inbound: %v", err)
  82. }
  83. svc := ClientService{}
  84. const email = "[email protected]"
  85. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c004"
  86. const wantGroup = "vip"
  87. withGroup := model.Client{Email: email, ID: uid, Enable: true, Group: wantGroup}
  88. if err := svc.SyncInbound(nil, ib.Id, []model.Client{withGroup}); err != nil {
  89. t.Fatalf("SyncInbound (set group): %v", err)
  90. }
  91. noGroup := model.Client{Email: email, ID: uid, Enable: true, Group: ""}
  92. if err := svc.SyncInbound(nil, ib.Id, []model.Client{noGroup}); err != nil {
  93. t.Fatalf("SyncInbound (group-less rebuild): %v", err)
  94. }
  95. var row model.ClientRecord
  96. if err := db.Where("email = ?", email).First(&row).Error; err != nil {
  97. t.Fatalf("lookup client row: %v", err)
  98. }
  99. if row.Group != wantGroup {
  100. t.Errorf("group must survive a group-less settings rebuild (it is managed via the Groups page, not Xray settings): got %q, want %q", row.Group, wantGroup)
  101. }
  102. }
  103. // Removing the group in the client editor and saving must clear group_name and
  104. // drop the settings "group" key, even though SyncInbound preserves a group on a
  105. // group-less rebuild. The editor round-trips the field, so ClientService.Update
  106. // applies it explicitly.
  107. func TestClientUpdate_ClearsGroup(t *testing.T) {
  108. dbDir := t.TempDir()
  109. t.Setenv("XUI_DB_FOLDER", dbDir)
  110. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  111. t.Fatalf("InitDB: %v", err)
  112. }
  113. t.Cleanup(func() { _ = database.CloseDB() })
  114. db := database.GetDB()
  115. const email = "[email protected]"
  116. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c005"
  117. const wantGroup = "vip"
  118. ib := &model.Inbound{
  119. UserId: 1,
  120. Tag: "vless-clear",
  121. Enable: true,
  122. Port: 20003,
  123. Protocol: model.VLESS,
  124. Settings: `{"clients":[{"email":"` + email + `","id":"` + uid + `","enable":true,"group":"` + wantGroup + `"}]}`,
  125. }
  126. if err := db.Create(ib).Error; err != nil {
  127. t.Fatalf("create inbound: %v", err)
  128. }
  129. svc := ClientService{}
  130. inboundSvc := &InboundService{}
  131. // Seed the client record + inbound link from the settings.
  132. seedClients, err := inboundSvc.GetClients(ib)
  133. if err != nil {
  134. t.Fatalf("GetClients: %v", err)
  135. }
  136. if err := svc.SyncInbound(nil, ib.Id, seedClients); err != nil {
  137. t.Fatalf("seed SyncInbound: %v", err)
  138. }
  139. var rec model.ClientRecord
  140. if err := db.Where("email = ?", email).First(&rec).Error; err != nil {
  141. t.Fatalf("lookup seeded record: %v", err)
  142. }
  143. if rec.Group != wantGroup {
  144. t.Fatalf("setup: group not seeded, got %q", rec.Group)
  145. }
  146. // Edit the client and remove the group.
  147. updated := *rec.ToClient()
  148. updated.Group = ""
  149. if _, err := svc.Update(inboundSvc, rec.Id, updated); err != nil {
  150. t.Fatalf("Update (clear group): %v", err)
  151. }
  152. var after model.ClientRecord
  153. if err := db.Where("email = ?", email).First(&after).Error; err != nil {
  154. t.Fatalf("lookup record after update: %v", err)
  155. }
  156. if after.Group != "" {
  157. t.Errorf("group not cleared after editor removed it: got %q, want empty", after.Group)
  158. }
  159. var ibAfter model.Inbound
  160. if err := db.First(&ibAfter, ib.Id).Error; err != nil {
  161. t.Fatalf("lookup inbound after update: %v", err)
  162. }
  163. if strings.Contains(ibAfter.Settings, `"group"`) {
  164. t.Errorf("inbound settings still carry a group key after removal: %s", ibAfter.Settings)
  165. }
  166. }