config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
  90. func GetLogFolder() string {
  91. logFolderPath := os.Getenv("XUI_LOG_FOLDER")
  92. if logFolderPath != "" {
  93. return logFolderPath
  94. }
  95. if runtime.GOOS == "windows" {
  96. return filepath.Join(".", "log")
  97. }
  98. return "/var/log"
  99. }
  100. func copyFile(src, dst string) error {
  101. in, err := os.Open(src)
  102. if err != nil {
  103. return err
  104. }
  105. defer in.Close()
  106. out, err := os.Create(dst)
  107. if err != nil {
  108. return err
  109. }
  110. defer out.Close()
  111. _, err = io.Copy(out, in)
  112. if err != nil {
  113. return err
  114. }
  115. return out.Sync()
  116. }
  117. func init() {
  118. if runtime.GOOS != "windows" {
  119. return
  120. }
  121. if os.Getenv("XUI_DB_FOLDER") != "" {
  122. return
  123. }
  124. oldDBFolder := "/etc/x-ui"
  125. oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
  126. newDBFolder := GetDBFolderPath()
  127. newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
  128. _, err := os.Stat(newDBPath)
  129. if err == nil {
  130. return // new exists
  131. }
  132. _, err = os.Stat(oldDBPath)
  133. if os.IsNotExist(err) {
  134. return // old does not exist
  135. }
  136. _ = copyFile(oldDBPath, newDBPath) // ignore error
  137. }