|
|
@@ -3,15 +3,11 @@ package serial
|
|
|
import (
|
|
|
"bytes"
|
|
|
"encoding/json"
|
|
|
- "io"
|
|
|
-
|
|
|
- "github.com/ghodss/yaml"
|
|
|
- "github.com/pelletier/go-toml"
|
|
|
-
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
"github.com/xtls/xray-core/infra/conf"
|
|
|
json_reader "github.com/xtls/xray-core/infra/conf/json"
|
|
|
+ "io"
|
|
|
)
|
|
|
|
|
|
type offset struct {
|
|
|
@@ -83,68 +79,3 @@ func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
|
|
|
|
|
|
return pbConfig, nil
|
|
|
}
|
|
|
-
|
|
|
-// DecodeTOMLConfig reads from reader and decode the config into *conf.Config
|
|
|
-// using github.com/pelletier/go-toml and map to convert toml to json.
|
|
|
-func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
|
|
|
- tomlFile, err := io.ReadAll(reader)
|
|
|
- if err != nil {
|
|
|
- return nil, newError("failed to read config file").Base(err)
|
|
|
- }
|
|
|
-
|
|
|
- configMap := make(map[string]interface{})
|
|
|
- if err := toml.Unmarshal(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
|
|
|
-}
|
|
|
-
|
|
|
-// DecodeYAMLConfig reads from reader and decode the config into *conf.Config
|
|
|
-// using github.com/ghodss/yaml to convert yaml to json.
|
|
|
-func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
|
|
|
- yamlFile, err := io.ReadAll(reader)
|
|
|
- if err != nil {
|
|
|
- return nil, newError("failed to read config file").Base(err)
|
|
|
- }
|
|
|
-
|
|
|
- jsonFile, err := yaml.YAMLToJSON(yamlFile)
|
|
|
- if err != nil {
|
|
|
- return nil, newError("failed to convert yaml to json").Base(err)
|
|
|
- }
|
|
|
-
|
|
|
- return DecodeJSONConfig(bytes.NewReader(jsonFile))
|
|
|
-}
|
|
|
-
|
|
|
-func LoadYAMLConfig(reader io.Reader) (*core.Config, error) {
|
|
|
- yamlConfig, err := DecodeYAMLConfig(reader)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- pbConfig, err := yamlConfig.Build()
|
|
|
- if err != nil {
|
|
|
- return nil, newError("failed to parse yaml config").Base(err)
|
|
|
- }
|
|
|
-
|
|
|
- return pbConfig, nil
|
|
|
-}
|