inbound_migration_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package service
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "testing"
  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/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. }
  81. // TestMigrationRequirements_CleansLegacyZeroAddrTag guards the legacy tag cleanup that
  82. // strips the auto-generated "0.0.0.0:" prefix. The inbound is MultiDomain TLS so the
  83. // externalProxy detection query returns rows and the cleanup is reached (it early-returns
  84. // at len(externalProxy)==0 otherwise). The cleanup must use tx.Exec, not tx.Raw, which
  85. // only builds a non-SELECT statement without running it.
  86. func TestMigrationRequirements_CleansLegacyZeroAddrTag(t *testing.T) {
  87. dbDir := t.TempDir()
  88. t.Setenv("XUI_DB_FOLDER", dbDir)
  89. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  90. t.Fatalf("InitDB: %v", err)
  91. }
  92. t.Cleanup(func() { _ = database.CloseDB() })
  93. db := database.GetDB()
  94. legacy := &model.Inbound{
  95. UserId: 1,
  96. Tag: "inbound-0.0.0.0:30002",
  97. Enable: true,
  98. Port: 30002,
  99. Protocol: model.VLESS,
  100. Settings: `{"clients":[]}`,
  101. StreamSettings: `{"security":"tls","tlsSettings":{"settings":{"domains":[{"domain":"example.com"}]}}}`,
  102. }
  103. if err := db.Create(legacy).Error; err != nil {
  104. t.Fatalf("create legacy inbound: %v", err)
  105. }
  106. svc := InboundService{}
  107. svc.MigrationRequirements()
  108. var got model.Inbound
  109. if err := db.First(&got, legacy.Id).Error; err != nil {
  110. t.Fatalf("reload inbound: %v", err)
  111. }
  112. if got.Tag != "inbound-30002" {
  113. t.Fatalf("legacy 0.0.0.0: tag not stripped: got %q, want %q", got.Tag, "inbound-30002")
  114. }
  115. }
  116. func TestMigrationRemoveOrphanedTraffics(t *testing.T) {
  117. setupConflictDB(t)
  118. db := database.GetDB()
  119. clientSvc := &ClientService{}
  120. inboundSvc := &InboundService{}
  121. const attachedEmail = "[email protected]"
  122. attachedClient := model.Client{Email: attachedEmail, ID: "11111111-1111-1111-1111-111111111111", SubID: attachedEmail, Enable: true}
  123. attachedIb := mkInbound(t, 30003, model.VLESS, clientsSettings(t, []model.Client{attachedClient}))
  124. if err := clientSvc.SyncInbound(nil, attachedIb.Id, []model.Client{attachedClient}); err != nil {
  125. t.Fatalf("seed attached client: %v", err)
  126. }
  127. mkTraffic(t, attachedIb.Id, attachedEmail, 0, 0, 0, 0, true)
  128. const detachedEmail = "[email protected]"
  129. detachedClient := model.Client{Email: detachedEmail, ID: "22222222-2222-2222-2222-222222222222", SubID: detachedEmail, Enable: true}
  130. detachedIb := mkInbound(t, 30004, model.VLESS, clientsSettings(t, []model.Client{detachedClient}))
  131. if err := clientSvc.SyncInbound(nil, detachedIb.Id, []model.Client{detachedClient}); err != nil {
  132. t.Fatalf("seed detached client: %v", err)
  133. }
  134. mkTraffic(t, detachedIb.Id, detachedEmail, 123, 456, 0, 0, true)
  135. detachedRec := lookupClientRecord(t, detachedEmail)
  136. if _, err := clientSvc.Detach(inboundSvc, detachedRec.Id, []int{detachedIb.Id}); err != nil {
  137. t.Fatalf("Detach: %v", err)
  138. }
  139. const jsonOnlyEmail = "[email protected]"
  140. jsonOnlyClient := model.Client{Email: jsonOnlyEmail, ID: "33333333-3333-3333-3333-333333333333", SubID: jsonOnlyEmail, Enable: true}
  141. jsonOnlyIb := mkInbound(t, 30005, model.VLESS, clientsSettings(t, []model.Client{jsonOnlyClient}))
  142. mkTraffic(t, jsonOnlyIb.Id, jsonOnlyEmail, 0, 0, 0, 0, true)
  143. const trulyOrphanedEmail = "[email protected]"
  144. mkTraffic(t, attachedIb.Id, trulyOrphanedEmail, 0, 0, 0, 0, true)
  145. inboundSvc.MigrationRemoveOrphanedTraffics()
  146. cases := []struct {
  147. name string
  148. email string
  149. want int64
  150. }{
  151. {"attached, in clients table and JSON", attachedEmail, 1},
  152. {"detached-but-alive, in clients table only", detachedEmail, 1},
  153. {"seeder-skipped-but-live, in JSON only", jsonOnlyEmail, 1},
  154. {"truly orphaned, in neither", trulyOrphanedEmail, 0},
  155. }
  156. for _, c := range cases {
  157. t.Run(c.name, func(t *testing.T) {
  158. var got int64
  159. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", c.email).Count(&got).Error; err != nil {
  160. t.Fatalf("count client_traffics for %s: %v", c.email, err)
  161. }
  162. if got != c.want {
  163. t.Errorf("client_traffics count for %s: got %d, want %d", c.email, got, c.want)
  164. }
  165. })
  166. }
  167. }
  168. func TestMigrationRequirements_NormalizesShareAddressFields(t *testing.T) {
  169. setupConflictDB(t)
  170. db := database.GetDB()
  171. invalidStrategy := &model.Inbound{
  172. UserId: 1,
  173. Tag: "invalid-share-strategy",
  174. Enable: true,
  175. Port: 31001,
  176. Protocol: model.VLESS,
  177. Settings: `{"clients":[]}`,
  178. StreamSettings: `{"network":"tcp","security":"none"}`,
  179. }
  180. paddedStrategy := &model.Inbound{
  181. UserId: 1,
  182. Tag: "padded-share-strategy",
  183. Enable: true,
  184. Port: 31002,
  185. Protocol: model.VLESS,
  186. Settings: `{"clients":[]}`,
  187. StreamSettings: `{"network":"tcp","security":"none"}`,
  188. }
  189. invalidAddress := &model.Inbound{
  190. UserId: 1,
  191. Tag: "invalid-share-address",
  192. Enable: true,
  193. Port: 31003,
  194. Protocol: model.VLESS,
  195. Settings: `{"clients":[]}`,
  196. StreamSettings: `{"network":"tcp","security":"none"}`,
  197. }
  198. if err := db.Create(invalidStrategy).Error; err != nil {
  199. t.Fatalf("create invalid strategy inbound: %v", err)
  200. }
  201. if err := db.Create(paddedStrategy).Error; err != nil {
  202. t.Fatalf("create padded strategy inbound: %v", err)
  203. }
  204. if err := db.Create(invalidAddress).Error; err != nil {
  205. t.Fatalf("create invalid address inbound: %v", err)
  206. }
  207. if err := db.Model(&model.Inbound{}).Where("id = ?", invalidStrategy.Id).Updates(map[string]any{
  208. "share_addr_strategy": " auto ",
  209. "share_addr": " edge.example.com ",
  210. }).Error; err != nil {
  211. t.Fatalf("seed invalid share fields: %v", err)
  212. }
  213. if err := db.Model(&model.Inbound{}).Where("id = ?", paddedStrategy.Id).Updates(map[string]any{
  214. "share_addr_strategy": " listen ",
  215. "share_addr": " 10.0.0.1 ",
  216. }).Error; err != nil {
  217. t.Fatalf("seed padded share fields: %v", err)
  218. }
  219. if err := db.Model(&model.Inbound{}).Where("id = ?", invalidAddress.Id).Updates(map[string]any{
  220. "share_addr_strategy": "custom",
  221. "share_addr": "edge.example.com:8443",
  222. }).Error; err != nil {
  223. t.Fatalf("seed invalid address share fields: %v", err)
  224. }
  225. svc := InboundService{}
  226. svc.MigrationRequirements()
  227. var gotInvalid model.Inbound
  228. if err := db.First(&gotInvalid, invalidStrategy.Id).Error; err != nil {
  229. t.Fatalf("reload invalid strategy inbound: %v", err)
  230. }
  231. if gotInvalid.ShareAddrStrategy != "node" || gotInvalid.ShareAddr != "edge.example.com" {
  232. t.Fatalf("invalid share fields = (%q, %q), want (node, edge.example.com)", gotInvalid.ShareAddrStrategy, gotInvalid.ShareAddr)
  233. }
  234. var gotPadded model.Inbound
  235. if err := db.First(&gotPadded, paddedStrategy.Id).Error; err != nil {
  236. t.Fatalf("reload padded strategy inbound: %v", err)
  237. }
  238. if gotPadded.ShareAddrStrategy != "listen" || gotPadded.ShareAddr != "10.0.0.1" {
  239. t.Fatalf("padded share fields = (%q, %q), want (listen, 10.0.0.1)", gotPadded.ShareAddrStrategy, gotPadded.ShareAddr)
  240. }
  241. var gotInvalidAddress model.Inbound
  242. if err := db.First(&gotInvalidAddress, invalidAddress.Id).Error; err != nil {
  243. t.Fatalf("reload invalid address inbound: %v", err)
  244. }
  245. if gotInvalidAddress.ShareAddrStrategy != "node" || gotInvalidAddress.ShareAddr != "" {
  246. t.Fatalf("invalid address share fields = (%q, %q), want (node, empty)", gotInvalidAddress.ShareAddrStrategy, gotInvalidAddress.ShareAddr)
  247. }
  248. }