3x-ui/config/config.go
Sora39831 c94372a22c fix: address all code review issues for MariaDB support
- Prevent DBPassword from leaking to frontend (json:"-")
- Make migration direction explicit via --direction flag, set dbType only after success
- Use driver-appropriate DROP INDEX IF EXISTS for SQLite vs MariaDB
- Build DSN with mysql.Config.FormatDSN() to prevent injection with special chars
- Close DB before re-initialization in InitDB
- Add migration tests (5 tests using SQLite in-memory DBs)
- Parse JSON once in GetDBConfigFromJSON instead of 7 times
- Use Go binary for dbType in shell script instead of fragile grep
- Add rollback on failure in db_switch_to_sqlite
- Validate DB settings in CheckValid
2026-04-03 09:53:20 +08:00

280 lines
6.8 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"
"encoding/json"
"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
}
// GetDBTypeFromJSON reads the dbType setting directly from the JSON config file.
// This is needed before the database is initialized. Falls back to "sqlite".
func GetDBTypeFromJSON() string {
data, err := os.ReadFile(GetSettingPath())
if err != nil {
return "sqlite"
}
var settings map[string]any
if err := json.Unmarshal(data, &settings); err != nil {
return "sqlite"
}
// Check nested format: "other" group contains "dbType"
if other, ok := settings["other"].(map[string]any); ok {
if dbType, ok := other["dbType"].(string); ok && dbType != "" {
return dbType
}
}
// Check flat format: top-level "dbType"
if dbType, ok := settings["dbType"].(string); ok && dbType != "" {
return dbType
}
return "sqlite"
}
// DBConfig holds MariaDB connection settings read from the JSON config file.
type DBConfig struct {
Type string
Host string
Port string
User string
Password string
Name string
}
// GetDBConfigFromJSON reads all MariaDB connection settings from the JSON config file.
func GetDBConfigFromJSON() DBConfig {
data, err := os.ReadFile(GetSettingPath())
if err != nil {
return DBConfig{Type: "sqlite", Host: "127.0.0.1", Port: "3306", Name: "3xui"}
}
var settings map[string]any
if err := json.Unmarshal(data, &settings); err != nil {
return DBConfig{Type: "sqlite", Host: "127.0.0.1", Port: "3306", Name: "3xui"}
}
// readString extracts a value from either nested (group.key) or flat format
readString := func(nestedGroup, flatKey string) string {
if group, ok := settings[nestedGroup].(map[string]any); ok {
if v, ok := group[flatKey].(string); ok {
return v
}
}
if v, ok := settings[flatKey].(string); ok {
return v
}
return ""
}
// Read dbType from the same parsed settings
dbType := "sqlite"
if other, ok := settings["other"].(map[string]any); ok {
if t, ok := other["dbType"].(string); ok && t != "" {
dbType = t
}
} else if t, ok := settings["dbType"].(string); ok && t != "" {
dbType = t
}
return DBConfig{
Type: dbType,
Host: readString("other", "dbHost"),
Port: readString("other", "dbPort"),
User: readString("other", "dbUser"),
Password: readString("other", "dbPassword"),
Name: readString("other", "dbName"),
}
}
// WriteSettingToJSON writes a single setting key to the JSON config file.
// It reads the existing file, updates the value, and writes back.
func WriteSettingToJSON(key, value string) error {
path := GetSettingPath()
data, err := os.ReadFile(path)
if err != nil {
return err
}
var settings map[string]any
if err := json.Unmarshal(data, &settings); err != nil {
return err
}
// Check if the key lives in a nested group
groupMap := map[string]string{
"dbType": "other", "dbHost": "other", "dbPort": "other",
"dbUser": "other", "dbPassword": "other", "dbName": "other",
}
if group, ok := groupMap[key]; ok {
if _, exists := settings[group]; !exists {
settings[group] = make(map[string]any)
}
settings[group].(map[string]any)[key] = value
} else {
settings[key] = value
}
out, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, out, 0644)
}