1
0

inbound_migration_test.go 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package service
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/database"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/xray"
  9. )
  10. // TestMigrationRequirements_BackfillsClientTrafficsWithMultiDomainInbound guards the
  11. // PostgreSQL fix where the externalProxy detection query (executed via .Scan) errored on
  12. // json_extract and rolled back the whole transaction — including the client_traffics
  13. // backfill at inbound.go:3093-3106, leaving clients with no traffic rows. A MultiDomain
  14. // inbound is present so that query returns rows and the function runs to completion; both
  15. // the backfill and the MultiDomain→ExternalProxy migration must then commit.
  16. func TestMigrationRequirements_BackfillsClientTrafficsWithMultiDomainInbound(t *testing.T) {
  17. dbDir := t.TempDir()
  18. t.Setenv("XUI_DB_FOLDER", dbDir)
  19. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  20. t.Fatalf("InitDB: %v", err)
  21. }
  22. t.Cleanup(func() { _ = database.CloseDB() })
  23. db := database.GetDB()
  24. const backfillEmail = "[email protected]"
  25. const uid = "ce8d33df-3a64-4f10-8f9b-91c3a8e0c010"
  26. // Inbound A: a client present only in settings.clients, with no client_traffics row.
  27. clientInbound := &model.Inbound{
  28. UserId: 1,
  29. Tag: "a-tag",
  30. Enable: true,
  31. Port: 30001,
  32. Protocol: model.VLESS,
  33. Settings: `{"clients":[{"email":"` + backfillEmail + `","id":"` + uid + `","enable":true}]}`,
  34. StreamSettings: `{"network":"tcp","security":"none"}`,
  35. }
  36. if err := db.Create(clientInbound).Error; err != nil {
  37. t.Fatalf("create client inbound: %v", err)
  38. }
  39. // Inbound B: a legacy MultiDomain inbound whose tag carries the 0.0.0.0: prefix.
  40. // Its presence makes the externalProxy query return rows, so the function does not
  41. // early-return and reaches the tag-cleanup statement.
  42. multiDomainInbound := &model.Inbound{
  43. UserId: 1,
  44. Tag: "inbound-0.0.0.0:30002",
  45. Enable: true,
  46. Port: 30002,
  47. Protocol: model.VLESS,
  48. Settings: `{"clients":[]}`,
  49. StreamSettings: `{"security":"tls","tlsSettings":{"settings":{"domains":[{"domain":"example.com"}]}}}`,
  50. }
  51. if err := db.Create(multiDomainInbound).Error; err != nil {
  52. t.Fatalf("create multidomain inbound: %v", err)
  53. }
  54. var before int64
  55. if err := db.Model(xray.ClientTraffic{}).Count(&before).Error; err != nil {
  56. t.Fatalf("count client_traffics before: %v", err)
  57. }
  58. if before != 0 {
  59. t.Fatalf("expected no client_traffics before migration, got %d", before)
  60. }
  61. svc := InboundService{}
  62. svc.MigrationRequirements()
  63. // The backfill must have committed: the settings-only client now owns a row.
  64. // Before the fix this was rolled back whenever the externalProxy detection query
  65. // errored (it does on Postgres via json_extract), so the MultiDomain inbound below
  66. // is deliberately present to make that query return rows and run to completion.
  67. var ct xray.ClientTraffic
  68. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", backfillEmail).First(&ct).Error; err != nil {
  69. t.Fatalf("client_traffics row not backfilled for %s: %v", backfillEmail, err)
  70. }
  71. // The MultiDomain→ExternalProxy migration must have committed too: the detection
  72. // query ran (.Scan executes it) and the loop rewrote the inbound's streamSettings.
  73. var refreshed model.Inbound
  74. if err := db.First(&refreshed, multiDomainInbound.Id).Error; err != nil {
  75. t.Fatalf("reload multidomain inbound: %v", err)
  76. }
  77. if !strings.Contains(refreshed.StreamSettings, "externalProxy") {
  78. t.Errorf("MultiDomain migration did not commit; streamSettings = %q", refreshed.StreamSettings)
  79. }
  80. }