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
|
@ -4,7 +4,9 @@ import (
|
|||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/core"
|
||||
"github.com/xtls/xray-core/infra/conf"
|
||||
|
@ -80,3 +82,38 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
|
|||
|
||||
return pbConfig, nil
|
||||
}
|
||||
|
||||
func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
|
||||
tomlFile, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, newError("failed to read config file").Base(err)
|
||||
}
|
||||
|
||||
configMap := make(map[string]interface{})
|
||||
|
||||
if _, err := toml.Decode(string(tomlFile), &configMap); err != nil {
|
||||
return nil, newError("failed to convert TOML to Map").Base(err)
|
||||
}
|
||||
|
||||
jsonFile, err := json.Marshal(&configMap)
|
||||
|
||||
if err != nil {
|
||||
return nil, newError("failed to convert Map to JSON").Base(err)
|
||||
}
|
||||
|
||||
return DecodeJSONConfig(bytes.NewReader(jsonFile))
|
||||
}
|
||||
|
||||
func LoadTOMLConfig(reader io.Reader) (*core.Config, error) {
|
||||
tomlConfig, err := DecodeTOMLConfig(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pbConfig, err := tomlConfig.Build()
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse toml config").Base(err)
|
||||
}
|
||||
|
||||
return pbConfig, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue