inbound_mtproto_client_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package service
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/mtproto"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  8. )
  9. func TestClientCrudMtprotoAppliesImmediately(t *testing.T) {
  10. setupConflictDB(t)
  11. pidFile := installFakeMtg(t)
  12. runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }}))
  13. t.Cleanup(func() { runtime.SetManager(nil) })
  14. inboundSvc := &InboundService{}
  15. clientSvc := &ClientService{}
  16. created, _, err := inboundSvc.AddInbound(&model.Inbound{
  17. Enable: true,
  18. Listen: "",
  19. Port: 46201,
  20. Protocol: model.MTProto,
  21. Settings: `{"clients":[{"email":"first","secret":"` + mtprotoTestSecretA + `","enable":true}]}`,
  22. })
  23. if err != nil {
  24. t.Fatalf("AddInbound: %v", err)
  25. }
  26. t.Cleanup(func() { mtproto.GetManager().Remove(created.Id) })
  27. waitForSpawns(t, pidFile, 1)
  28. t.Run("add client rewrites the served config", func(t *testing.T) {
  29. payload := &model.Inbound{
  30. Id: created.Id,
  31. Settings: `{"clients":[{"email":"second","secret":"` + mtprotoTestSecretB + `","enable":true}]}`,
  32. }
  33. needRestart, err := clientSvc.AddInboundClient(inboundSvc, payload)
  34. if err != nil {
  35. t.Fatalf("AddInboundClient: %v", err)
  36. }
  37. if needRestart {
  38. t.Fatal("adding an mtproto client must not request an xray restart")
  39. }
  40. cfg := readMtgConfig(t, created.Id)
  41. if !strings.Contains(cfg, `"second" = "`+mtprotoTestSecretB+`"`) {
  42. t.Fatalf("new client must be in the served config:\n%s", cfg)
  43. }
  44. if !strings.Contains(cfg, `"first" = "`+mtprotoTestSecretA+`"`) {
  45. t.Fatalf("existing client must remain served:\n%s", cfg)
  46. }
  47. })
  48. t.Run("delete client drops it from the served config", func(t *testing.T) {
  49. if _, err := clientSvc.DelInboundClientByEmail(inboundSvc, created.Id, "second", false, true); err != nil {
  50. t.Fatalf("DelInboundClientByEmail: %v", err)
  51. }
  52. cfg := readMtgConfig(t, created.Id)
  53. if strings.Contains(cfg, mtprotoTestSecretB) {
  54. t.Fatalf("deleted client must leave the served config:\n%s", cfg)
  55. }
  56. if !strings.Contains(cfg, mtprotoTestSecretA) {
  57. t.Fatalf("surviving client must stay served:\n%s", cfg)
  58. }
  59. })
  60. }