1
0

inbound_mtproto_txfail_test.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package service
  2. import (
  3. "errors"
  4. "testing"
  5. "gorm.io/gorm"
  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. // A local MTProto inbound edit must not push to the managed sidecar from inside
  11. // the serialized write transaction: that blocks the single traffic-writer
  12. // goroutine on process/network I/O, and a later step failing the transaction
  13. // would leave the sidecar ahead of the rolled-back database. The push belongs in
  14. // the post-commit hook, exactly as the xray branch already does it.
  15. func TestUpdateInboundLocalMtprotoDefersPushUntilCommit(t *testing.T) {
  16. setupConflictDB(t)
  17. mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }})
  18. fake := &fakeNodeRuntime{}
  19. mgr.SetLocalRuntimeOverride(fake)
  20. runtime.SetManager(mgr)
  21. t.Cleanup(func() { runtime.SetManager(nil) })
  22. seedInboundConflict(t, "mt-txfail", "", 46150, model.MTProto, "",
  23. `{"clients":[{"email":"mtx","secret":"`+mtprotoTestSecretA+`","enable":true}]}`)
  24. seeded := loadInboundByTag(t, "mt-txfail")
  25. seedClientTraffic(t, seeded.Id, "mtx", true)
  26. db := database.GetDB()
  27. const cbName = "b1-05:fail-inbound-update"
  28. if err := db.Callback().Update().After("gorm:update").Register(cbName, func(tx *gorm.DB) {
  29. if tx.Statement != nil && tx.Statement.Table == "inbounds" {
  30. tx.AddError(errors.New("injected transaction failure"))
  31. }
  32. }); err != nil {
  33. t.Fatalf("register callback: %v", err)
  34. }
  35. t.Cleanup(func() { _ = db.Callback().Update().Remove(cbName) })
  36. update := *loadInboundByTag(t, "mt-txfail")
  37. update.Remark = "edited"
  38. if _, _, err := (&InboundService{}).UpdateInbound(&update); err == nil {
  39. t.Fatal("UpdateInbound: expected the injected transaction failure")
  40. }
  41. if n := fake.updateInbound.Load(); n != 0 {
  42. t.Fatalf("the MTProto sidecar push ran %d time(s) inside the failed transaction; it must be deferred until the commit succeeds", n)
  43. }
  44. }
  45. // Re-enabling a routed MTProto inbound must request an xray restart: the egress
  46. // SOCKS bridge is only injected for enabled inbounds, so the running config
  47. // needs regenerating or the sidecar dials a bridge that is not there.
  48. func TestSetInboundEnableRoutedMtprotoRequestsRestart(t *testing.T) {
  49. setupConflictDB(t)
  50. mgr := runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }})
  51. mgr.SetLocalRuntimeOverride(&fakeNodeRuntime{})
  52. runtime.SetManager(mgr)
  53. t.Cleanup(func() { runtime.SetManager(nil) })
  54. seedInboundConflict(t, "mt-route", "", 46160, model.MTProto, "",
  55. `{"clients":[{"email":"mtr","secret":"`+mtprotoTestSecretA+`","enable":true}],"routeThroughXray":true,"routeXrayPort":12345}`)
  56. seeded := loadInboundByTag(t, "mt-route")
  57. if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", seeded.Id).Update("enable", false).Error; err != nil {
  58. t.Fatalf("force disable: %v", err)
  59. }
  60. needRestart, err := (&InboundService{}).SetInboundEnable(seeded.Id, true)
  61. if err != nil {
  62. t.Fatalf("SetInboundEnable: %v", err)
  63. }
  64. if !needRestart {
  65. t.Fatal("re-enabling a routed MTProto inbound must request an xray restart to re-inject the egress bridge")
  66. }
  67. }