config.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "strconv"
  12. "strings"
  13. "testing"
  14. )
  15. //go:embed version
  16. var version string
  17. //go:embed name
  18. var name string
  19. // LogLevel represents the logging level for the application.
  20. type LogLevel string
  21. // Logging level constants
  22. const (
  23. Debug LogLevel = "debug"
  24. Info LogLevel = "info"
  25. Notice LogLevel = "notice"
  26. Warning LogLevel = "warning"
  27. Error LogLevel = "error"
  28. )
  29. // GetVersion returns the version string of the 3x-ui application.
  30. func GetVersion() string {
  31. return strings.TrimSpace(version)
  32. }
  33. // GetName returns the name of the 3x-ui application.
  34. func GetName() string {
  35. return strings.TrimSpace(name)
  36. }
  37. // GetLogLevel returns the current logging level based on environment variables or defaults to Info.
  38. func GetLogLevel() LogLevel {
  39. if IsDebug() {
  40. return Debug
  41. }
  42. logLevel := os.Getenv("XUI_LOG_LEVEL")
  43. if logLevel == "" {
  44. return Info
  45. }
  46. return LogLevel(logLevel)
  47. }
  48. // IsDebug returns true if debug mode is enabled via the XUI_DEBUG environment variable.
  49. func IsDebug() bool {
  50. return os.Getenv("XUI_DEBUG") == "true"
  51. }
  52. // IsSkipHSTS returns true if skipping HSTS mode is enabled via the XUI_SKIP_HSTS environment variable.
  53. func IsSkipHSTS() bool {
  54. return os.Getenv("XUI_SKIP_HSTS") == "true"
  55. }
  56. func GetPortOverride() (port int, configured bool, err error) {
  57. value, ok := os.LookupEnv("XUI_PORT")
  58. if !ok || strings.TrimSpace(value) == "" {
  59. return 0, false, nil
  60. }
  61. port, err = strconv.Atoi(strings.TrimSpace(value))
  62. if err != nil {
  63. return 0, true, fmt.Errorf("parse XUI_PORT: %w", err)
  64. }
  65. if port < 1 || port > 65535 {
  66. return 0, true, fmt.Errorf("XUI_PORT must be between 1 and 65535")
  67. }
  68. return port, true, nil
  69. }
  70. // GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
  71. func GetBinFolderPath() string {
  72. binFolderPath := os.Getenv("XUI_BIN_FOLDER")
  73. if binFolderPath == "" {
  74. binFolderPath = "bin"
  75. }
  76. return binFolderPath
  77. }
  78. func getBaseDir() string {
  79. exePath, err := os.Executable()
  80. if err != nil {
  81. return "."
  82. }
  83. exeDir := filepath.Dir(exePath)
  84. exeDirLower := strings.ToLower(filepath.ToSlash(exeDir))
  85. if strings.Contains(exeDirLower, "/appdata/local/temp/") || strings.Contains(exeDirLower, "/go-build") {
  86. wd, err := os.Getwd()
  87. if err != nil {
  88. return "."
  89. }
  90. return wd
  91. }
  92. return exeDir
  93. }
  94. // GetDBFolderPath returns the path to the database folder based on environment variables or platform defaults.
  95. func GetDBFolderPath() string {
  96. dbFolderPath := os.Getenv("XUI_DB_FOLDER")
  97. if dbFolderPath != "" {
  98. return dbFolderPath
  99. }
  100. if runtime.GOOS == "windows" {
  101. return getBaseDir()
  102. }
  103. return "/etc/x-ui"
  104. }
  105. // GetDBPath returns the full path to the database file.
  106. func GetDBPath() string {
  107. return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
  108. }
  109. // GetDBKind returns the configured database backend: "sqlite" (default) or "postgres".
  110. func GetDBKind() string {
  111. v := strings.ToLower(strings.TrimSpace(os.Getenv("XUI_DB_TYPE")))
  112. switch v {
  113. case "postgres", "postgresql", "pg":
  114. return "postgres"
  115. default:
  116. return "sqlite"
  117. }
  118. }
  119. // GetDBDSN returns the PostgreSQL DSN from XUI_DB_DSN. Empty for sqlite.
  120. func GetDBDSN() string {
  121. return strings.TrimSpace(os.Getenv("XUI_DB_DSN"))
  122. }
  123. // GetEnvFilePaths returns the candidate service environment file paths (the file
  124. // systemd loads via EnvironmentFile) across the supported distro families.
  125. func GetEnvFilePaths() []string {
  126. if runtime.GOOS == "windows" {
  127. return nil
  128. }
  129. return []string{
  130. "/etc/default/x-ui",
  131. "/etc/conf.d/x-ui",
  132. "/etc/sysconfig/x-ui",
  133. }
  134. }
  135. // GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
  136. func GetLogFolder() string {
  137. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  138. if logFolderPath != "" {
  139. return logFolderPath
  140. }
  141. // Under `go test` the Windows default below is CWD-relative ("./log"), which
  142. // scatters a log/ directory through the source tree (one per tested package).
  143. // Redirect test runs to a shared temp folder so the source tree stays clean.
  144. if testing.Testing() {
  145. return filepath.Join(os.TempDir(), "3x-ui-test-log")
  146. }
  147. if runtime.GOOS == "windows" {
  148. return filepath.Join(".", "log")
  149. }
  150. return "/var/log/x-ui"
  151. }
  152. func copyFile(src, dst string) error {
  153. in, err := os.Open(src)
  154. if err != nil {
  155. return err
  156. }
  157. defer in.Close()
  158. out, err := os.Create(dst)
  159. if err != nil {
  160. return err
  161. }
  162. defer out.Close()
  163. _, err = io.Copy(out, in)
  164. if err != nil {
  165. return err
  166. }
  167. return out.Sync()
  168. }
  169. func init() {
  170. if runtime.GOOS != "windows" {
  171. return
  172. }
  173. if os.Getenv("XUI_DB_FOLDER") != "" {
  174. return
  175. }
  176. oldDBFolder := "/etc/x-ui"
  177. oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
  178. newDBFolder := GetDBFolderPath()
  179. newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
  180. _, err := os.Stat(newDBPath)
  181. if err == nil {
  182. return // new exists
  183. }
  184. _, err = os.Stat(oldDBPath)
  185. if os.IsNotExist(err) {
  186. return // old does not exist
  187. }
  188. _ = copyFile(oldDBPath, newDBPath) // ignore error
  189. }