db.go 33 KB

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