Files
mmw-agent/internal/config/config.go
T

153 lines
3.8 KiB
Go
Raw Normal View History

2026-01-28 13:13:58 +08:00
package config
import (
"os"
"strconv"
"time"
2026-04-10 15:25:21 +08:00
"mmw-agent/internal/constants"
2026-01-28 13:13:58 +08:00
"gopkg.in/yaml.v3"
)
2026-04-10 15:25:21 +08:00
const AgentUserAgent = constants.AgentUserAgent
2026-03-12 16:13:50 +08:00
2026-04-10 15:25:21 +08:00
// Config 保存 agent 的运行配置。
2026-01-28 13:13:58 +08:00
type Config struct {
MasterURL string `yaml:"master_url"`
Token string `yaml:"token"`
ConnectionMode string `yaml:"connection_mode"`
ListenPort string `yaml:"listen_port"`
XrayServers []XrayServer `yaml:"xray_servers"`
TrafficReportInterval time.Duration `yaml:"traffic_report_interval"`
SpeedReportInterval time.Duration `yaml:"speed_report_interval"`
}
2026-04-10 15:25:21 +08:00
// XrayServer 表示本机 Xray 节点配置。
2026-01-28 13:13:58 +08:00
type XrayServer struct {
Name string `yaml:"name"`
ConfigPath string `yaml:"config_path"`
}
2026-04-10 15:25:21 +08:00
// DefaultXrayConfigPaths 是默认的 Xray 配置搜索路径。
var DefaultXrayConfigPaths = append([]string(nil), constants.DefaultXrayConfigPaths...)
2026-01-28 13:13:58 +08:00
2026-04-10 15:25:21 +08:00
// 从 YAML 文件加载配置。
2026-01-28 13:13:58 +08:00
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}
2026-04-10 15:25:21 +08:00
// 补齐默认值
2026-01-28 13:13:58 +08:00
config.applyDefaults()
return &config, nil
}
2026-04-10 15:25:21 +08:00
// 从环境变量构造配置。
2026-01-28 13:13:58 +08:00
func FromEnv() *Config {
config := &Config{
MasterURL: os.Getenv("MMWX_MASTER_URL"),
Token: os.Getenv("MMWX_TOKEN"),
ConnectionMode: os.Getenv("MMWX_CONNECTION_MODE"),
ListenPort: os.Getenv("MMWX_LISTEN_PORT"),
}
2026-04-10 15:25:21 +08:00
// 读取 Xray 配置路径
2026-01-28 13:13:58 +08:00
if xrayConfig := os.Getenv("MMWX_XRAY_CONFIG"); xrayConfig != "" {
config.XrayServers = []XrayServer{
{Name: "primary", ConfigPath: xrayConfig},
}
}
2026-04-10 15:25:21 +08:00
// 读取上报间隔
2026-01-28 13:13:58 +08:00
if interval := os.Getenv("MMWX_TRAFFIC_INTERVAL"); interval != "" {
if d, err := time.ParseDuration(interval); err == nil {
config.TrafficReportInterval = d
}
}
if interval := os.Getenv("MMWX_SPEED_INTERVAL"); interval != "" {
if d, err := time.ParseDuration(interval); err == nil {
config.SpeedReportInterval = d
}
}
config.applyDefaults()
return config
}
2026-04-10 15:25:21 +08:00
// 合并环境变量配置到文件配置(环境变量优先)。
2026-01-28 13:13:58 +08:00
func (c *Config) Merge(env *Config) {
if env.MasterURL != "" {
c.MasterURL = env.MasterURL
}
if env.Token != "" {
c.Token = env.Token
}
if env.ConnectionMode != "" {
c.ConnectionMode = env.ConnectionMode
}
if env.ListenPort != "" {
c.ListenPort = env.ListenPort
}
if len(env.XrayServers) > 0 {
c.XrayServers = env.XrayServers
}
if env.TrafficReportInterval > 0 {
c.TrafficReportInterval = env.TrafficReportInterval
}
if env.SpeedReportInterval > 0 {
c.SpeedReportInterval = env.SpeedReportInterval
}
}
2026-04-10 15:25:21 +08:00
// 为空字段填充默认值。
2026-01-28 13:13:58 +08:00
func (c *Config) applyDefaults() {
if c.ConnectionMode == "" {
2026-04-10 15:25:21 +08:00
c.ConnectionMode = constants.ConnectionModeAuto
2026-01-28 13:13:58 +08:00
}
if c.ListenPort == "" {
2026-04-10 15:25:21 +08:00
c.ListenPort = constants.DefaultListenPort
2026-01-28 13:13:58 +08:00
}
if c.TrafficReportInterval == 0 {
2026-04-10 15:25:21 +08:00
c.TrafficReportInterval = constants.DefaultTrafficReportInterval
2026-01-28 13:13:58 +08:00
}
if c.SpeedReportInterval == 0 {
2026-04-10 15:25:21 +08:00
c.SpeedReportInterval = constants.DefaultSpeedReportInterval
2026-01-28 13:13:58 +08:00
}
2026-04-10 15:25:21 +08:00
// 未显式配置时自动探测 Xray 配置
2026-01-28 13:13:58 +08:00
if len(c.XrayServers) == 0 {
c.XrayServers = c.discoverXrayServers()
}
}
2026-04-10 15:25:21 +08:00
// 扫描默认路径中的 Xray 配置文件。
2026-01-28 13:13:58 +08:00
func (c *Config) discoverXrayServers() []XrayServer {
var servers []XrayServer
for i, path := range DefaultXrayConfigPaths {
if _, err := os.Stat(path); err == nil {
servers = append(servers, XrayServer{
Name: "xray-" + strconv.Itoa(i+1),
ConfigPath: path,
})
}
}
return servers
}
2026-04-10 15:25:21 +08:00
// 校验配置是否合法。
2026-01-28 13:13:58 +08:00
func (c *Config) Validate() error {
2026-04-10 15:25:21 +08:00
// 拉取模式之外通常需要 token
if c.ConnectionMode != constants.ConnectionModePull && c.Token == "" {
// 兼容空 token,实际仅拉取模式可正常工作
2026-01-28 13:13:58 +08:00
}
return nil
}