|
@@ -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
|
|
|
+}
|