config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // IsSkipHSTS returns true if skipping HSTS mode is enabled via the XUI_SKIP_HSTS environment variable.
  51. func IsSkipHSTS() bool {
  52. return os.Getenv("XUI_SKIP_HSTS") == "true"
  53. }
  54. // GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
  55. func GetBinFolderPath() string {
  56. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  57. if binFolderPath == "" {
  58. binFolderPath = "bin"
  59. }
  60. return binFolderPath
  61. }
  62. func getBaseDir() string {
  63. exePath, err := os.Executable()
  64. if err != nil {
  65. return "."
  66. }
  67. exeDir := filepath.Dir(exePath)
  68. exeDirLower := strings.ToLower(filepath.ToSlash(exeDir))
  69. if strings.Contains(exeDirLower, "/appdata/local/temp/") || strings.Contains(exeDirLower, "/go-build") {
  70. wd, err := os.Getwd()
  71. if err != nil {
  72. return "."
  73. }
  74. return wd
  75. }
  76. return exeDir
  77. }
  78. // GetDBFolderPath returns the path to the database folder based on environment variables or platform defaults.
  79. func GetDBFolderPath() string {
  80. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  81. if dbFolderPath != "" {
  82. return dbFolderPath
  83. }
  84. if runtime.GOOS == "windows" {
  85. return getBaseDir()
  86. }
  87. return "/etc/x-ui"
  88. }
  89. // GetDBPath returns the full path to the database file.
  90. func GetDBPath() string {
  91. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  92. }
  93. // GetDBKind returns the configured database backend: "sqlite" (default) or "postgres".
  94. func GetDBKind() string {
  95. v := strings.ToLower(strings.TrimSpace(os.Getenv("XUI_DB_TYPE")))
  96. switch v {
  97. case "postgres", "postgresql", "pg":
  98. return "postgres"
  99. default:
  100. return "sqlite"
  101. }
  102. }
  103. // GetDBDSN returns the PostgreSQL DSN from XUI_DB_DSN. Empty for sqlite.
  104. func GetDBDSN() string {
  105. return strings.TrimSpace(os.Getenv("XUI_DB_DSN"))
  106. }
  107. // GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
  108. func GetLogFolder() string {
  109. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  110. if logFolderPath != "" {
  111. return logFolderPath
  112. }
  113. if runtime.GOOS == "windows" {
  114. return filepath.Join(".", "log")
  115. }
  116. return "/var/log/x-ui"
  117. }
  118. func copyFile(src, dst string) error {
  119. in, err := os.Open(src)
  120. if err != nil {
  121. return err
  122. }
  123. defer in.Close()
  124. out, err := os.Create(dst)
  125. if err != nil {
  126. return err
  127. }
  128. defer out.Close()
  129. _, err = io.Copy(out, in)
  130. if err != nil {
  131. return err
  132. }
  133. return out.Sync()
  134. }
  135. func init() {
  136. if runtime.GOOS != "windows" {
  137. return
  138. }
  139. if os.Getenv("XUI_DB_FOLDER") != "" {
  140. return
  141. }
  142. oldDBFolder := "/etc/x-ui"
  143. oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
  144. newDBFolder := GetDBFolderPath()
  145. newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
  146. _, err := os.Stat(newDBPath)
  147. if err == nil {
  148. return // new exists
  149. }
  150. _, err = os.Stat(oldDBPath)
  151. if os.IsNotExist(err) {
  152. return // old does not exist
  153. }
  154. _ = copyFile(oldDBPath, newDBPath) // ignore error
  155. }