1
0

db.go 33 KB

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