From e263e70f373569d408171f532877fd8940450c15 Mon Sep 17 00:00:00 2001 From: yuzuki999 Date: Fri, 2 Jun 2023 09:19:37 +0800 Subject: [PATCH] add migrate old config --- cmd/common_test.go | 4 --- cmd/version.go | 1 - conf/conf.go | 13 ++++++- conf/conf_test.go | 2 +- conf/old.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 conf/old.go diff --git a/cmd/common_test.go b/cmd/common_test.go index 53366bd..88ebe6e 100644 --- a/cmd/common_test.go +++ b/cmd/common_test.go @@ -2,10 +2,6 @@ package cmd import "testing" -func TestExecCommand(t *testing.T) { - t.Log(execCommand("echo test")) -} - func Test_printFailed(t *testing.T) { t.Log(Err("test")) } diff --git a/cmd/version.go b/cmd/version.go index 92a5204..2d1f11f 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -27,5 +27,4 @@ func showVersion() { fmt.Printf("%s %s (%s) \n", codename, version, intro) // Warning fmt.Println(Warn("This version need V2board version >= 1.7.0.")) - fmt.Println(Warn("This version changed config file. Please check config file before running.")) } diff --git a/conf/conf.go b/conf/conf.go index 03885d9..47ef9b4 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/fsnotify/fsnotify" "gopkg.in/yaml.v3" + "io" "log" "os" "path" @@ -39,10 +40,20 @@ func (p *Conf) LoadFromPath(filePath string) error { if err != nil { return fmt.Errorf("open config file error: %s", err) } - err = yaml.NewDecoder(f).Decode(p) + defer f.Close() + content, err := io.ReadAll(f) + if err != nil { + return fmt.Errorf("read file error: %s", err) + } + err = yaml.Unmarshal(content, p) if err != nil { return fmt.Errorf("decode config error: %s", err) } + old := &OldConfig{} + err = yaml.Unmarshal(content, old) + if err == nil { + migrateOldConfig(p, old) + } return nil } diff --git a/conf/conf_test.go b/conf/conf_test.go index 3f8404a..48a2acf 100644 --- a/conf/conf_test.go +++ b/conf/conf_test.go @@ -4,5 +4,5 @@ import "testing" func TestConf_LoadFromPath(t *testing.T) { c := New() - t.Log(c.LoadFromPath("../example/config.yml.example")) + t.Log(c.LoadFromPath("../example/config.yml.example"), c.NodesConfig[0].ControllerConfig.LimitConfig.IPLimit) } diff --git a/conf/old.go b/conf/old.go new file mode 100644 index 0000000..5efa33a --- /dev/null +++ b/conf/old.go @@ -0,0 +1,86 @@ +package conf + +import "log" + +type OldConfig struct { + NodesConfig []*struct { + ApiConfig *OldApiConfig `yaml:"ApiConfig"` + ControllerConfig *OldControllerConfig `yaml:"ControllerConfig"` + } `yaml:"Nodes"` +} + +type OldControllerConfig struct { + ListenIP string `yaml:"ListenIP"` + SendIP string `yaml:"SendIP"` + EnableDNS bool `yaml:"EnableDNS"` + DNSType string `yaml:"DNSType"` + DisableUploadTraffic bool `yaml:"DisableUploadTraffic"` + DisableGetRule bool `yaml:"DisableGetRule"` + EnableProxyProtocol bool `yaml:"EnableProxyProtocol"` + EnableFallback bool `yaml:"EnableFallback"` + DisableIVCheck bool `yaml:"DisableIVCheck"` + DisableSniffing bool `yaml:"DisableSniffing"` + FallBackConfigs []*FallBackConfig `yaml:"FallBackConfigs"` + EnableIpRecorder bool `yaml:"EnableIpRecorder"` + IpRecorderConfig *IpReportConfig `yaml:"IpRecorderConfig"` + EnableDynamicSpeedLimit bool `yaml:"EnableDynamicSpeedLimit"` + DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `yaml:"DynamicSpeedLimitConfig"` + CertConfig *CertConfig `yaml:"CertConfig"` +} + +type OldApiConfig struct { + APIHost string `yaml:"ApiHost"` + NodeID int `yaml:"NodeID"` + Key string `yaml:"ApiKey"` + NodeType string `yaml:"NodeType"` + EnableVless bool `yaml:"EnableVless"` + Timeout int `yaml:"Timeout"` + SpeedLimit int `yaml:"SpeedLimit"` + DeviceLimit int `yaml:"DeviceLimit"` + RuleListPath string `yaml:"RuleListPath"` + DisableCustomConfig bool `yaml:"DisableCustomConfig"` +} + +func migrateOldConfig(c *Conf, old *OldConfig) { + changed := false + for i, n := range c.NodesConfig { + if i >= len(old.NodesConfig) { + break + } + // node option + if !old.NodesConfig[i].ApiConfig.EnableVless { + n.ControllerConfig.EnableVless = true + changed = true + } + // limit config + if old.NodesConfig[i].ApiConfig.SpeedLimit != 0 { + n.ControllerConfig.LimitConfig.SpeedLimit = old.NodesConfig[i].ApiConfig.SpeedLimit + changed = true + } + if old.NodesConfig[i].ApiConfig.DeviceLimit != 0 { + n.ControllerConfig.LimitConfig.IPLimit = old.NodesConfig[i].ApiConfig.DeviceLimit + changed = true + } + if !old.NodesConfig[i].ControllerConfig.EnableDynamicSpeedLimit { + n.ControllerConfig.LimitConfig.EnableDynamicSpeedLimit = true + changed = true + } + if old.NodesConfig[i].ControllerConfig.DynamicSpeedLimitConfig != nil { + n.ControllerConfig.LimitConfig.DynamicSpeedLimitConfig = + old.NodesConfig[i].ControllerConfig.DynamicSpeedLimitConfig + changed = true + } + if !old.NodesConfig[i].ControllerConfig.EnableIpRecorder { + n.ControllerConfig.LimitConfig.EnableIpRecorder = true + changed = true + } + if old.NodesConfig[i].ControllerConfig.IpRecorderConfig != nil { + n.ControllerConfig.LimitConfig.IpRecorderConfig = + old.NodesConfig[i].ControllerConfig.IpRecorderConfig + changed = true + } + } + if changed { + log.Println("Warning: This config file is old.") + } +}