格式化
This commit is contained in:
+24
-26
@@ -5,12 +5,14 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"mmw-agent/internal/constants"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const AgentUserAgent = "miaomiaowux/0.1"
|
||||
const AgentUserAgent = constants.AgentUserAgent
|
||||
|
||||
// Config holds the agent configuration
|
||||
// Config 保存 agent 的运行配置。
|
||||
type Config struct {
|
||||
MasterURL string `yaml:"master_url"`
|
||||
Token string `yaml:"token"`
|
||||
@@ -21,20 +23,16 @@ type Config struct {
|
||||
SpeedReportInterval time.Duration `yaml:"speed_report_interval"`
|
||||
}
|
||||
|
||||
// XrayServer represents a local Xray server configuration
|
||||
// XrayServer 表示本机 Xray 节点配置。
|
||||
type XrayServer struct {
|
||||
Name string `yaml:"name"`
|
||||
ConfigPath string `yaml:"config_path"`
|
||||
}
|
||||
|
||||
// DefaultXrayConfigPaths are the default paths to search for Xray config
|
||||
var DefaultXrayConfigPaths = []string{
|
||||
"/usr/local/etc/xray/config.json",
|
||||
"/etc/xray/config.json",
|
||||
"/opt/xray/config.json",
|
||||
}
|
||||
// DefaultXrayConfigPaths 是默认的 Xray 配置搜索路径。
|
||||
var DefaultXrayConfigPaths = append([]string(nil), constants.DefaultXrayConfigPaths...)
|
||||
|
||||
// Load loads configuration from a YAML file
|
||||
// 从 YAML 文件加载配置。
|
||||
func Load(path string) (*Config, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -46,13 +44,13 @@ func Load(path string) (*Config, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply defaults
|
||||
// 补齐默认值
|
||||
config.applyDefaults()
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// FromEnv creates configuration from environment variables
|
||||
// 从环境变量构造配置。
|
||||
func FromEnv() *Config {
|
||||
config := &Config{
|
||||
MasterURL: os.Getenv("MMWX_MASTER_URL"),
|
||||
@@ -61,14 +59,14 @@ func FromEnv() *Config {
|
||||
ListenPort: os.Getenv("MMWX_LISTEN_PORT"),
|
||||
}
|
||||
|
||||
// Parse Xray config path from env
|
||||
// 读取 Xray 配置路径
|
||||
if xrayConfig := os.Getenv("MMWX_XRAY_CONFIG"); xrayConfig != "" {
|
||||
config.XrayServers = []XrayServer{
|
||||
{Name: "primary", ConfigPath: xrayConfig},
|
||||
}
|
||||
}
|
||||
|
||||
// Parse intervals
|
||||
// 读取上报间隔
|
||||
if interval := os.Getenv("MMWX_TRAFFIC_INTERVAL"); interval != "" {
|
||||
if d, err := time.ParseDuration(interval); err == nil {
|
||||
config.TrafficReportInterval = d
|
||||
@@ -84,7 +82,7 @@ func FromEnv() *Config {
|
||||
return config
|
||||
}
|
||||
|
||||
// Merge merges environment config into file config (env takes precedence)
|
||||
// 合并环境变量配置到文件配置(环境变量优先)。
|
||||
func (c *Config) Merge(env *Config) {
|
||||
if env.MasterURL != "" {
|
||||
c.MasterURL = env.MasterURL
|
||||
@@ -109,28 +107,28 @@ func (c *Config) Merge(env *Config) {
|
||||
}
|
||||
}
|
||||
|
||||
// applyDefaults sets default values for unset fields
|
||||
// 为空字段填充默认值。
|
||||
func (c *Config) applyDefaults() {
|
||||
if c.ConnectionMode == "" {
|
||||
c.ConnectionMode = "auto"
|
||||
c.ConnectionMode = constants.ConnectionModeAuto
|
||||
}
|
||||
if c.ListenPort == "" {
|
||||
c.ListenPort = "23889"
|
||||
c.ListenPort = constants.DefaultListenPort
|
||||
}
|
||||
if c.TrafficReportInterval == 0 {
|
||||
c.TrafficReportInterval = 1 * time.Minute
|
||||
c.TrafficReportInterval = constants.DefaultTrafficReportInterval
|
||||
}
|
||||
if c.SpeedReportInterval == 0 {
|
||||
c.SpeedReportInterval = 3 * time.Second
|
||||
c.SpeedReportInterval = constants.DefaultSpeedReportInterval
|
||||
}
|
||||
|
||||
// Auto-discover Xray servers if not configured
|
||||
// 未显式配置时自动探测 Xray 配置
|
||||
if len(c.XrayServers) == 0 {
|
||||
c.XrayServers = c.discoverXrayServers()
|
||||
}
|
||||
}
|
||||
|
||||
// discoverXrayServers scans default paths for Xray config files
|
||||
// 扫描默认路径中的 Xray 配置文件。
|
||||
func (c *Config) discoverXrayServers() []XrayServer {
|
||||
var servers []XrayServer
|
||||
for i, path := range DefaultXrayConfigPaths {
|
||||
@@ -144,11 +142,11 @@ func (c *Config) discoverXrayServers() []XrayServer {
|
||||
return servers
|
||||
}
|
||||
|
||||
// Validate checks if the configuration is valid
|
||||
// 校验配置是否合法。
|
||||
func (c *Config) Validate() error {
|
||||
// Token is required for non-pull modes
|
||||
if c.ConnectionMode != "pull" && c.Token == "" {
|
||||
// Allow empty token, will work in pull mode only
|
||||
// 拉取模式之外通常需要 token
|
||||
if c.ConnectionMode != constants.ConnectionModePull && c.Token == "" {
|
||||
// 兼容空 token,实际仅拉取模式可正常工作
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user