db.go 27 KB

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