1
0

db.go 38 KB

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