V2bX/conf/conf.go

54 lines
1.0 KiB
Go
Raw Normal View History

package conf
2022-09-13 11:08:20 +08:00
import (
"fmt"
2023-06-16 10:04:39 +08:00
"gopkg.in/yaml.v3"
2023-06-02 09:19:37 +08:00
"io"
2022-09-13 11:08:20 +08:00
"os"
)
type Conf struct {
CoreConfig CoreConfig `yaml:"CoreConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"`
}
func New() *Conf {
return &Conf{
CoreConfig: CoreConfig{
Type: "xray",
XrayConfig: &XrayConfig{
LogConfig: NewLogConfig(),
2023-06-09 21:20:41 +08:00
AssetPath: "/etc/V2bX/",
DnsConfigPath: "",
InboundConfigPath: "",
OutboundConfigPath: "",
RouteConfigPath: "",
ConnectionConfig: NewConnectionConfig(),
},
},
NodesConfig: []*NodeConfig{},
}
}
2022-09-13 11:08:20 +08:00
func (p *Conf) LoadFromPath(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
2022-10-10 11:31:15 +08:00
return fmt.Errorf("open config file error: %s", err)
2022-09-13 11:08:20 +08:00
}
2023-06-02 09:19:37 +08:00
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("read file error: %s", err)
}
err = yaml.Unmarshal(content, p)
2022-09-13 11:08:20 +08:00
if err != nil {
2022-10-10 11:31:15 +08:00
return fmt.Errorf("decode config error: %s", err)
}
2023-06-02 09:19:37 +08:00
old := &OldConfig{}
err = yaml.Unmarshal(content, old)
if err == nil {
migrateOldConfig(p, old)
}
2022-10-10 11:31:15 +08:00
return nil
}