2022-07-28 11:00:05 -04:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-07-28 11:00:05 -04:00
|
|
|
type Conf struct {
|
2023-06-07 13:18:56 -04:00
|
|
|
CoreConfig CoreConfig `yaml:"CoreConfig"`
|
|
|
|
NodesConfig []*NodeConfig `yaml:"Nodes"`
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Conf {
|
|
|
|
return &Conf{
|
2023-06-07 13:18:56 -04:00
|
|
|
CoreConfig: CoreConfig{
|
|
|
|
Type: "xray",
|
|
|
|
XrayConfig: &XrayConfig{
|
|
|
|
LogConfig: NewLogConfig(),
|
2023-06-09 09:20:41 -04:00
|
|
|
AssetPath: "/etc/V2bX/",
|
2023-06-07 13:18:56 -04:00
|
|
|
DnsConfigPath: "",
|
|
|
|
InboundConfigPath: "",
|
|
|
|
OutboundConfigPath: "",
|
|
|
|
RouteConfigPath: "",
|
|
|
|
ConnectionConfig: NewConnectionConfig(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
NodesConfig: []*NodeConfig{},
|
2022-07-28 11:00:05 -04:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|