client_sync_multiprotocol_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/dbtest"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. func TestSyncInbound_PreservesCredentialsAcrossProtocols(t *testing.T) {
  10. dbDir := t.TempDir()
  11. t.Setenv("XUI_DB_FOLDER", dbDir)
  12. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  13. db := database.GetDB()
  14. vlessInbound := &model.Inbound{Tag: "vless-in", Enable: true, Port: 10001, Protocol: model.VLESS}
  15. if err := db.Create(vlessInbound).Error; err != nil {
  16. t.Fatalf("create vless inbound: %v", err)
  17. }
  18. hysteriaInbound := &model.Inbound{Tag: "hy-in", Enable: true, Port: 10002, Protocol: model.Hysteria}
  19. if err := db.Create(hysteriaInbound).Error; err != nil {
  20. t.Fatalf("create hysteria inbound: %v", err)
  21. }
  22. svc := ClientService{}
  23. const sharedEmail = "[email protected]"
  24. const wantUUID = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c001"
  25. const wantAuth = "h2-auth-token"
  26. const wantFlow = "xtls-rprx-vision"
  27. vlessClient := model.Client{Email: sharedEmail, ID: wantUUID, Enable: true, Flow: wantFlow}
  28. if err := svc.SyncInbound(nil, vlessInbound.Id, []model.Client{vlessClient}); err != nil {
  29. t.Fatalf("vless SyncInbound: %v", err)
  30. }
  31. hysteriaClient := model.Client{Email: sharedEmail, Auth: wantAuth, Enable: true}
  32. if err := svc.SyncInbound(nil, hysteriaInbound.Id, []model.Client{hysteriaClient}); err != nil {
  33. t.Fatalf("hysteria SyncInbound: %v", err)
  34. }
  35. var row model.ClientRecord
  36. if err := db.Where("email = ?", sharedEmail).First(&row).Error; err != nil {
  37. t.Fatalf("lookup client row: %v", err)
  38. }
  39. if row.UUID != wantUUID {
  40. t.Errorf("UUID was clobbered by Hysteria sync: got %q, want %q", row.UUID, wantUUID)
  41. }
  42. if row.Auth != wantAuth {
  43. t.Errorf("Auth not persisted: got %q, want %q", row.Auth, wantAuth)
  44. }
  45. vlessList, err := svc.ListForInbound(nil, vlessInbound.Id)
  46. if err != nil {
  47. t.Fatalf("ListForInbound(vless): %v", err)
  48. }
  49. if len(vlessList) != 1 || vlessList[0].Flow != wantFlow {
  50. t.Errorf("VLESS inbound should still report flow=%q via FlowOverride, got %#v", wantFlow, vlessList)
  51. }
  52. hysteriaList, err := svc.ListForInbound(nil, hysteriaInbound.Id)
  53. if err != nil {
  54. t.Fatalf("ListForInbound(hysteria): %v", err)
  55. }
  56. if len(hysteriaList) != 1 || hysteriaList[0].Flow != "" {
  57. t.Errorf("Hysteria inbound should report empty flow, got %#v", hysteriaList)
  58. }
  59. }
  60. func TestSyncInbound_AllowsClearingFlow(t *testing.T) {
  61. dbDir := t.TempDir()
  62. t.Setenv("XUI_DB_FOLDER", dbDir)
  63. dbtest.InitDB(t, filepath.Join(dbDir, "x-ui.db"))
  64. db := database.GetDB()
  65. vless := &model.Inbound{Tag: "vless-in", Enable: true, Port: 10003, Protocol: model.VLESS}
  66. if err := db.Create(vless).Error; err != nil {
  67. t.Fatalf("create vless inbound: %v", err)
  68. }
  69. svc := ClientService{}
  70. const email = "[email protected]"
  71. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c002"
  72. withFlow := model.Client{Email: email, ID: uid, Enable: true, Flow: "xtls-rprx-vision"}
  73. if err := svc.SyncInbound(nil, vless.Id, []model.Client{withFlow}); err != nil {
  74. t.Fatalf("vless SyncInbound (set flow): %v", err)
  75. }
  76. cleared := model.Client{Email: email, ID: uid, Enable: true, Flow: ""}
  77. if err := svc.SyncInbound(nil, vless.Id, []model.Client{cleared}); err != nil {
  78. t.Fatalf("vless SyncInbound (clear flow): %v", err)
  79. }
  80. list, err := svc.ListForInbound(nil, vless.Id)
  81. if err != nil {
  82. t.Fatalf("ListForInbound: %v", err)
  83. }
  84. if len(list) != 1 {
  85. t.Fatalf("expected 1 client, got %d", len(list))
  86. }
  87. if list[0].Flow != "" {
  88. t.Errorf("flow should be clearable on the owning inbound, got %q (Copilot review on #4545)", list[0].Flow)
  89. }
  90. }