Xray-core/infra/conf/serial/loader.go

150 lines
3.5 KiB
Go
Raw Permalink Normal View History

2020-11-25 13:01:53 +02:00
package serial
import (
"bytes"
"encoding/json"
"io"
2020-12-24 21:30:26 +02:00
"github.com/ghodss/yaml"
"github.com/pelletier/go-toml"
2020-12-04 03:36:16 +02:00
"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"
2020-11-25 13:01:53 +02:00
)
type offset struct {
line int
char int
}
func findOffset(b []byte, o int) *offset {
if o >= len(b) || o < 0 {
return nil
}
line := 1
char := 0
for i, x := range b {
if i == o {
break
}
if x == '\n' {
line++
char = 0
} else {
char++
}
}
return &offset{line: line, char: char}
}
// DecodeJSONConfig reads from reader and decode the config into *conf.Config
// syntax error could be detected.
func DecodeJSONConfig(reader io.Reader) (*conf.Config, error) {
jsonConfig := &conf.Config{}
jsonContent := bytes.NewBuffer(make([]byte, 0, 10240))
jsonReader := io.TeeReader(&json_reader.Reader{
Reader: reader,
}, jsonContent)
decoder := json.NewDecoder(jsonReader)
if err := decoder.Decode(jsonConfig); err != nil {
var pos *offset
cause := errors.Cause(err)
switch tErr := cause.(type) {
case *json.SyntaxError:
pos = findOffset(jsonContent.Bytes(), int(tErr.Offset))
case *json.UnmarshalTypeError:
pos = findOffset(jsonContent.Bytes(), int(tErr.Offset))
}
if pos != nil {
return nil, errors.New("failed to read config file at line ", pos.line, " char ", pos.char).Base(err)
2020-11-25 13:01:53 +02:00
}
return nil, errors.New("failed to read config file").Base(err)
2020-11-25 13:01:53 +02:00
}
return jsonConfig, nil
}
func LoadJSONConfig(reader io.Reader) (*core.Config, error) {
jsonConfig, err := DecodeJSONConfig(reader)
if err != nil {
return nil, err
}
pbConfig, err := jsonConfig.Build()
if err != nil {
return nil, errors.New("failed to parse json config").Base(err)
2020-11-25 13:01:53 +02:00
}
return pbConfig, nil
}
2020-12-24 21:11:32 +02:00
2020-12-25 12:53:17 +02:00
// DecodeTOMLConfig reads from reader and decode the config into *conf.Config
// using github.com/pelletier/go-toml and map to convert toml to json.
2020-12-24 21:11:32 +02:00
func DecodeTOMLConfig(reader io.Reader) (*conf.Config, error) {
tomlFile, err := io.ReadAll(reader)
2020-12-24 21:11:32 +02:00
if err != nil {
return nil, errors.New("failed to read config file").Base(err)
2020-12-24 21:11:32 +02:00
}
configMap := make(map[string]interface{})
if err := toml.Unmarshal(tomlFile, &configMap); err != nil {
return nil, errors.New("failed to convert toml to map").Base(err)
2020-12-24 21:11:32 +02:00
}
jsonFile, err := json.Marshal(&configMap)
if err != nil {
return nil, errors.New("failed to convert map to json").Base(err)
2020-12-24 21:11:32 +02:00
}
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, errors.New("failed to parse toml config").Base(err)
2020-12-24 21:11:32 +02:00
}
return pbConfig, nil
}
2020-12-24 21:30:26 +02:00
// DecodeYAMLConfig reads from reader and decode the config into *conf.Config
2020-12-25 12:53:17 +02:00
// using github.com/ghodss/yaml to convert yaml to json.
2020-12-24 21:30:26 +02:00
func DecodeYAMLConfig(reader io.Reader) (*conf.Config, error) {
yamlFile, err := io.ReadAll(reader)
2020-12-24 21:30:26 +02:00
if err != nil {
return nil, errors.New("failed to read config file").Base(err)
2020-12-24 21:30:26 +02:00
}
jsonFile, err := yaml.YAMLToJSON(yamlFile)
if err != nil {
return nil, errors.New("failed to convert yaml to json").Base(err)
2020-12-24 21:30:26 +02:00
}
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, errors.New("failed to parse yaml config").Base(err)
2020-12-24 21:30:26 +02:00
}
return pbConfig, nil
}