mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 01:08:33 +00:00
Add TOML Support (#98)
This commit is contained in:
parent
09f9d03fb6
commit
f073456ac0
7 changed files with 96 additions and 0 deletions
|
@ -63,6 +63,8 @@ import (
|
|||
// The following line loads JSON internally
|
||||
_ "github.com/xtls/xray-core/main/jsonem"
|
||||
|
||||
_ "github.com/xtls/xray-core/main/toml"
|
||||
|
||||
// Load config from file or http(s)
|
||||
_ "github.com/xtls/xray-core/main/confloader/external"
|
||||
|
||||
|
|
|
@ -154,6 +154,8 @@ func getConfigFormat() string {
|
|||
switch strings.ToLower(*format) {
|
||||
case "pb", "protobuf":
|
||||
return "protobuf"
|
||||
case "toml":
|
||||
return "toml"
|
||||
default:
|
||||
return "json"
|
||||
}
|
||||
|
|
9
main/toml/errors.generated.go
Normal file
9
main/toml/errors.generated.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package toml
|
||||
|
||||
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/toml/toml.go
Normal file
44
main/toml/toml.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package toml
|
||||
|
||||
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: "TOML",
|
||||
Extension: []string{"toml"},
|
||||
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.DecodeTOMLConfig(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.LoadTOMLConfig(v)
|
||||
default:
|
||||
return nil, newError("unknow type")
|
||||
}
|
||||
},
|
||||
}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue