2023-02-09 19:18:06 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2025-08-11 19:15:43 +00:00
|
|
|
"io"
|
2023-02-09 19:18:06 +00:00
|
|
|
"os"
|
2025-08-09 11:39:50 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2023-02-09 19:18:06 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed version
|
|
|
|
var version string
|
|
|
|
|
|
|
|
//go:embed name
|
|
|
|
var name string
|
|
|
|
|
|
|
|
type LogLevel string
|
|
|
|
|
|
|
|
const (
|
2023-06-16 14:55:33 +00:00
|
|
|
Debug LogLevel = "debug"
|
|
|
|
Info LogLevel = "info"
|
|
|
|
Notice LogLevel = "notice"
|
|
|
|
Warn LogLevel = "warn"
|
|
|
|
Error LogLevel = "error"
|
2023-02-09 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetVersion() string {
|
|
|
|
return strings.TrimSpace(version)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetName() string {
|
|
|
|
return strings.TrimSpace(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLogLevel() LogLevel {
|
|
|
|
if IsDebug() {
|
|
|
|
return Debug
|
|
|
|
}
|
|
|
|
logLevel := os.Getenv("XUI_LOG_LEVEL")
|
|
|
|
if logLevel == "" {
|
|
|
|
return Info
|
|
|
|
}
|
|
|
|
return LogLevel(logLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsDebug() bool {
|
|
|
|
return os.Getenv("XUI_DEBUG") == "true"
|
|
|
|
}
|
|
|
|
|
2023-04-13 19:33:46 +00:00
|
|
|
func GetBinFolderPath() string {
|
|
|
|
binFolderPath := os.Getenv("XUI_BIN_FOLDER")
|
|
|
|
if binFolderPath == "" {
|
|
|
|
binFolderPath = "bin"
|
|
|
|
}
|
|
|
|
return binFolderPath
|
|
|
|
}
|
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
func getBaseDir() string {
|
|
|
|
exePath, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "."
|
|
|
|
}
|
|
|
|
exeDir := filepath.Dir(exePath)
|
|
|
|
exeDirLower := strings.ToLower(filepath.ToSlash(exeDir))
|
|
|
|
if strings.Contains(exeDirLower, "/appdata/local/temp/") || strings.Contains(exeDirLower, "/go-build") {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return "."
|
|
|
|
}
|
|
|
|
return wd
|
2025-08-09 12:37:18 +00:00
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
return exeDir
|
2025-08-09 12:37:18 +00:00
|
|
|
}
|
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
func GetDBFolderPath() string {
|
2023-04-13 19:33:46 +00:00
|
|
|
dbFolderPath := os.Getenv("XUI_DB_FOLDER")
|
2025-08-09 11:39:50 +00:00
|
|
|
if dbFolderPath != "" {
|
|
|
|
return dbFolderPath
|
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return getBaseDir()
|
|
|
|
}
|
|
|
|
return "/etc/x-ui"
|
|
|
|
}
|
2025-08-09 11:39:50 +00:00
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
func GetDBPath() string {
|
|
|
|
return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLogFolder() string {
|
|
|
|
logFolderPath := os.Getenv("XUI_LOG_FOLDER")
|
|
|
|
if logFolderPath != "" {
|
|
|
|
return logFolderPath
|
|
|
|
}
|
2025-08-09 11:39:50 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2025-08-11 19:15:43 +00:00
|
|
|
return getBaseDir()
|
2025-08-09 16:27:33 +00:00
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
return "/var/log"
|
2025-08-09 16:27:33 +00:00
|
|
|
}
|
2025-08-09 11:39:50 +00:00
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
func copyFile(src, dst string) error {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2025-08-09 16:27:33 +00:00
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
defer in.Close()
|
2025-08-09 11:39:50 +00:00
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
out, err := os.Create(dst)
|
2025-08-09 16:27:33 +00:00
|
|
|
if err != nil {
|
2025-08-11 19:15:43 +00:00
|
|
|
return err
|
2023-04-13 19:33:46 +00:00
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
defer out.Close()
|
2025-08-09 16:27:33 +00:00
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Sync()
|
2023-04-13 19:33:46 +00:00
|
|
|
}
|
|
|
|
|
2025-08-11 19:15:43 +00:00
|
|
|
func init() {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if os.Getenv("XUI_DB_FOLDER") != "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oldDBFolder := "/etc/x-ui"
|
|
|
|
oldDBPath := fmt.Sprintf("%s/%s.db", oldDBFolder, GetName())
|
|
|
|
newDBFolder := GetDBFolderPath()
|
|
|
|
newDBPath := fmt.Sprintf("%s/%s.db", newDBFolder, GetName())
|
|
|
|
_, err := os.Stat(newDBPath)
|
|
|
|
if err == nil {
|
|
|
|
return // new exists
|
|
|
|
}
|
|
|
|
_, err = os.Stat(oldDBPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return // old does not exist
|
2025-08-09 11:39:50 +00:00
|
|
|
}
|
2025-08-11 19:15:43 +00:00
|
|
|
_ = copyFile(oldDBPath, newDBPath) // ignore error
|
2025-08-09 11:39:50 +00:00
|
|
|
}
|