This commit is contained in:
mhsanaei 2025-08-11 21:15:43 +02:00
parent 1d81151327
commit 07242779ef
No known key found for this signature in database
GPG key ID: D875CD086CF668A0
4 changed files with 69 additions and 47 deletions

4
.gitignore vendored
View file

@ -29,9 +29,9 @@ main
.DS_Store .DS_Store
Thumbs.db Thumbs.db
# Ignore Go specific files # Ignore Go build files
*.exe *.exe
*.exe~ x-ui.db
# Ignore Docker specific files # Ignore Docker specific files
docker-compose.override.yml docker-compose.override.yml

View file

@ -3,13 +3,11 @@ package config
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"x-ui/logger"
"github.com/otiai10/copy"
) )
//go:embed version //go:embed version
@ -18,9 +16,6 @@ var version string
//go:embed name //go:embed name
var name string var name string
// default folder for database
var defaultDbFolder = "/etc/x-ui"
type LogLevel string type LogLevel string
const ( const (
@ -62,55 +57,88 @@ func GetBinFolderPath() string {
return binFolderPath return binFolderPath
} }
func GetDBPath() string { func getBaseDir() string {
return fmt.Sprintf("%s/%s.db", getDBFolderPath(), GetName()) exePath, err := os.Executable()
} if err != nil {
return "."
func GetLogFolder() string {
logFolderPath := os.Getenv("XUI_LOG_FOLDER")
if logFolderPath == "" {
logFolderPath = "/var/log"
} }
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") dbFolderPath := os.Getenv("XUI_DB_FOLDER")
if dbFolderPath != "" { if dbFolderPath != "" {
return dbFolderPath return dbFolderPath
} }
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return getWindowsDbPath() return getBaseDir()
} else {
return defaultDbFolder
} }
return "/etc/x-ui"
} }
func getWindowsDbPath() string { func GetDBPath() string {
homeDir := os.Getenv("LOCALAPPDATA") return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
if homeDir == "" { }
logger.Errorf("Error while getting local app data folder, falling back to %s", defaultDbFolder)
return defaultDbFolder
}
userFolder := filepath.Join(homeDir, "x-ui") func GetLogFolder() string {
err := moveExistingDb(defaultDbFolder, userFolder) 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 { if err != nil {
logger.Error("Error while moving existing DB: %w, falling back to %s", err, defaultDbFolder) return err
return defaultDbFolder }
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 { func init() {
if _, err := os.Stat(to); os.IsNotExist(err) { if runtime.GOOS != "windows" {
if _, err := os.Stat(from); !os.IsNotExist(err) { return
if err := copy.Copy(from, to); err != nil {
return fmt.Errorf("failed to copy %s to %s: %w", from, to, err)
}
}
} }
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
} }

2
go.mod
View file

@ -61,8 +61,6 @@ require (
github.com/miekg/dns v1.1.68 // indirect github.com/miekg/dns v1.1.68 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // 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/pires/go-proxyproto v0.8.1 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/quic-go/qpack v0.5.1 // indirect github.com/quic-go/qpack v0.5.1 // indirect

4
go.sum
View file

@ -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/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 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= 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 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= 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= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=