1
0

config.go 6.1 KB

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