mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-06-07 13:44:24 +00:00
- config: add GetSettingPath for JSON-based settings storage - setting.go: load/save settings from JSON file instead of DB; keep xrayTemplateConfig in DB; fix ResetSettings to not clear users - xray_setting.go: save xray template config to DB directly - install.sh: add Cloudflare SSL option (wildcard via DNS), allow user to input custom credentials on fresh install, fix existing install logic to preserve user config
161 lines
3.6 KiB
Go
161 lines
3.6 KiB
Go
// Package config provides configuration management utilities for the 3x-ui panel,
|
|
// including version information, logging levels, database paths, and environment variable handling.
|
|
package config
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed version
|
|
var version string
|
|
|
|
//go:embed name
|
|
var name string
|
|
|
|
// LogLevel represents the logging level for the application.
|
|
type LogLevel string
|
|
|
|
// Logging level constants
|
|
const (
|
|
Debug LogLevel = "debug"
|
|
Info LogLevel = "info"
|
|
Notice LogLevel = "notice"
|
|
Warning LogLevel = "warning"
|
|
Error LogLevel = "error"
|
|
)
|
|
|
|
// GetVersion returns the version string of the 3x-ui application.
|
|
func GetVersion() string {
|
|
return strings.TrimSpace(version)
|
|
}
|
|
|
|
// GetName returns the name of the 3x-ui application.
|
|
func GetName() string {
|
|
return strings.TrimSpace(name)
|
|
}
|
|
|
|
// GetLogLevel returns the current logging level based on environment variables or defaults to Info.
|
|
func GetLogLevel() LogLevel {
|
|
if IsDebug() {
|
|
return Debug
|
|
}
|
|
logLevel := os.Getenv("XUI_LOG_LEVEL")
|
|
if logLevel == "" {
|
|
return Info
|
|
}
|
|
return LogLevel(logLevel)
|
|
}
|
|
|
|
// IsDebug returns true if debug mode is enabled via the XUI_DEBUG environment variable.
|
|
func IsDebug() bool {
|
|
return os.Getenv("XUI_DEBUG") == "true"
|
|
}
|
|
|
|
// GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
|
|
func GetBinFolderPath() string {
|
|
binFolderPath := os.Getenv("XUI_BIN_FOLDER")
|
|
if binFolderPath == "" {
|
|
binFolderPath = "bin"
|
|
}
|
|
return binFolderPath
|
|
}
|
|
|
|
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
|
|
}
|
|
return exeDir
|
|
}
|
|
|
|
// GetDBFolderPath returns the path to the database folder based on environment variables or platform defaults.
|
|
func GetDBFolderPath() string {
|
|
dbFolderPath := os.Getenv("XUI_DB_FOLDER")
|
|
if dbFolderPath != "" {
|
|
return dbFolderPath
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
return getBaseDir()
|
|
}
|
|
return "/etc/x-ui"
|
|
}
|
|
|
|
// GetDBPath returns the full path to the database file.
|
|
func GetDBPath() string {
|
|
return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
|
|
}
|
|
|
|
// GetSettingPath returns the full path to the panel settings JSON file.
|
|
func GetSettingPath() string {
|
|
return fmt.Sprintf("%s/%s.json", GetDBFolderPath(), GetName())
|
|
}
|
|
|
|
// GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
|
|
func GetLogFolder() string {
|
|
logFolderPath := os.Getenv("XUI_LOG_FOLDER")
|
|
if logFolderPath != "" {
|
|
return logFolderPath
|
|
}
|
|
if runtime.GOOS == "windows" {
|
|
return filepath.Join(".", "log")
|
|
}
|
|
return "/var/log/x-ui"
|
|
}
|
|
|
|
func copyFile(src, dst string) error {
|
|
in, err := os.Open(src)
|
|
if err != nil {
|
|
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 out.Sync()
|
|
}
|
|
|
|
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
|
|
}
|
|
_ = copyFile(oldDBPath, newDBPath) // ignore error
|
|
}
|