mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
add migrate old config
This commit is contained in:
13
conf/conf.go
13
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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
86
conf/old.go
Normal 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.")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user