2025-09-20 07:35:50 +00:00
|
|
|
// Package config provides configuration management utilities for the 3x-ui panel,
|
|
|
|
|
// including version information, logging levels, database paths, and environment variable handling.
|
2023-02-09 19:18:06 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "embed"
|
2026-04-03 01:24:39 +00:00
|
|
|
"encoding/json"
|
2023-02-09 19:18:06 +00:00
|
|
|
"fmt"
|
2025-08-13 21:19:59 +00:00
|
|
|
"io"
|
2023-02-09 19:18:06 +00:00
|
|
|
"os"
|
2025-08-13 21:19:59 +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
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// LogLevel represents the logging level for the application.
|
2023-02-09 19:18:06 +00:00
|
|
|
type LogLevel string
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// Logging level constants
|
2023-02-09 19:18:06 +00:00
|
|
|
const (
|
2025-09-21 15:39:30 +00:00
|
|
|
Debug LogLevel = "debug"
|
|
|
|
|
Info LogLevel = "info"
|
|
|
|
|
Notice LogLevel = "notice"
|
|
|
|
|
Warning LogLevel = "warning"
|
|
|
|
|
Error LogLevel = "error"
|
2023-02-09 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetVersion returns the version string of the 3x-ui application.
|
2023-02-09 19:18:06 +00:00
|
|
|
func GetVersion() string {
|
|
|
|
|
return strings.TrimSpace(version)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetName returns the name of the 3x-ui application.
|
2023-02-09 19:18:06 +00:00
|
|
|
func GetName() string {
|
|
|
|
|
return strings.TrimSpace(name)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetLogLevel returns the current logging level based on environment variables or defaults to Info.
|
2023-02-09 19:18:06 +00:00
|
|
|
func GetLogLevel() LogLevel {
|
|
|
|
|
if IsDebug() {
|
|
|
|
|
return Debug
|
|
|
|
|
}
|
|
|
|
|
logLevel := os.Getenv("XUI_LOG_LEVEL")
|
|
|
|
|
if logLevel == "" {
|
|
|
|
|
return Info
|
|
|
|
|
}
|
|
|
|
|
return LogLevel(logLevel)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// IsDebug returns true if debug mode is enabled via the XUI_DEBUG environment variable.
|
2023-02-09 19:18:06 +00:00
|
|
|
func IsDebug() bool {
|
|
|
|
|
return os.Getenv("XUI_DEBUG") == "true"
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetBinFolderPath returns the path to the binary folder, defaulting to "bin" if not set via XUI_BIN_FOLDER.
|
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-13 21:19:59 +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
|
|
|
|
|
}
|
|
|
|
|
return exeDir
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetDBFolderPath returns the path to the database folder based on environment variables or platform defaults.
|
2023-04-13 19:33:46 +00:00
|
|
|
func GetDBFolderPath() string {
|
|
|
|
|
dbFolderPath := os.Getenv("XUI_DB_FOLDER")
|
2025-08-13 21:19:59 +00:00
|
|
|
if dbFolderPath != "" {
|
|
|
|
|
return dbFolderPath
|
|
|
|
|
}
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
return getBaseDir()
|
2023-04-13 19:33:46 +00:00
|
|
|
}
|
2025-08-13 21:19:59 +00:00
|
|
|
return "/etc/x-ui"
|
2023-04-13 19:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetDBPath returns the full path to the database file.
|
2023-02-09 19:18:06 +00:00
|
|
|
func GetDBPath() string {
|
2023-04-13 19:33:46 +00:00
|
|
|
return fmt.Sprintf("%s/%s.db", GetDBFolderPath(), GetName())
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
2023-07-01 12:26:43 +00:00
|
|
|
|
2026-04-02 08:16:52 +00:00
|
|
|
// GetSettingPath returns the full path to the panel settings JSON file.
|
|
|
|
|
func GetSettingPath() string {
|
|
|
|
|
return fmt.Sprintf("%s/%s.json", GetDBFolderPath(), GetName())
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetLogFolder returns the path to the log folder based on environment variables or platform defaults.
|
2023-07-01 12:26:43 +00:00
|
|
|
func GetLogFolder() string {
|
|
|
|
|
logFolderPath := os.Getenv("XUI_LOG_FOLDER")
|
2025-08-13 21:19:59 +00:00
|
|
|
if logFolderPath != "" {
|
|
|
|
|
return logFolderPath
|
|
|
|
|
}
|
|
|
|
|
if runtime.GOOS == "windows" {
|
2025-09-10 19:12:37 +00:00
|
|
|
return filepath.Join(".", "log")
|
2025-08-13 21:19:59 +00:00
|
|
|
}
|
2026-01-02 15:11:32 +00:00
|
|
|
return "/var/log/x-ui"
|
2025-08-13 21:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 06:46:24 +00:00
|
|
|
var settingGroupAliases = map[string][]string{
|
|
|
|
|
"dbType": {"databaseConnection", "other"},
|
|
|
|
|
"dbHost": {"databaseConnection", "other"},
|
|
|
|
|
"dbPort": {"databaseConnection", "other"},
|
|
|
|
|
"dbUser": {"databaseConnection", "other"},
|
|
|
|
|
"dbPassword": {"databaseConnection", "other"},
|
|
|
|
|
"dbName": {"databaseConnection", "other"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func readGroupedString(settings map[string]any, key string) string {
|
|
|
|
|
if groups, ok := settingGroupAliases[key]; ok {
|
|
|
|
|
for _, groupName := range groups {
|
|
|
|
|
if group, ok := settings[groupName].(map[string]any); ok {
|
|
|
|
|
if value, ok := group[key].(string); ok && value != "" {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if value, ok := settings[key].(string); ok && value != "" {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func settingsLayoutMeta() map[string]any {
|
|
|
|
|
return map[string]any{
|
|
|
|
|
"layout": "按模块-用途来归类",
|
|
|
|
|
"schema": "module-purpose-v1",
|
|
|
|
|
"description": "Top-level groups are organized by module and purpose for easier maintenance and development.",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-13 21:19:59 +00:00
|
|
|
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
|
2023-07-01 12:26:43 +00:00
|
|
|
}
|
2025-08-13 21:19:59 +00:00
|
|
|
_ = copyFile(oldDBPath, newDBPath) // ignore error
|
2023-07-01 12:26:43 +00:00
|
|
|
}
|
2026-04-03 01:24:39 +00:00
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 06:46:24 +00:00
|
|
|
if dbType := readGroupedString(settings, "dbType"); dbType != "" {
|
2026-04-03 01:24:39 +00:00
|
|
|
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 {
|
2026-04-03 01:53:20 +00:00
|
|
|
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"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbType := "sqlite"
|
2026-04-04 06:46:24 +00:00
|
|
|
if t := readGroupedString(settings, "dbType"); t != "" {
|
2026-04-03 01:53:20 +00:00
|
|
|
dbType = t
|
2026-04-03 01:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DBConfig{
|
2026-04-03 01:53:20 +00:00
|
|
|
Type: dbType,
|
2026-04-04 06:46:24 +00:00
|
|
|
Host: readGroupedString(settings, "dbHost"),
|
|
|
|
|
Port: readGroupedString(settings, "dbPort"),
|
|
|
|
|
User: readGroupedString(settings, "dbUser"),
|
|
|
|
|
Password: readGroupedString(settings, "dbPassword"),
|
|
|
|
|
Name: readGroupedString(settings, "dbName"),
|
2026-04-03 01:24:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
2026-04-04 06:46:24 +00:00
|
|
|
if _, exists := settings["_meta"]; !exists {
|
|
|
|
|
settings["_meta"] = settingsLayoutMeta()
|
2026-04-03 01:24:39 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 06:46:24 +00:00
|
|
|
// Check if the key lives in a nested group
|
|
|
|
|
if groups, ok := settingGroupAliases[key]; ok && len(groups) > 0 {
|
|
|
|
|
group := groups[0]
|
2026-04-03 01:24:39 +00:00
|
|
|
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)
|
|
|
|
|
}
|