You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.6 KiB
108 lines
2.6 KiB
2 weeks ago
|
package config
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"time"
|
||
|
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
// Config 客户端配置结构
|
||
|
type Config struct {
|
||
|
ServiceType string `yaml:"serviceType"`
|
||
|
Server Server `yaml:"server"`
|
||
|
Proxy Proxy `yaml:"proxy"`
|
||
|
GlobalProxy GlobalProxy `yaml:"globalProxy"`
|
||
|
TransparentProxy TransparentProxy `yaml:"transparentProxy"`
|
||
|
LogLevel string `yaml:"logLevel"`
|
||
|
Timeout time.Duration `yaml:"timeout"`
|
||
|
}
|
||
|
|
||
|
// Server SOCKS5服务器配置
|
||
|
type Server struct {
|
||
|
Address string `yaml:"address"`
|
||
|
Port int `yaml:"port"`
|
||
|
Username string `yaml:"username"`
|
||
|
Password string `yaml:"password"`
|
||
|
}
|
||
|
|
||
|
// Proxy 代理模式配置
|
||
|
type Proxy struct {
|
||
|
Mode string `yaml:"mode"`
|
||
|
LocalPort int `yaml:"localPort"`
|
||
|
}
|
||
|
|
||
|
// GlobalProxy 全局代理配置
|
||
|
type GlobalProxy struct {
|
||
|
Enabled bool `yaml:"enabled"`
|
||
|
DNSProxy bool `yaml:"dnsProxy"`
|
||
|
DNSPort int `yaml:"dnsPort"`
|
||
|
Routing Routing `yaml:"routing"`
|
||
|
}
|
||
|
|
||
|
// TransparentProxy 透明代理配置
|
||
|
type TransparentProxy struct {
|
||
|
Enabled bool `yaml:"enabled"`
|
||
|
Port int `yaml:"port"`
|
||
|
DNSPort int `yaml:"dnsPort"`
|
||
|
ModifyDNS bool `yaml:"modifyDNS"`
|
||
|
ModifyRoute bool `yaml:"modifyRoute"`
|
||
|
}
|
||
|
|
||
|
// Routing 路由规则配置
|
||
|
type Routing struct {
|
||
|
BypassLocal bool `yaml:"bypassLocal"`
|
||
|
BypassPrivate bool `yaml:"bypassPrivate"`
|
||
|
BypassDomains []string `yaml:"bypassDomains"`
|
||
|
ForceDomains []string `yaml:"forceDomains"`
|
||
|
}
|
||
|
|
||
|
// LoadConfig 从文件加载配置
|
||
|
func LoadConfig(configPath string) (*Config, error) {
|
||
|
data, err := ioutil.ReadFile(configPath)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("failed to read config file: %v", err)
|
||
|
}
|
||
|
|
||
|
var config Config
|
||
|
if err := yaml.Unmarshal(data, &config); err != nil {
|
||
|
return nil, fmt.Errorf("failed to parse config file: %v", err)
|
||
|
}
|
||
|
|
||
|
// 设置默认值
|
||
|
if config.LogLevel == "" {
|
||
|
config.LogLevel = "info"
|
||
|
}
|
||
|
if config.Timeout == 0 {
|
||
|
config.Timeout = 30 * time.Second
|
||
|
}
|
||
|
if config.Proxy.LocalPort == 0 {
|
||
|
config.Proxy.LocalPort = 8080
|
||
|
}
|
||
|
|
||
|
return &config, nil
|
||
|
}
|
||
|
|
||
|
// GetServerAddr 获取服务器地址
|
||
|
func (c *Config) GetServerAddr() string {
|
||
|
return fmt.Sprintf("%s:%d", c.Server.Address, c.Server.Port)
|
||
|
}
|
||
|
|
||
|
// Validate 验证配置
|
||
|
func (c *Config) Validate() error {
|
||
|
if c.Server.Address == "" {
|
||
|
return fmt.Errorf("server address is required")
|
||
|
}
|
||
|
if c.Server.Port <= 0 || c.Server.Port > 65535 {
|
||
|
return fmt.Errorf("invalid server port: %d", c.Server.Port)
|
||
|
}
|
||
|
|
||
|
validModes := map[string]bool{"http": true, "global": true, "transparent": true}
|
||
|
if !validModes[c.Proxy.Mode] {
|
||
|
return fmt.Errorf("invalid proxy mode: %s", c.Proxy.Mode)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|