db.go 34 KB

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