config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Package config provides configuration management utilities for the 3x-ui panel,
  2. // including version information, logging levels, database paths, and environment variable handling.
  3. package config
  4. import (
  5. _ "embed"
  6. "fmt"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. )
  13. //go:embed version
  14. var version string
  15. //go:embed name
  16. var name string
  17. // LogLevel represents the logging level for the application.
  18. type LogLevel string
  19. // Logging level constants
  20. const (
  21. Debug LogLevel = "debug"
  22. Info LogLevel = "info"
  23. Notice LogLevel = "notice"
  24. Warning LogLevel = "warning"
  25. Error LogLevel = "error"
  26. )
  27. // GetVersion returns the version string of the 3x-ui application.
  28. func GetVersion() string {
  29. return strings.TrimSpace(version)
  30. }
  31. // GetName returns the name of the 3x-ui application.
  32. func GetName() string {
  33. return strings.TrimSpace(name)
  34. }
  35. // GetLogLevel returns the current logging level based on environment variables or defaults to Info.
  36. func GetLogLevel() LogLevel {
  37. if IsDebug() {
  38. return Debug
  39. }
  40. logLevel := os.Getenv("XUI_LOG_LEVEL")
  41. if logLevel == "" {
  42. return Info
  43. }
  44. return LogLevel(logLevel)
  45. }
  46. // IsDebug returns true if debug mode is enabled via the XUI_DEBUG environment variable.
  47. func IsDebug() bool {
  48. return os.Getenv("XUI_DEBUG") == "true"
  49. }
  50. // GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
  51. func GetBinFolderPath() string {
  52. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  53. if binFolderPath == "" {
  54. binFolderPath = "bin"
  55. }
  56. return binFolderPath
  57. }
  58. func getBaseDir() string {
  59. exePath, err := os.Executable()
  60. if err != nil {
  61. return "."
  62. }
  63. exeDir := filepath.Dir(exePath)
  64. exeDirLower := strings.ToLower(filepath.ToSlash(exeDir))
  65. if strings.Contains(exeDirLower, "/appdata/local/temp/") || strings.Contains(exeDirLower, "/go-build") {
  66. wd, err := os.Getwd()
  67. if err != nil {
  68. return "."
  69. }
  70. return wd
  71. }
  72. return exeDir
  73. }
  74. // GetDBFolderPath returns the path to the database folder based on environment variables or platform defaults.
  75. func GetDBFolderPath() string {
  76. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  77. if dbFolderPath != "" {
  78. return dbFolderPath
  79. }
  80. if runtime.GOOS == "windows" {
  81. return getBaseDir()
  82. }
  83. return "/etc/x-ui"
  84. }
  85. // GetDBPath returns the full path to the database file.
  86. func GetDBPath() string {
  87. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  88. }
  89. // GetDBKind returns the configured database backend: "sqlite" (default) or "postgres".
  90. func GetDBKind() string {
  91. v := strings.ToLower(strings.TrimSpace(os.Getenv("XUI_DB_TYPE")))
  92. switch v {
  93. case "postgres", "postgresql", "pg":
  94. return "postgres"
  95. default:
  96. return "sqlite"
  97. }
  98. }
  99. // GetDBDSN returns the PostgreSQL DSN from XUI_DB_DSN. Empty for sqlite.
  100. func GetDBDSN() string {
  101. return strings.TrimSpace(os.Getenv("XUI_DB_DSN"))
  102. }
  103. // GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
  104. func GetLogFolder() string {
  105. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  106. if logFolderPath != "" {
  107. return logFolderPath
  108. }
  109. if runtime.GOOS == "windows" {
  110. return filepath.Join(".", "log")
  111. }
  112. return "/var/log/x-ui"
  113. }
  114. func copyFile(src, dst string) error {
  115. in, err := os.Open(src)
  116. if err != nil {
  117. return err
  118. }
  119. defer in.Close()
  120. out, err := os.Create(dst)
  121. if err != nil {
  122. return err
  123. }
  124. defer out.Close()
  125. _, err = io.Copy(out, in)
  126. if err != nil {
  127. return err
  128. }
  129. return out.Sync()
  130. }
  131. func init() {
  132. if runtime.GOOS != "windows" {
  133. return
  134. }
  135. if os.Getenv("XUI_DB_FOLDER") != "" {
  136. return
  137. }
  138. oldDBFolder := "/etc/x-ui"
  139. oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
  140. newDBFolder := GetDBFolderPath()
  141. newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
  142. _, err := os.Stat(newDBPath)
  143. if err == nil {
  144. return // new exists
  145. }
  146. _, err = os.Stat(oldDBPath)
  147. if os.IsNotExist(err) {
  148. return // old does not exist
  149. }
  150. _ = copyFile(oldDBPath, newDBPath) // ignore error
  151. }