1
0

db.go 41 KB

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