client_disable_restart_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  8. )
  9. // The manual switch applies through the runtime, and the core's API removal
  10. // drops the credential only: whether the live session ends is what the setting
  11. // asks for, exactly as on the auto-disable path #6533 reports from.
  12. func TestManualClientDisableHonoursRestartSetting(t *testing.T) {
  13. const email = "[email protected]"
  14. for _, tc := range []struct {
  15. name string
  16. setting bool
  17. want bool
  18. }{
  19. {"setting on", true, true},
  20. {"setting off", false, false},
  21. } {
  22. t.Run(tc.name, func(t *testing.T) {
  23. setupConflictDB(t)
  24. setRestartOnClientDisable(t, tc.setting)
  25. mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }})
  26. mgr.SetLocalRuntimeOverride(&fakeNodeRuntime{})
  27. runtime.SetManager(mgr)
  28. t.Cleanup(func() { runtime.SetManager(nil) })
  29. seedInboundConflict(t, "manual-disable", "0.0.0.0", 50055, model.VLESS, `{"network":"tcp"}`,
  30. `{"clients":[{"email":"`+email+`","id":"5f2eb9d6-3a2f-4a55-9812-6ea1e2f7a333","enable":true}]}`)
  31. inbound := loadInboundByTag(t, "manual-disable")
  32. inboundSvc := InboundService{}
  33. clientSvc := ClientService{}
  34. clients, err := inboundSvc.GetClients(inbound)
  35. if err != nil {
  36. t.Fatalf("GetClients: %v", err)
  37. }
  38. if err := clientSvc.SyncInbound(nil, inbound.Id, clients); err != nil {
  39. t.Fatalf("SyncInbound: %v", err)
  40. }
  41. if err := database.GetDB().Create(&xray.ClientTraffic{InboundId: inbound.Id, Email: email, Enable: true}).Error; err != nil {
  42. t.Fatalf("seed traffic: %v", err)
  43. }
  44. changed, needRestart, err := clientSvc.SetClientEnableByEmail(&inboundSvc, email, false)
  45. if err != nil {
  46. t.Fatalf("SetClientEnableByEmail: %v", err)
  47. }
  48. if !changed {
  49. t.Fatal("the disable must be recorded")
  50. }
  51. if needRestart != tc.want {
  52. t.Fatalf("needRestart = %v, want %v", needRestart, tc.want)
  53. }
  54. })
  55. }
  56. }