db.go 45 KB

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