mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 01:08:33 +00:00
Add YAML Support (#86)
This commit is contained in:
parent
f073456ac0
commit
85619b5a29
7 changed files with 96 additions and 0 deletions
|
@ -62,6 +62,7 @@ import (
|
|||
// _ "github.com/xtls/xray-core/main/json"
|
||||
// The following line loads JSON internally
|
||||
_ "github.com/xtls/xray-core/main/jsonem"
|
||||
_ "github.com/xtls/xray-core/main/yaml"
|
||||
|
||||
_ "github.com/xtls/xray-core/main/toml"
|
||||
|
||||
|
|
|
@ -152,6 +152,8 @@ func getConfigFilePath() cmdarg.Arg {
|
|||
|
||||
func getConfigFormat() string {
|
||||
switch strings.ToLower(*format) {
|
||||
case "yaml", "yml":
|
||||
return "yaml"
|
||||
case "pb", "protobuf":
|
||||
return "protobuf"
|
||||
case "toml":
|
||||
|
@ -165,6 +167,9 @@ func startXray() (core.Server, error) {
|
|||
configFiles := getConfigFilePath()
|
||||
|
||||
config, err := core.LoadConfig(getConfigFormat(), configFiles[0], configFiles)
|
||||
|
||||
//config, err := core.LoadConfigs(getConfigFormat(), configFiles)
|
||||
|
||||
if err != nil {
|
||||
return nil, newError("failed to load config files: [", configFiles.String(), "]").Base(err)
|
||||
}
|
||||
|
|
9
main/yaml/errors.generated.go
Normal file
9
main/yaml/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package yaml
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
44
main/yaml/yaml.go
Normal file
44
main/yaml/yaml.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package yaml
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/cmdarg"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
"github.com/xtls/xray-core/infra/conf/serial"
|
||||
"github.com/xtls/xray-core/main/confloader"
|
||||
)
|
||||
|
||||
func init() {
|
||||
common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
|
||||
Name: "YAML",
|
||||
Extension: []string{"yaml", "yml"},
|
||||
Loader: func(input interface{}) (*core.Config, error) {
|
||||
switch v := input.(type) {
|
||||
case cmdarg.Arg:
|
||||
cf := &conf.Config{}
|
||||
for i, arg := range v {
|
||||
newError("Reading config: ", arg).AtInfo().WriteToLog()
|
||||
r, err := confloader.LoadConfig(arg)
|
||||
common.Must(err)
|
||||
c, err := serial.DecodeYAMLConfig(r)
|
||||
common.Must(err)
|
||||
if i == 0 {
|
||||
// This ensure even if the muti-json parser do not support a setting,
|
||||
// It is still respected automatically for the first configure file
|
||||
*cf = *c
|
||||
continue
|
||||
}
|
||||
cf.Override(c, arg)
|
||||
}
|
||||
return cf.Build()
|
||||
case io.Reader:
|
||||
return serial.LoadYAMLConfig(v)
|
||||
default:
|
||||
return nil, newError("unknow type")
|
||||
}
|
||||
},
|
||||
}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue