V2bX/conf/conf.go

54 lines
1.0 KiB
Go
Raw Normal View History

package conf
2022-09-12 23:08:20 -04:00
import (
"fmt"
2023-06-15 22:04:39 -04:00
"gopkg.in/yaml.v3"
2023-06-01 21:19:37 -04:00
"io"
2022-09-12 23:08:20 -04: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 09:20:41 -04:00
AssetPath: "/etc/V2bX/",
DnsConfigPath: "",
InboundConfigPath: "",
OutboundConfigPath: "",
RouteConfigPath: "",
ConnectionConfig: NewConnectionConfig(),
},
},
NodesConfig: []*NodeConfig{},
}
}
2022-09-12 23:08:20 -04:00
func (p *Conf) LoadFromPath(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
2022-10-09 23:31:15 -04:00
return fmt.Errorf("open config file error: %s", err)
2022-09-12 23:08:20 -04:00
}
2023-06-01 21:19:37 -04: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-12 23:08:20 -04:00
if err != nil {
2022-10-09 23:31:15 -04:00
return fmt.Errorf("decode config error: %s", err)
}
2023-06-01 21:19:37 -04:00
old := &OldConfig{}
err = yaml.Unmarshal(content, old)
if err == nil {
migrateOldConfig(p, old)
}
2022-10-09 23:31:15 -04:00
return nil
}