This commit is contained in:
wjqserver 2025-08-11 18:28:03 +08:00
parent 8dca51b897
commit d389a61f09
10 changed files with 273 additions and 155 deletions

View file

@ -1,25 +1,31 @@
package config
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/WJQSERVER/wanf"
)
// Config 结构体定义了整个应用程序的配置
type Config struct {
Server ServerConfig `toml:"server"`
Httpc HttpcConfig `toml:"httpc"`
GitClone GitCloneConfig `toml:"gitclone"`
Shell ShellConfig `toml:"shell"`
Pages PagesConfig `toml:"pages"`
Log LogConfig `toml:"log"`
Auth AuthConfig `toml:"auth"`
Blacklist BlacklistConfig `toml:"blacklist"`
Whitelist WhitelistConfig `toml:"whitelist"`
IPFilter IPFilterConfig `toml:"ipFilter"`
RateLimit RateLimitConfig `toml:"rateLimit"`
Outbound OutboundConfig `toml:"outbound"`
Docker DockerConfig `toml:"docker"`
Server ServerConfig `toml:"server" wanf:"server"`
Httpc HttpcConfig `toml:"httpc" wanf:"httpc"`
GitClone GitCloneConfig `toml:"gitclone" wanf:"gitclone"`
Shell ShellConfig `toml:"shell" wanf:"shell"`
Pages PagesConfig `toml:"pages" wanf:"pages"`
Log LogConfig `toml:"log" wanf:"log"`
Auth AuthConfig `toml:"auth" wanf:"auth"`
Blacklist BlacklistConfig `toml:"blacklist" wanf:"blacklist"`
Whitelist WhitelistConfig `toml:"whitelist" wanf:"whitelist"`
IPFilter IPFilterConfig `toml:"ipFilter" wanf:"ipFilter"`
RateLimit RateLimitConfig `toml:"rateLimit" wanf:"rateLimit"`
Outbound OutboundConfig `toml:"outbound" wanf:"outbound"`
Docker DockerConfig `toml:"docker" wanf:"docker"`
}
/*
@ -32,13 +38,14 @@ cors = "*" # "*"/"" -> "*" ; "nil" -> "" ;
debug = false
*/
// ServerConfig 定义服务器相关的配置
type ServerConfig struct {
Port int `toml:"port"`
Host string `toml:"host"`
SizeLimit int `toml:"sizeLimit"`
MemLimit int64 `toml:"memLimit"`
Cors string `toml:"cors"`
Debug bool `toml:"debug"`
Port int `toml:"port" wanf:"port"`
Host string `toml:"host" wanf:"host"`
SizeLimit int `toml:"sizeLimit" wanf:"sizeLimit"`
MemLimit int64 `toml:"memLimit" wanf:"memLimit"`
Cors string `toml:"cors" wanf:"cors"`
Debug bool `toml:"debug" wanf:"debug"`
}
/*
@ -49,12 +56,13 @@ maxIdleConnsPerHost = 60 # only for advanced mode
maxConnsPerHost = 0 # only for advanced mode
useCustomRawHeaders = false
*/
// HttpcConfig 定义 HTTP 客户端相关的配置
type HttpcConfig struct {
Mode string `toml:"mode"`
MaxIdleConns int `toml:"maxIdleConns"`
MaxIdleConnsPerHost int `toml:"maxIdleConnsPerHost"`
MaxConnsPerHost int `toml:"maxConnsPerHost"`
UseCustomRawHeaders bool `toml:"useCustomRawHeaders"`
Mode string `toml:"mode" wanf:"mode"`
MaxIdleConns int `toml:"maxIdleConns" wanf:"maxIdleConns"`
MaxIdleConnsPerHost int `toml:"maxIdleConnsPerHost" wanf:"maxIdleConnsPerHost"`
MaxConnsPerHost int `toml:"maxConnsPerHost" wanf:"maxConnsPerHost"`
UseCustomRawHeaders bool `toml:"useCustomRawHeaders" wanf:"useCustomRawHeaders"`
}
/*
@ -64,11 +72,12 @@ smartGitAddr = "http://127.0.0.1:8080"
//cacheTimeout = 10
ForceH2C = true
*/
// GitCloneConfig 定义 Git 克隆相关的配置
type GitCloneConfig struct {
Mode string `toml:"mode"`
SmartGitAddr string `toml:"smartGitAddr"`
Mode string `toml:"mode" wanf:"mode"`
SmartGitAddr string `toml:"smartGitAddr" wanf:"smartGitAddr"`
//CacheTimeout int `toml:"cacheTimeout"`
ForceH2C bool `toml:"ForceH2C"`
ForceH2C bool `toml:"ForceH2C" wanf:"ForceH2C"`
}
/*
@ -76,9 +85,10 @@ type GitCloneConfig struct {
editor = true
rewriteAPI = false
*/
// ShellConfig 定义 Shell 相关的配置
type ShellConfig struct {
Editor bool `toml:"editor"`
RewriteAPI bool `toml:"rewriteAPI"`
Editor bool `toml:"editor" wanf:"editor"`
RewriteAPI bool `toml:"rewriteAPI" wanf:"rewriteAPI"`
}
/*
@ -87,16 +97,18 @@ mode = "internal" # "internal" or "external"
theme = "bootstrap" # "bootstrap" or "nebula"
staticDir = "/data/www"
*/
// PagesConfig 定义静态页面相关的配置
type PagesConfig struct {
Mode string `toml:"mode"`
Theme string `toml:"theme"`
StaticDir string `toml:"staticDir"`
Mode string `toml:"mode" wanf:"mode"`
Theme string `toml:"theme" wanf:"theme"`
StaticDir string `toml:"staticDir" wanf:"staticDir"`
}
// LogConfig 定义日志相关的配置
type LogConfig struct {
LogFilePath string `toml:"logFilePath"`
MaxLogSize int64 `toml:"maxLogSize"`
Level string `toml:"level"`
LogFilePath string `toml:"logFilePath" wanf:"logFilePath"`
MaxLogSize int64 `toml:"maxLogSize" wanf:"maxLogSize"`
Level string `toml:"level" wanf:"level"`
}
/*
@ -109,31 +121,35 @@ passThrough = false
ForceAllowApi = false
ForceAllowApiPassList = false
*/
// AuthConfig 定义认证相关的配置
type AuthConfig struct {
Enabled bool `toml:"enabled"`
Method string `toml:"method"`
Key string `toml:"key"`
Token string `toml:"token"`
PassThrough bool `toml:"passThrough"`
ForceAllowApi bool `toml:"ForceAllowApi"`
ForceAllowApiPassList bool `toml:"ForceAllowApiPassList"`
Enabled bool `toml:"enabled" wanf:"enabled"`
Method string `toml:"method" wanf:"method"`
Key string `toml:"key" wanf:"key"`
Token string `toml:"token" wanf:"token"`
PassThrough bool `toml:"passThrough" wanf:"passThrough"`
ForceAllowApi bool `toml:"ForceAllowApi" wanf:"ForceAllowApi"`
ForceAllowApiPassList bool `toml:"ForceAllowApiPassList" wanf:"ForceAllowApiPassList"`
}
// BlacklistConfig 定义黑名单相关的配置
type BlacklistConfig struct {
Enabled bool `toml:"enabled"`
BlacklistFile string `toml:"blacklistFile"`
Enabled bool `toml:"enabled" wanf:"enabled"`
BlacklistFile string `toml:"blacklistFile" wanf:"blacklistFile"`
}
// WhitelistConfig 定义白名单相关的配置
type WhitelistConfig struct {
Enabled bool `toml:"enabled"`
WhitelistFile string `toml:"whitelistFile"`
Enabled bool `toml:"enabled" wanf:"enabled"`
WhitelistFile string `toml:"whitelistFile" wanf:"whitelistFile"`
}
// IPFilterConfig 定义 IP 过滤相关的配置
type IPFilterConfig struct {
Enabled bool `toml:"enabled"`
EnableAllowList bool `toml:"enableAllowList"`
EnableBlockList bool `toml:"enableBlockList"`
IPFilterFile string `toml:"ipFilterFile"`
Enabled bool `toml:"enabled" wanf:"enabled"`
EnableAllowList bool `toml:"enableAllowList" wanf:"enableAllowList"`
EnableBlockList bool `toml:"enableBlockList" wanf:"enableBlockList"`
IPFilterFile string `toml:"ipFilterFile" wanf:"ipFilterFile"`
}
/*
@ -150,19 +166,21 @@ burst = 10
singleBurst = "10mbps"
*/
// RateLimitConfig 定义限速相关的配置
type RateLimitConfig struct {
Enabled bool `toml:"enabled"`
RatePerMinute int `toml:"ratePerMinute"`
Burst int `toml:"burst"`
BandwidthLimit BandwidthLimitConfig
Enabled bool `toml:"enabled" wanf:"enabled"`
RatePerMinute int `toml:"ratePerMinute" wanf:"ratePerMinute"`
Burst int `toml:"burst" wanf:"burst"`
BandwidthLimit BandwidthLimitConfig `toml:"bandwidthLimit" wanf:"bandwidthLimit"`
}
// BandwidthLimitConfig 定义带宽限制相关的配置
type BandwidthLimitConfig struct {
Enabled bool `toml:"enabled"`
TotalLimit string `toml:"totalLimit"`
TotalBurst string `toml:"totalBurst"`
SingleLimit string `toml:"singleLimit"`
SingleBurst string `toml:"singleBurst"`
Enabled bool `toml:"enabled" wanf:"enabled"`
TotalLimit string `toml:"totalLimit" wanf:"totalLimit"`
TotalBurst string `toml:"totalBurst" wanf:"totalBurst"`
SingleLimit string `toml:"singleLimit" wanf:"singleLimit"`
SingleBurst string `toml:"singleBurst" wanf:"singleBurst"`
}
/*
@ -170,9 +188,10 @@ type BandwidthLimitConfig struct {
enabled = false
url = "socks5://127.0.0.1:1080" # "http://127.0.0.1:7890"
*/
// OutboundConfig 定义出站代理相关的配置
type OutboundConfig struct {
Enabled bool `toml:"enabled"`
Url string `toml:"url"`
Enabled bool `toml:"enabled" wanf:"enabled"`
Url string `toml:"url" wanf:"url"`
}
/*
@ -184,15 +203,16 @@ auth = false
user1 = "testpass"
test = "test123"
*/
// DockerConfig 定义 Docker 相关的配置
type DockerConfig struct {
Enabled bool `toml:"enabled"`
Target string `toml:"target"`
Auth bool `toml:"auth"`
Credentials map[string]string `toml:"credentials"`
AuthPassThrough bool `toml:"authPassThrough"`
Enabled bool `toml:"enabled" wanf:"enabled"`
Target string `toml:"target" wanf:"target"`
Auth bool `toml:"auth" wanf:"auth"`
Credentials map[string]string `toml:"credentials" wanf:"credentials"`
AuthPassThrough bool `toml:"authPassThrough" wanf:"authPassThrough"`
}
// LoadConfig 从 TOML 配置文件加载配置
// LoadConfig 从配置文件加载配置
func LoadConfig(filePath string) (*Config, error) {
if !FileExists(filePath) {
// 楔入配置文件
@ -202,15 +222,23 @@ func LoadConfig(filePath string) (*Config, error) {
}
return DefaultConfig(), nil
}
var config Config
ext := filepath.Ext(filePath)
log.Printf("Loading config from %s with extension %s", filePath, ext)
if ext == ".wanf" {
if err := wanf.DecodeFile(filePath, &config); err != nil {
return nil, err
}
return &config, nil
}
if _, err := toml.DecodeFile(filePath, &config); err != nil {
return nil, err
}
return &config, nil
}
// 写入配置文件
// WriteConfig 写入配置文件
func (c *Config) WriteConfig(filePath string) error {
file, err := os.Create(filePath)
if err != nil {
@ -218,17 +246,27 @@ func (c *Config) WriteConfig(filePath string) error {
}
defer file.Close()
ext := filepath.Ext(filePath)
fmt.Printf("%s", ext)
if ext == ".wanf" {
err := wanf.NewStreamEncoder(file).Encode(c)
if err != nil {
return err
}
return nil
}
encoder := toml.NewEncoder(file)
return encoder.Encode(c)
}
// 检测文件是否存在
// FileExists 检测文件是否存在
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
// 默认配置结构体
// DefaultConfig 返回默认配置结构体
func DefaultConfig() *Config {
return &Config{
Server: ServerConfig{