init
This commit is contained in:
79
pkg/config/config.go
Normal file
79
pkg/config/config.go
Normal file
@@ -0,0 +1,79 @@
|
||||
// Package config 处理配置文件的加载和解析
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// ServerConfig 服务器基本配置
|
||||
type ServerConfig struct {
|
||||
Socks5Port int `yaml:"socks5_port"` // SOCKS5 代理端口
|
||||
HttpPort int `yaml:"http_port"` // HTTP 代理端口
|
||||
BindAddress string `yaml:"bind_address"` // 监听地址
|
||||
}
|
||||
|
||||
// LogConfig 日志系统配置
|
||||
type LogConfig struct {
|
||||
Level string `yaml:"level"` // 日志级别
|
||||
Output []string `yaml:"output"` // 输出位置列表
|
||||
File string `yaml:"file"` // 日志文件路径
|
||||
ShowCaller bool `yaml:"show_caller"` // 是否显示调用者
|
||||
Format string `yaml:"format"` // 日志格式
|
||||
MaxSize int `yaml:"max_size"` // 单个日志文件最大大小(MB)
|
||||
MaxBackups int `yaml:"max_backups"` // 保留的旧文件数量
|
||||
MaxAge int `yaml:"max_age"` // 日志保留天数
|
||||
Compress bool `yaml:"compress"` // 是否压缩旧文件
|
||||
}
|
||||
|
||||
// UpstreamConfig 上游代理配置
|
||||
type UpstreamConfig struct {
|
||||
Enable bool `yaml:"enable"` // 是否启用上游代理
|
||||
Type string `yaml:"type"` // 代理类型
|
||||
Server string `yaml:"server"` // 服务器地址
|
||||
Username string `yaml:"username"` // 认证用户名
|
||||
Password string `yaml:"password"` // 认证密码
|
||||
}
|
||||
|
||||
// ProxyConfig 代理服务器详细配置
|
||||
type ProxyConfig struct {
|
||||
BufferSize int `yaml:"buffer_size"` // 缓冲区大小
|
||||
Timeout int `yaml:"timeout"` // 超时时间(秒)
|
||||
EnableMetrics bool `yaml:"enable_metrics"` // 是否启用指标
|
||||
Debug bool `yaml:"debug"` // 是否启用调试
|
||||
Upstream UpstreamConfig `yaml:"upstream"` // 上游代理配置
|
||||
}
|
||||
|
||||
// Config 总配置结构
|
||||
type Config struct {
|
||||
Server ServerConfig `yaml:"server"` // 服务器配置
|
||||
Log LogConfig `yaml:"log"` // 日志配置
|
||||
Proxy ProxyConfig `yaml:"proxy"` // 代理配置
|
||||
}
|
||||
|
||||
// Load 从文件加载配置
|
||||
func Load(filename string) (*Config, error) {
|
||||
// 读取配置文件
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取配置文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析配置
|
||||
config := &Config{}
|
||||
if err := yaml.Unmarshal(data, config); err != nil {
|
||||
return nil, fmt.Errorf("解析配置文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if len(config.Log.Output) == 0 {
|
||||
config.Log.Output = []string{"console"} // 默认输出到控制台
|
||||
}
|
||||
if config.Log.File == "" {
|
||||
config.Log.File = "proxy.log" // 默认日志文件名
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
Reference in New Issue
Block a user