diff --git a/.gitignore b/.gitignore index a7644d78..8fa4eeb0 100644 --- a/.gitignore +++ b/.gitignore @@ -29,9 +29,9 @@ main .DS_Store Thumbs.db -# Ignore Go specific files +# Ignore Go build files *.exe -*.exe~ +x-ui.db # Ignore Docker specific files docker-compose.override.yml diff --git a/config/config.go b/config/config.go index 832f57e5..32d83055 100644 --- a/config/config.go +++ b/config/config.go @@ -3,13 +3,11 @@ package config import ( _ "embed" "fmt" + "io" "os" "path/filepath" "runtime" "strings" - "x-ui/logger" - - "github.com/otiai10/copy" ) //go:embed version @@ -18,9 +16,6 @@ var version string //go:embed name var name string -// default folder for database -var defaultDbFolder = "/etc/x-ui" - type LogLevel string const ( @@ -62,55 +57,88 @@ func GetBinFolderPath() string { return binFolderPath } -func GetDBPath() string { - return fmt.Sprintf("%s/%s.db", getDBFolderPath(), GetName()) -} - -func GetLogFolder() string { - logFolderPath := os.Getenv("XUI_LOG_FOLDER") - if logFolderPath == "" { - logFolderPath = "/var/log" +func getBaseDir() string { + exePath, err := os.Executable() + if err != nil { + return "." } - return logFolderPath + 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 + } + return exeDir } -func getDBFolderPath() string { +func GetDBFolderPath() string { dbFolderPath := os.Getenv("XUI_DB_FOLDER") if dbFolderPath != "" { return dbFolderPath } - if runtime.GOOS == "windows" { - return getWindowsDbPath() - } else { - return defaultDbFolder + return getBaseDir() } + return "/etc/x-ui" } -func getWindowsDbPath() string { - homeDir := os.Getenv("LOCALAPPDATA") - if homeDir == "" { - logger.Errorf("Error while getting local app data folder, falling back to %s", defaultDbFolder) - return defaultDbFolder - } +func GetDBPath() string { + return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName()) +} - userFolder := filepath.Join(homeDir, "x-ui") - err := moveExistingDb(defaultDbFolder, userFolder) +func GetLogFolder() string { + logFolderPath := os.Getenv("XUI_LOG_FOLDER") + if logFolderPath != "" { + return logFolderPath + } + if runtime.GOOS == "windows" { + return getBaseDir() + } + return "/var/log" +} + +func copyFile(src, dst string) error { + in, err := os.Open(src) if err != nil { - logger.Error("Error while moving existing DB: %w, falling back to %s", err, defaultDbFolder) - return defaultDbFolder + return err + } + defer in.Close() + + out, err := os.Create(dst) + if err != nil { + return err + } + defer out.Close() + + _, err = io.Copy(out, in) + if err != nil { + return err } - return userFolder + return out.Sync() } -func moveExistingDb(from string, to string) error { - if _, err := os.Stat(to); os.IsNotExist(err) { - if _, err := os.Stat(from); !os.IsNotExist(err) { - if err := copy.Copy(from, to); err != nil { - return fmt.Errorf("failed to copy %s to %s: %w", from, to, err) - } - } +func init() { + if runtime.GOOS != "windows" { + return } - return nil + 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 + } + _ = copyFile(oldDBPath, newDBPath) // ignore error } diff --git a/go.mod b/go.mod index af046e6c..771f1070 100644 --- a/go.mod +++ b/go.mod @@ -61,8 +61,6 @@ require ( github.com/miekg/dns v1.1.68 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect - github.com/otiai10/mint v1.6.3 // indirect - github.com/otiai10/copy v1.14.1 github.com/pires/go-proxyproto v0.8.1 // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/quic-go/qpack v0.5.1 // indirect diff --git a/go.sum b/go.sum index d7b8da45..74eee2b8 100644 --- a/go.sum +++ b/go.sum @@ -116,10 +116,6 @@ github.com/nicksnyder/go-i18n/v2 v2.6.0 h1:C/m2NNWNiTB6SK4Ao8df5EWm3JETSTIGNXBpM github.com/nicksnyder/go-i18n/v2 v2.6.0/go.mod h1:88sRqr0C6OPyJn0/KRNaEz1uWorjxIKP7rUUcvycecE= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88= github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= -github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8= -github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I= -github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs= -github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=