db.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. // Package database provides database initialization, migration, and management utilities
  2. // for the 3x-ui panel using GORM with SQLite or PostgreSQL.
  3. package database
  4. import (
  5. "bytes"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "log"
  11. "math"
  12. "os"
  13. "path"
  14. "slices"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/mhsanaei/3x-ui/v3/internal/config"
  19. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  20. "github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
  21. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  22. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  23. "gorm.io/driver/postgres"
  24. "gorm.io/driver/sqlite"
  25. "gorm.io/gorm"
  26. "gorm.io/gorm/logger"
  27. )
  28. var db *gorm.DB
  29. const (
  30. DialectSQLite = "sqlite"
  31. DialectPostgres = "postgres"
  32. )
  33. // IsPostgres reports whether the active connection is a PostgreSQL backend.
  34. func IsPostgres() bool {
  35. if db == nil {
  36. return config.GetDBKind() == "postgres"
  37. }
  38. return db.Dialector.Name() == "postgres"
  39. }
  40. // Dialect returns the active GORM dialect name, or "" if the DB is not open.
  41. func Dialect() string {
  42. if db == nil {
  43. return ""
  44. }
  45. return db.Dialector.Name()
  46. }
  47. const (
  48. defaultUsername = "admin"
  49. defaultPassword = "admin"
  50. )
  51. func initModels() error {
  52. models := []any{
  53. &model.User{},
  54. &model.Inbound{},
  55. &model.OutboundTraffics{},
  56. &model.Setting{},
  57. &model.InboundClientIps{},
  58. &xray.ClientTraffic{},
  59. &model.HistoryOfSeeders{},
  60. &model.Node{},
  61. &model.ApiToken{},
  62. &model.ClientRecord{},
  63. &model.ClientInbound{},
  64. &model.ClientExternalLink{},
  65. &model.ClientGroup{},
  66. &model.InboundFallback{},
  67. &model.Host{},
  68. &model.NodeClientTraffic{},
  69. &model.NodeClientIp{},
  70. &model.ClientGlobalTraffic{},
  71. &model.OutboundSubscription{},
  72. }
  73. for _, mdl := range models {
  74. if err := db.AutoMigrate(mdl); err != nil {
  75. if isIgnorableDuplicateColumnErr(err, mdl) {
  76. log.Printf("Ignoring duplicate column during auto migration for %T: %v", mdl, err)
  77. continue
  78. }
  79. log.Printf("Error auto migrating model: %v", err)
  80. return err
  81. }
  82. }
  83. if err := migrateHostVerifyPeerCertByNameColumn(); err != nil {
  84. return err
  85. }
  86. if err := dropLegacyForeignKeys(); err != nil {
  87. return err
  88. }
  89. if err := pruneOrphanedClientInbounds(); err != nil {
  90. return err
  91. }
  92. if err := pruneOrphanedHosts(); err != nil {
  93. return err
  94. }
  95. if err := normalizeInboundSubSortIndex(); err != nil {
  96. return err
  97. }
  98. if IsPostgres() {
  99. if err := resyncPostgresSequences(db, models); err != nil {
  100. log.Printf("Error resyncing postgres sequences: %v", err)
  101. return err
  102. }
  103. }
  104. return nil
  105. }
  106. func dropLegacyForeignKeys() error {
  107. if !IsPostgres() {
  108. return nil
  109. }
  110. if err := db.Exec("ALTER TABLE client_traffics DROP CONSTRAINT IF EXISTS fk_inbounds_client_stats").Error; err != nil {
  111. log.Printf("Error dropping legacy foreign key fk_inbounds_client_stats: %v", err)
  112. return err
  113. }
  114. return nil
  115. }
  116. // migrateHostVerifyPeerCertByNameColumn converts hosts.verify_peer_cert_by_name
  117. // from its original boolean shape to the comma-separated string xray-core's
  118. // verifyPeerCertByName (vcn) actually expects. The legacy boolean was dead
  119. // (never emitted into links), so its value carries no meaning and is discarded.
  120. // Idempotent by construction (no HistoryOfSeeders row — writing one here would
  121. // flip the fresh-DB detection in runSeeders). Runs right after AutoMigrate,
  122. // before anything reads or writes Host rows (critical on Postgres, where the
  123. // column stays boolean-typed until the ALTER below).
  124. func migrateHostVerifyPeerCertByNameColumn() error {
  125. if !db.Migrator().HasColumn(&model.Host{}, "verify_peer_cert_by_name") {
  126. return nil
  127. }
  128. if IsPostgres() {
  129. // Only convert a still-boolean column; once it is text this is a no-op,
  130. // so a user-set name is never wiped on a later restart.
  131. var dataType string
  132. if err := db.Raw(
  133. `SELECT data_type FROM information_schema.columns WHERE table_name = 'hosts' AND column_name = 'verify_peer_cert_by_name'`,
  134. ).Scan(&dataType).Error; err != nil {
  135. return err
  136. }
  137. if dataType != "boolean" {
  138. return nil
  139. }
  140. if err := db.Exec(`ALTER TABLE hosts ALTER COLUMN verify_peer_cert_by_name DROP DEFAULT`).Error; err != nil {
  141. return err
  142. }
  143. return db.Exec(`ALTER TABLE hosts ALTER COLUMN verify_peer_cert_by_name TYPE text USING ''`).Error
  144. }
  145. // SQLite keeps the original numeric-affinity column; blank any legacy
  146. // integer/null value so it doesn't read back as "0"/"1". After conversion
  147. // every value is text, so re-running touches nothing.
  148. return db.Exec(`UPDATE hosts SET verify_peer_cert_by_name = '' WHERE verify_peer_cert_by_name IS NULL OR typeof(verify_peer_cert_by_name) <> 'text'`).Error
  149. }
  150. // seedHostsFromExternalProxy is a one-time, self-gated migration that creates a
  151. // Host row for every legacy externalProxy entry on every inbound. Additive: the
  152. // externalProxy arrays are left intact in StreamSettings.
  153. func seedHostsFromExternalProxy() error {
  154. var history []string
  155. if err := db.Model(&model.HistoryOfSeeders{}).Pluck("seeder_name", &history).Error; err != nil {
  156. return err
  157. }
  158. if slices.Contains(history, "HostsFromExternalProxy") {
  159. return nil
  160. }
  161. var inbounds []model.Inbound
  162. if err := db.Find(&inbounds).Error; err != nil {
  163. return err
  164. }
  165. return db.Transaction(func(tx *gorm.DB) error {
  166. for _, inbound := range inbounds {
  167. if strings.TrimSpace(inbound.StreamSettings) == "" {
  168. continue
  169. }
  170. var stream map[string]any
  171. if err := json.Unmarshal([]byte(inbound.StreamSettings), &stream); err != nil {
  172. log.Printf("HostsFromExternalProxy: skip inbound %d (invalid stream json): %v", inbound.Id, err)
  173. continue
  174. }
  175. eps, ok := stream["externalProxy"].([]any)
  176. if !ok || len(eps) == 0 {
  177. continue
  178. }
  179. for i, raw := range eps {
  180. ep, ok := raw.(map[string]any)
  181. if !ok {
  182. continue
  183. }
  184. if err := tx.Create(externalProxyEntryToHost(inbound.Id, i, ep)).Error; err != nil {
  185. return err
  186. }
  187. }
  188. }
  189. return tx.Create(&model.HistoryOfSeeders{SeederName: "HostsFromExternalProxy"}).Error
  190. })
  191. }
  192. // externalProxyEntryToHost maps one legacy externalProxy entry onto a Host.
  193. // forceTls (same|tls|none) maps straight to Security; an unknown value falls back
  194. // to "same" (inherit). An empty remark gets a stable generated label so the row
  195. // stays valid/editable, and the remark is capped at the model's 256-char limit.
  196. func externalProxyEntryToHost(inboundId, index int, ep map[string]any) *model.Host {
  197. security, _ := ep["forceTls"].(string)
  198. switch security {
  199. case "same", "tls", "none":
  200. default:
  201. security = "same"
  202. }
  203. dest, _ := ep["dest"].(string)
  204. port := 0
  205. if p, ok := ep["port"].(float64); ok {
  206. port = int(p)
  207. }
  208. remark, _ := ep["remark"].(string)
  209. if strings.TrimSpace(remark) == "" {
  210. remark = "imported " + strconv.Itoa(index+1)
  211. }
  212. if len(remark) > 256 {
  213. remark = remark[:256]
  214. }
  215. sni, _ := ep["sni"].(string)
  216. fingerprint, _ := ep["fingerprint"].(string)
  217. ech, _ := ep["echConfigList"].(string)
  218. return &model.Host{
  219. InboundId: inboundId,
  220. SortOrder: index,
  221. Remark: remark,
  222. Address: dest,
  223. Port: port,
  224. Security: security,
  225. Sni: sni,
  226. Fingerprint: fingerprint,
  227. Alpn: anyToNonEmptyStrings(ep["alpn"]),
  228. PinnedPeerCertSha256: anyToNonEmptyStrings(ep["pinnedPeerCertSha256"]),
  229. EchConfigList: ech,
  230. }
  231. }
  232. func anyToNonEmptyStrings(v any) []string {
  233. switch t := v.(type) {
  234. case []any:
  235. out := make([]string, 0, len(t))
  236. for _, e := range t {
  237. if s, ok := e.(string); ok && s != "" {
  238. out = append(out, s)
  239. }
  240. }
  241. return out
  242. case []string:
  243. out := make([]string, 0, len(t))
  244. for _, s := range t {
  245. if s != "" {
  246. out = append(out, s)
  247. }
  248. }
  249. return out
  250. default:
  251. return nil
  252. }
  253. }
  254. func pruneOrphanedHosts() error {
  255. res := db.Exec("DELETE FROM hosts WHERE inbound_id NOT IN (SELECT id FROM inbounds)")
  256. if res.Error != nil {
  257. log.Printf("Error pruning orphaned hosts rows: %v", res.Error)
  258. return res.Error
  259. }
  260. if res.RowsAffected > 0 {
  261. log.Printf("Pruned %d orphaned hosts row(s)", res.RowsAffected)
  262. }
  263. return nil
  264. }
  265. func pruneOrphanedClientInbounds() error {
  266. res := db.Exec("DELETE FROM client_inbounds WHERE inbound_id NOT IN (SELECT id FROM inbounds)")
  267. if res.Error != nil {
  268. log.Printf("Error pruning orphaned client_inbounds rows: %v", res.Error)
  269. return res.Error
  270. }
  271. if res.RowsAffected > 0 {
  272. log.Printf("Pruned %d orphaned client_inbounds row(s)", res.RowsAffected)
  273. }
  274. return nil
  275. }
  276. // normalizeInboundSubSortIndex lifts sub_sort_index values below the 1-based
  277. // minimum (rows written by builds that defaulted the column to 0, or by nodes
  278. // predating the field) so they cannot sort ahead of explicitly ranked inbounds.
  279. func normalizeInboundSubSortIndex() error {
  280. res := db.Exec("UPDATE inbounds SET sub_sort_index = 1 WHERE sub_sort_index < 1")
  281. if res.Error != nil {
  282. log.Printf("Error normalizing inbound sub_sort_index: %v", res.Error)
  283. return res.Error
  284. }
  285. if res.RowsAffected > 0 {
  286. log.Printf("Normalized sub_sort_index on %d inbound(s)", res.RowsAffected)
  287. }
  288. return nil
  289. }
  290. func isIgnorableDuplicateColumnErr(err error, mdl any) bool {
  291. if err == nil {
  292. return false
  293. }
  294. errMsg := strings.ToLower(err.Error())
  295. // SQLite: "duplicate column name: foo"
  296. // Postgres: `pq: column "foo" of relation "bar" already exists` / `sqlstate 42701`
  297. const sqlitePrefix = "duplicate column name:"
  298. if _, after, ok := strings.Cut(errMsg, sqlitePrefix); ok {
  299. col := strings.TrimSpace(after)
  300. col = strings.Trim(col, "`\"[]")
  301. return col != "" && db != nil && db.Migrator().HasColumn(mdl, col)
  302. }
  303. if strings.Contains(errMsg, "already exists") && strings.Contains(errMsg, "column ") {
  304. // Best effort: extract the column name between the first pair of double quotes.
  305. if _, after, ok := strings.Cut(errMsg, "column \""); ok {
  306. rest := after
  307. if e := strings.Index(rest, "\""); e > 0 {
  308. col := rest[:e]
  309. return col != "" && db != nil && db.Migrator().HasColumn(mdl, col)
  310. }
  311. }
  312. }
  313. return false
  314. }
  315. // initUser creates a default admin user if the users table is empty.
  316. func initUser() error {
  317. empty, err := isTableEmpty("users")
  318. if err != nil {
  319. log.Printf("Error checking if users table is empty: %v", err)
  320. return err
  321. }
  322. if empty {
  323. hashedPassword, err := crypto.HashPasswordAsBcrypt(defaultPassword)
  324. if err != nil {
  325. log.Printf("Error hashing default password: %v", err)
  326. return err
  327. }
  328. user := &model.User{
  329. Username: defaultUsername,
  330. Password: hashedPassword,
  331. }
  332. return db.Create(user).Error
  333. }
  334. return nil
  335. }
  336. // runSeeders migrates user passwords to bcrypt and records seeder execution to prevent re-running.
  337. func runSeeders(isUsersEmpty bool) error {
  338. empty, err := isTableEmpty("history_of_seeders")
  339. if err != nil {
  340. log.Printf("Error checking if users table is empty: %v", err)
  341. return err
  342. }
  343. if empty && isUsersEmpty {
  344. seeders := []string{"UserPasswordHash", "ClientsTable", "InboundClientsArrayFix", "InboundClientTgIdFix", "InboundClientSubIdFix", "FreedomFinalRulesReverseFix", "ApiTokensHash", "LegacyProxySettingsCleanup"}
  345. for _, name := range seeders {
  346. if err := db.Create(&model.HistoryOfSeeders{SeederName: name}).Error; err != nil {
  347. return err
  348. }
  349. }
  350. return seedApiTokens()
  351. }
  352. var seedersHistory []string
  353. if err := db.Model(&model.HistoryOfSeeders{}).Pluck("seeder_name", &seedersHistory).Error; err != nil {
  354. log.Printf("Error fetching seeder history: %v", err)
  355. return err
  356. }
  357. if !slices.Contains(seedersHistory, "UserPasswordHash") && !isUsersEmpty {
  358. var users []model.User
  359. if err := db.Find(&users).Error; err != nil {
  360. log.Printf("Error fetching users for password migration: %v", err)
  361. return err
  362. }
  363. for _, user := range users {
  364. if crypto.IsHashed(user.Password) {
  365. continue
  366. }
  367. hashedPassword, err := crypto.HashPasswordAsBcrypt(user.Password)
  368. if err != nil {
  369. log.Printf("Error hashing password for user '%s': %v", user.Username, err)
  370. return err
  371. }
  372. if err := db.Model(&user).Update("password", hashedPassword).Error; err != nil {
  373. log.Printf("Error updating password for user '%s': %v", user.Username, err)
  374. return err
  375. }
  376. }
  377. hashSeeder := &model.HistoryOfSeeders{
  378. SeederName: "UserPasswordHash",
  379. }
  380. if err := db.Create(hashSeeder).Error; err != nil {
  381. return err
  382. }
  383. }
  384. if !slices.Contains(seedersHistory, "ApiTokensTable") {
  385. if err := seedApiTokens(); err != nil {
  386. return err
  387. }
  388. }
  389. if !slices.Contains(seedersHistory, "ApiTokensHash") {
  390. if err := hashExistingApiTokens(); err != nil {
  391. return err
  392. }
  393. }
  394. if !slices.Contains(seedersHistory, "ClientsTable") {
  395. if err := seedClientsFromInboundJSON(); err != nil {
  396. return err
  397. }
  398. }
  399. if !slices.Contains(seedersHistory, "InboundClientsArrayFix") {
  400. if err := normalizeInboundClientsArray(); err != nil {
  401. return err
  402. }
  403. }
  404. if !slices.Contains(seedersHistory, "InboundClientTgIdFix") {
  405. if err := normalizeInboundClientTgId(); err != nil {
  406. return err
  407. }
  408. }
  409. if !slices.Contains(seedersHistory, "InboundClientSubIdFix") {
  410. if err := normalizeInboundClientSubId(); err != nil {
  411. return err
  412. }
  413. }
  414. if !slices.Contains(seedersHistory, "FreedomFinalRulesReverseFix") {
  415. if err := normalizeFreedomFinalRules(); err != nil {
  416. return err
  417. }
  418. }
  419. if !slices.Contains(seedersHistory, "LegacyProxySettingsCleanup") {
  420. if err := clearLegacyProxySettings(); err != nil {
  421. return err
  422. }
  423. }
  424. // Self-gated on the "HostsFromExternalProxy" row, so it is safe to call
  425. // unconditionally here.
  426. if err := seedHostsFromExternalProxy(); err != nil {
  427. return err
  428. }
  429. return nil
  430. }
  431. // clearLegacyProxySettings drops the deprecated panelProxy/tgBotProxy rows so a
  432. // stale tgBotProxy no longer masks the panelOutbound egress fallback.
  433. func clearLegacyProxySettings() error {
  434. return db.Transaction(func(tx *gorm.DB) error {
  435. if err := tx.Where("key IN ?", []string{"panelProxy", "tgBotProxy"}).
  436. Delete(&model.Setting{}).Error; err != nil {
  437. return err
  438. }
  439. return tx.Create(&model.HistoryOfSeeders{SeederName: "LegacyProxySettingsCleanup"}).Error
  440. })
  441. }
  442. func normalizeInboundClientTgId() error {
  443. var inbounds []model.Inbound
  444. if err := db.Find(&inbounds).Error; err != nil {
  445. return err
  446. }
  447. return db.Transaction(func(tx *gorm.DB) error {
  448. for _, inbound := range inbounds {
  449. if strings.TrimSpace(inbound.Settings) == "" {
  450. continue
  451. }
  452. var settings map[string]any
  453. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  454. log.Printf("InboundClientTgIdFix: skip inbound %d (invalid settings json): %v", inbound.Id, err)
  455. continue
  456. }
  457. clients, ok := settings["clients"].([]any)
  458. if !ok {
  459. continue
  460. }
  461. mutated := false
  462. for i, raw := range clients {
  463. obj, ok := raw.(map[string]any)
  464. if !ok {
  465. continue
  466. }
  467. tgRaw, present := obj["tgId"]
  468. if !present {
  469. continue
  470. }
  471. v, isFloat := tgRaw.(float64)
  472. if isFloat && !math.IsNaN(v) && !math.IsInf(v, 0) && v == math.Trunc(v) {
  473. continue
  474. }
  475. obj["tgId"] = int64(0)
  476. clients[i] = obj
  477. mutated = true
  478. }
  479. if !mutated {
  480. continue
  481. }
  482. settings["clients"] = clients
  483. newSettings, err := json.MarshalIndent(settings, "", " ")
  484. if err != nil {
  485. log.Printf("InboundClientTgIdFix: skip inbound %d (marshal failed): %v", inbound.Id, err)
  486. continue
  487. }
  488. if err := tx.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
  489. Update("settings", string(newSettings)).Error; err != nil {
  490. return err
  491. }
  492. }
  493. return tx.Create(&model.HistoryOfSeeders{SeederName: "InboundClientTgIdFix"}).Error
  494. })
  495. }
  496. func normalizeInboundClientSubId() error {
  497. var inbounds []model.Inbound
  498. if err := db.Find(&inbounds).Error; err != nil {
  499. return err
  500. }
  501. return db.Transaction(func(tx *gorm.DB) error {
  502. for _, inbound := range inbounds {
  503. if strings.TrimSpace(inbound.Settings) == "" {
  504. continue
  505. }
  506. var settings map[string]any
  507. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  508. log.Printf("InboundClientSubIdFix: skip inbound %d (invalid settings json): %v", inbound.Id, err)
  509. continue
  510. }
  511. clients, ok := settings["clients"].([]any)
  512. if !ok {
  513. continue
  514. }
  515. mutated := false
  516. for i, raw := range clients {
  517. obj, ok := raw.(map[string]any)
  518. if !ok {
  519. continue
  520. }
  521. existing, _ := obj["subId"].(string)
  522. if strings.TrimSpace(existing) != "" {
  523. continue
  524. }
  525. obj["subId"] = random.NumLower(16)
  526. clients[i] = obj
  527. mutated = true
  528. }
  529. if !mutated {
  530. continue
  531. }
  532. settings["clients"] = clients
  533. newSettings, err := json.MarshalIndent(settings, "", " ")
  534. if err != nil {
  535. log.Printf("InboundClientSubIdFix: skip inbound %d (marshal failed): %v", inbound.Id, err)
  536. continue
  537. }
  538. if err := tx.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
  539. Update("settings", string(newSettings)).Error; err != nil {
  540. return err
  541. }
  542. }
  543. return tx.Create(&model.HistoryOfSeeders{SeederName: "InboundClientSubIdFix"}).Error
  544. })
  545. }
  546. func normalizeInboundClientsArray() error {
  547. var inbounds []model.Inbound
  548. if err := db.Find(&inbounds).Error; err != nil {
  549. return err
  550. }
  551. return db.Transaction(func(tx *gorm.DB) error {
  552. for _, inbound := range inbounds {
  553. if strings.TrimSpace(inbound.Settings) == "" {
  554. continue
  555. }
  556. var settings map[string]any
  557. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  558. log.Printf("InboundClientsArrayFix: skip inbound %d (invalid settings json): %v", inbound.Id, err)
  559. continue
  560. }
  561. raw, exists := settings["clients"]
  562. if !exists || raw != nil {
  563. continue
  564. }
  565. settings["clients"] = []any{}
  566. newSettings, err := json.MarshalIndent(settings, "", " ")
  567. if err != nil {
  568. log.Printf("InboundClientsArrayFix: skip inbound %d (marshal failed): %v", inbound.Id, err)
  569. continue
  570. }
  571. if err := tx.Model(&model.Inbound{}).Where("id = ?", inbound.Id).
  572. Update("settings", string(newSettings)).Error; err != nil {
  573. return err
  574. }
  575. }
  576. return tx.Create(&model.HistoryOfSeeders{SeederName: "InboundClientsArrayFix"}).Error
  577. })
  578. }
  579. func normalizeFreedomFinalRules() error {
  580. var setting model.Setting
  581. err := db.Model(model.Setting{}).Where("key = ?", "xrayTemplateConfig").First(&setting).Error
  582. if errors.Is(err, gorm.ErrRecordNotFound) {
  583. return db.Create(&model.HistoryOfSeeders{SeederName: "FreedomFinalRulesReverseFix"}).Error
  584. }
  585. if err != nil {
  586. return err
  587. }
  588. updated, changed, rErr := rewriteFreedomFinalRules(setting.Value)
  589. if rErr != nil {
  590. log.Printf("FreedomFinalRulesReverseFix: skip (invalid xrayTemplateConfig json): %v", rErr)
  591. return db.Create(&model.HistoryOfSeeders{SeederName: "FreedomFinalRulesReverseFix"}).Error
  592. }
  593. return db.Transaction(func(tx *gorm.DB) error {
  594. if changed {
  595. if err := tx.Model(&model.Setting{}).Where("key = ?", "xrayTemplateConfig").
  596. Update("value", updated).Error; err != nil {
  597. return err
  598. }
  599. }
  600. return tx.Create(&model.HistoryOfSeeders{SeederName: "FreedomFinalRulesReverseFix"}).Error
  601. })
  602. }
  603. func rewriteFreedomFinalRules(raw string) (string, bool, error) {
  604. if strings.TrimSpace(raw) == "" {
  605. return raw, false, nil
  606. }
  607. var cfg map[string]any
  608. if err := json.Unmarshal([]byte(raw), &cfg); err != nil {
  609. return raw, false, err
  610. }
  611. outbounds, ok := cfg["outbounds"].([]any)
  612. if !ok {
  613. return raw, false, nil
  614. }
  615. changed := false
  616. for _, ob := range outbounds {
  617. obj, ok := ob.(map[string]any)
  618. if !ok {
  619. continue
  620. }
  621. if proto, _ := obj["protocol"].(string); proto != "freedom" {
  622. continue
  623. }
  624. settings, ok := obj["settings"].(map[string]any)
  625. if !ok {
  626. continue
  627. }
  628. if !isLegacyPrivateOnlyFinalRules(settings["finalRules"]) {
  629. continue
  630. }
  631. settings["finalRules"] = []any{map[string]any{"action": "allow"}}
  632. changed = true
  633. }
  634. if !changed {
  635. return raw, false, nil
  636. }
  637. out, err := json.MarshalIndent(cfg, "", " ")
  638. if err != nil {
  639. return raw, false, err
  640. }
  641. return string(out), true, nil
  642. }
  643. func isLegacyPrivateOnlyFinalRules(v any) bool {
  644. rules, ok := v.([]any)
  645. if !ok || len(rules) != 1 {
  646. return false
  647. }
  648. rule, ok := rules[0].(map[string]any)
  649. if !ok {
  650. return false
  651. }
  652. if action, _ := rule["action"].(string); action != "allow" {
  653. return false
  654. }
  655. ips, ok := rule["ip"].([]any)
  656. if !ok || len(ips) != 1 {
  657. return false
  658. }
  659. if s, _ := ips[0].(string); s != "geoip:private" {
  660. return false
  661. }
  662. for k := range rule {
  663. if k != "action" && k != "ip" {
  664. return false
  665. }
  666. }
  667. return true
  668. }
  669. // normalizeClientJSONFields coerces loosely-typed numeric fields in a raw
  670. // settings.clients entry so json.Unmarshal into model.Client doesn't fail
  671. // when older rows wrote tgId/limitIp/totalGB/etc. as strings. Empty strings
  672. // drop the key so the field falls back to its zero value.
  673. func normalizeClientJSONFields(obj map[string]any) {
  674. normalizeInt := func(key string) {
  675. raw, exists := obj[key]
  676. if !exists {
  677. return
  678. }
  679. s, ok := raw.(string)
  680. if !ok {
  681. return
  682. }
  683. trimmed := strings.ReplaceAll(strings.TrimSpace(s), " ", "")
  684. if trimmed == "" {
  685. delete(obj, key)
  686. return
  687. }
  688. if n, err := strconv.ParseInt(trimmed, 10, 64); err == nil {
  689. obj[key] = n
  690. } else {
  691. delete(obj, key)
  692. }
  693. }
  694. for _, k := range []string{"tgId", "limitIp", "totalGB", "expiryTime", "reset", "created_at", "updated_at"} {
  695. normalizeInt(k)
  696. }
  697. }
  698. func seedClientsFromInboundJSON() error {
  699. var inbounds []model.Inbound
  700. if err := db.Find(&inbounds).Error; err != nil {
  701. return err
  702. }
  703. return db.Transaction(func(tx *gorm.DB) error {
  704. byEmail := map[string]*model.ClientRecord{}
  705. var existing []model.ClientRecord
  706. if err := tx.Find(&existing).Error; err != nil {
  707. return err
  708. }
  709. for i := range existing {
  710. byEmail[existing[i].Email] = &existing[i]
  711. }
  712. for _, inbound := range inbounds {
  713. if strings.TrimSpace(inbound.Settings) == "" {
  714. continue
  715. }
  716. var settings map[string]any
  717. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  718. log.Printf("ClientsTable seed: skip inbound %d (invalid settings json): %v", inbound.Id, err)
  719. continue
  720. }
  721. rawList, ok := settings["clients"].([]any)
  722. if !ok {
  723. continue
  724. }
  725. for _, raw := range rawList {
  726. obj, ok := raw.(map[string]any)
  727. if !ok {
  728. continue
  729. }
  730. normalizeClientJSONFields(obj)
  731. blob, err := json.Marshal(obj)
  732. if err != nil {
  733. continue
  734. }
  735. var c model.Client
  736. if err := json.Unmarshal(blob, &c); err != nil {
  737. log.Printf("ClientsTable seed: skip client in inbound %d (unmarshal failed): %v; payload=%s",
  738. inbound.Id, err, string(blob))
  739. continue
  740. }
  741. email := strings.TrimSpace(c.Email)
  742. if email == "" {
  743. continue
  744. }
  745. incoming := c.ToRecord()
  746. row, dup := byEmail[email]
  747. if !dup {
  748. if err := tx.Create(incoming).Error; err != nil {
  749. return err
  750. }
  751. byEmail[email] = incoming
  752. row = incoming
  753. } else {
  754. conflicts := model.MergeClientRecord(row, incoming)
  755. for _, x := range conflicts {
  756. log.Printf("client merge: email=%s conflict on %s old=%v new=%v kept=%v",
  757. email, x.Field, x.Old, x.New, x.Kept)
  758. }
  759. if err := tx.Save(row).Error; err != nil {
  760. return err
  761. }
  762. }
  763. link := model.ClientInbound{
  764. ClientId: row.Id,
  765. InboundId: inbound.Id,
  766. FlowOverride: c.Flow,
  767. }
  768. if err := tx.Where("client_id = ? AND inbound_id = ?", row.Id, inbound.Id).
  769. FirstOrCreate(&link).Error; err != nil {
  770. return err
  771. }
  772. }
  773. }
  774. return tx.Create(&model.HistoryOfSeeders{SeederName: "ClientsTable"}).Error
  775. })
  776. }
  777. // seedApiTokens copies the legacy `apiToken` setting into the new
  778. // api_tokens table as a row named "default" so existing central panels
  779. // keep working after the upgrade. Idempotent — records itself in
  780. // history_of_seeders and only runs when api_tokens is empty.
  781. func seedApiTokens() error {
  782. empty, err := isTableEmpty("api_tokens")
  783. if err != nil {
  784. return err
  785. }
  786. if empty {
  787. var legacy model.Setting
  788. err := db.Model(model.Setting{}).Where("key = ?", "apiToken").First(&legacy).Error
  789. if err == nil && legacy.Value != "" {
  790. row := &model.ApiToken{
  791. Name: "default",
  792. Token: legacy.Value,
  793. Enabled: true,
  794. }
  795. if err := db.Create(row).Error; err != nil {
  796. log.Printf("Error migrating legacy apiToken: %v", err)
  797. return err
  798. }
  799. }
  800. }
  801. return db.Create(&model.HistoryOfSeeders{SeederName: "ApiTokensTable"}).Error
  802. }
  803. // hashExistingApiTokens replaces any plaintext token stored before tokens were
  804. // hashed at rest with its SHA-256 digest. Callers keep their plaintext copy
  805. // (used on remote nodes), so existing tokens keep authenticating; the panel
  806. // just can no longer reveal them. Idempotent — already-hashed rows are skipped.
  807. func hashExistingApiTokens() error {
  808. var rows []*model.ApiToken
  809. if err := db.Find(&rows).Error; err != nil {
  810. return err
  811. }
  812. for _, r := range rows {
  813. if crypto.IsSHA256Hex(r.Token) {
  814. continue
  815. }
  816. hashed := crypto.HashTokenSHA256(r.Token)
  817. if err := db.Model(model.ApiToken{}).Where("id = ?", r.Id).Update("token", hashed).Error; err != nil {
  818. log.Printf("Error hashing api token %d: %v", r.Id, err)
  819. return err
  820. }
  821. }
  822. return db.Create(&model.HistoryOfSeeders{SeederName: "ApiTokensHash"}).Error
  823. }
  824. // isTableEmpty returns true if the named table contains zero rows.
  825. func isTableEmpty(tableName string) (bool, error) {
  826. var count int64
  827. err := db.Table(tableName).Count(&count).Error
  828. return count == 0, err
  829. }
  830. // InitDB sets up the database connection, migrates models, and runs seeders.
  831. // When XUI_DB_TYPE=postgres, dbPath is ignored and XUI_DB_DSN is used instead.
  832. func InitDB(dbPath string) error {
  833. var gormLogger logger.Interface
  834. if config.IsDebug() {
  835. gormLogger = logger.New(
  836. log.New(os.Stdout, "\r\n", log.LstdFlags),
  837. logger.Config{
  838. SlowThreshold: time.Second,
  839. LogLevel: logger.Info,
  840. IgnoreRecordNotFoundError: true,
  841. Colorful: true,
  842. },
  843. )
  844. } else {
  845. gormLogger = logger.Discard
  846. }
  847. c := &gorm.Config{Logger: gormLogger, DisableForeignKeyConstraintWhenMigrating: true}
  848. var err error
  849. switch config.GetDBKind() {
  850. case "postgres":
  851. dsn := config.GetDBDSN()
  852. if dsn == "" {
  853. return errors.New("XUI_DB_TYPE=postgres but XUI_DB_DSN is empty")
  854. }
  855. db, err = gorm.Open(postgres.Open(dsn), c)
  856. if err != nil {
  857. return err
  858. }
  859. default:
  860. dir := path.Dir(dbPath)
  861. if err = os.MkdirAll(dir, 0755); err != nil {
  862. return err
  863. }
  864. // Keep journal_mode=DELETE so the DB stays a single file (no -wal/-shm
  865. // sidecars). synchronous defaults to FULL for durability but is tunable.
  866. sync := sqliteSynchronous()
  867. dsn := dbPath + "?_journal_mode=DELETE&_busy_timeout=10000&_synchronous=" + sync + "&_txlock=immediate"
  868. db, err = gorm.Open(sqlite.Open(dsn), c)
  869. if err != nil {
  870. return err
  871. }
  872. sqlDB, err := db.DB()
  873. if err != nil {
  874. return err
  875. }
  876. // Re-assert the DSN pragmas plus scan-friendly ones for large datasets.
  877. // cache_size/mmap_size/temp_store create no extra files, so the single-file
  878. // guarantee holds; they just cut disk I/O on the 50k-row hot paths.
  879. pragmas := []string{
  880. "PRAGMA journal_mode=DELETE",
  881. "PRAGMA busy_timeout=10000",
  882. "PRAGMA synchronous=" + sync,
  883. fmt.Sprintf("PRAGMA cache_size=-%d", envInt("XUI_DB_CACHE_MB", 32)*1024),
  884. fmt.Sprintf("PRAGMA mmap_size=%d", int64(envInt("XUI_DB_MMAP_MB", 256))*1024*1024),
  885. "PRAGMA temp_store=MEMORY",
  886. }
  887. for _, p := range pragmas {
  888. if _, err := sqlDB.Exec(p); err != nil {
  889. return err
  890. }
  891. }
  892. }
  893. sqlDB, err := db.DB()
  894. if err != nil {
  895. return err
  896. }
  897. var maxOpen, maxIdle int
  898. switch config.GetDBKind() {
  899. case "postgres":
  900. maxOpen = envInt("XUI_DB_MAX_OPEN_CONNS", 25)
  901. maxIdle = envInt("XUI_DB_MAX_IDLE_CONNS", 25)
  902. default:
  903. maxOpen = envInt("XUI_DB_MAX_OPEN_CONNS", 8)
  904. maxIdle = envInt("XUI_DB_MAX_IDLE_CONNS", 4)
  905. }
  906. sqlDB.SetMaxOpenConns(maxOpen)
  907. sqlDB.SetMaxIdleConns(maxIdle)
  908. sqlDB.SetConnMaxLifetime(time.Hour)
  909. sqlDB.SetConnMaxIdleTime(30 * time.Minute)
  910. if err := initModels(); err != nil {
  911. return err
  912. }
  913. isUsersEmpty, err := isTableEmpty("users")
  914. if err != nil {
  915. return err
  916. }
  917. if err := initUser(); err != nil {
  918. return err
  919. }
  920. return runSeeders(isUsersEmpty)
  921. }
  922. // sqliteSynchronous returns the SQLite synchronous mode, defaulting to FULL.
  923. // Whitelisted because the value is interpolated directly into a PRAGMA string.
  924. func sqliteSynchronous() string {
  925. switch strings.ToUpper(strings.TrimSpace(os.Getenv("XUI_DB_SYNCHRONOUS"))) {
  926. case "OFF":
  927. return "OFF"
  928. case "NORMAL":
  929. return "NORMAL"
  930. case "EXTRA":
  931. return "EXTRA"
  932. default:
  933. return "FULL"
  934. }
  935. }
  936. func envInt(key string, def int) int {
  937. v := strings.TrimSpace(os.Getenv(key))
  938. if v == "" {
  939. return def
  940. }
  941. n, err := strconv.Atoi(v)
  942. if err != nil || n <= 0 {
  943. return def
  944. }
  945. return n
  946. }
  947. // CloseDB closes the database connection if it exists.
  948. func CloseDB() error {
  949. if db != nil {
  950. sqlDB, err := db.DB()
  951. if err != nil {
  952. return err
  953. }
  954. return sqlDB.Close()
  955. }
  956. return nil
  957. }
  958. // GetDB returns the global GORM database instance.
  959. func GetDB() *gorm.DB {
  960. return db
  961. }
  962. func IsNotFound(err error) bool {
  963. return errors.Is(err, gorm.ErrRecordNotFound)
  964. }
  965. // IsSQLiteDB checks if the given file is a valid SQLite database by reading its signature.
  966. func IsSQLiteDB(file io.ReaderAt) (bool, error) {
  967. signature := []byte("SQLite format 3\x00")
  968. buf := make([]byte, len(signature))
  969. _, err := file.ReadAt(buf, 0)
  970. if err != nil {
  971. return false, err
  972. }
  973. return bytes.Equal(buf, signature), nil
  974. }
  975. // Checkpoint performs a WAL checkpoint on the SQLite database to ensure data consistency.
  976. // No-op on PostgreSQL (WAL there is managed by the server).
  977. func Checkpoint() error {
  978. if IsPostgres() {
  979. return nil
  980. }
  981. return db.Exec("PRAGMA wal_checkpoint;").Error
  982. }
  983. // ValidateSQLiteDB opens the provided sqlite DB path with a throw-away connection
  984. // and runs a PRAGMA integrity_check to ensure the file is structurally sound.
  985. // It does not mutate global state or run migrations.
  986. func ValidateSQLiteDB(dbPath string) error {
  987. if _, err := os.Stat(dbPath); err != nil { // file must exist
  988. return err
  989. }
  990. gdb, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{Logger: logger.Discard})
  991. if err != nil {
  992. return err
  993. }
  994. sqlDB, err := gdb.DB()
  995. if err != nil {
  996. return err
  997. }
  998. defer sqlDB.Close()
  999. var res string
  1000. if err := gdb.Raw("PRAGMA integrity_check;").Scan(&res).Error; err != nil {
  1001. return err
  1002. }
  1003. if res != "ok" {
  1004. return errors.New("sqlite integrity check failed: " + res)
  1005. }
  1006. return nil
  1007. }