add migrate old config

This commit is contained in:
yuzuki999 2023-06-02 09:19:37 +08:00
parent 989b4ff13e
commit e263e70f37
5 changed files with 99 additions and 7 deletions

View File

@ -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"))
}

View File

@ -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."))
}

View File

@ -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
}

View File

@ -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)
}

86
conf/old.go Normal file
View File

@ -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.")
}
}