inbound_mtproto_apply_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package service
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/internal/mtproto"
  10. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  11. )
  12. func mtgConfigPath(t *testing.T, inboundId int) string {
  13. t.Helper()
  14. return filepath.Join(os.Getenv("XUI_BIN_FOLDER"), "mtproto", fmt.Sprintf("mtg-%d.toml", inboundId))
  15. }
  16. func readMtgConfig(t *testing.T, inboundId int) string {
  17. t.Helper()
  18. data, err := os.ReadFile(mtgConfigPath(t, inboundId))
  19. if err != nil {
  20. t.Fatalf("read mtg config: %v", err)
  21. }
  22. return string(data)
  23. }
  24. func TestUpdateInboundMtprotoUnchangedDoesNotRestart(t *testing.T) {
  25. setupConflictDB(t)
  26. pidFile := installFakeMtg(t)
  27. runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }}))
  28. t.Cleanup(func() { runtime.SetManager(nil) })
  29. seedInboundConflict(t, "mt-apply", "", 46101, model.MTProto,
  30. "",
  31. `{"clients":[`+
  32. `{"email":"mtga","secret":"`+mtprotoTestSecretA+`","enable":true},`+
  33. `{"email":"mtgb","secret":"`+mtprotoTestSecretB+`","enable":true}]}`)
  34. seeded := loadInboundByTag(t, "mt-apply")
  35. seedClientTraffic(t, seeded.Id, "mtga", true)
  36. seedClientTraffic(t, seeded.Id, "mtgb", true)
  37. svc := &InboundService{}
  38. primed, ok := mtproto.InstanceFromInbound(seeded)
  39. if !ok {
  40. t.Fatal("seed inbound must produce an mtg instance")
  41. }
  42. if err := mtproto.GetManager().Ensure(primed); err != nil {
  43. t.Fatalf("prime mtg: %v", err)
  44. }
  45. t.Cleanup(func() { mtproto.GetManager().Remove(seeded.Id) })
  46. waitForSpawns(t, pidFile, 1)
  47. primedConfig := readMtgConfig(t, seeded.Id)
  48. saveAndAssertKept := func(t *testing.T, mutate func(*model.Inbound)) {
  49. t.Helper()
  50. update := *loadInboundByTag(t, "mt-apply")
  51. mutate(&update)
  52. _, needRestart, err := svc.UpdateInbound(&update)
  53. if err != nil {
  54. t.Fatalf("UpdateInbound: %v", err)
  55. }
  56. if needRestart {
  57. t.Fatal("an mtproto-only edit must not request an xray restart")
  58. }
  59. assertNoNewSpawns(t, pidFile, 1)
  60. if got := readMtgConfig(t, seeded.Id); got != primedConfig {
  61. t.Fatalf("config rewritten on a no-op edit:\nbefore:\n%s\nafter:\n%s", primedConfig, got)
  62. }
  63. }
  64. t.Run("unchangedSaveKeepsProcess", func(t *testing.T) {
  65. saveAndAssertKept(t, func(*model.Inbound) {})
  66. })
  67. t.Run("remarkOnlyEditKeepsProcess", func(t *testing.T) {
  68. saveAndAssertKept(t, func(ib *model.Inbound) { ib.Remark = "renamed while users stay connected" })
  69. })
  70. t.Run("rekeyedSecretRestartsProcess", func(t *testing.T) {
  71. update := *loadInboundByTag(t, "mt-apply")
  72. update.Settings = strings.Replace(update.Settings, mtprotoTestSecretA, mtprotoTestSecretD, 1)
  73. if !strings.Contains(update.Settings, mtprotoTestSecretD) {
  74. t.Fatal("fixture must contain the re-keyed secret")
  75. }
  76. _, needRestart, err := svc.UpdateInbound(&update)
  77. if err != nil {
  78. t.Fatalf("UpdateInbound: %v", err)
  79. }
  80. if needRestart {
  81. t.Fatal("an mtproto secret change must not request an xray restart")
  82. }
  83. waitForSpawns(t, pidFile, 2)
  84. if got := readMtgConfig(t, seeded.Id); !strings.Contains(got, mtprotoTestSecretD) {
  85. t.Fatalf("restarted config must carry the new secret:\n%s", got)
  86. }
  87. })
  88. }