mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 21:26:07 +03:00
ac628a9427
* Serialize enum to string in MarshalToJson(). * MarshalToJson() respect json tags. * Add insertTypeInfo parameter to MarshalToJson(). * Omit empty string in MarshalToJson(). * Serialize PortList to string in MarshalToJson(). --------- Co-authored-by: nobody <nobody@nowhere.mars>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package serial
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
creflect "github.com/xtls/xray-core/common/reflect"
|
|
"github.com/xtls/xray-core/core"
|
|
"github.com/xtls/xray-core/infra/conf"
|
|
"github.com/xtls/xray-core/main/confloader"
|
|
)
|
|
|
|
func MergeConfigFromFiles(files []string, formats []string) (string, error) {
|
|
c, err := mergeConfigs(files, formats)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if j, ok := creflect.MarshalToJson(c, true); ok {
|
|
return j, nil
|
|
}
|
|
return "", errors.New("marshal to json failed.").AtError()
|
|
}
|
|
|
|
func mergeConfigs(files []string, formats []string) (*conf.Config, error) {
|
|
cf := &conf.Config{}
|
|
for i, file := range files {
|
|
errors.LogInfo(context.Background(), "Reading config: ", file)
|
|
r, err := confloader.LoadConfig(file)
|
|
if err != nil {
|
|
return nil, errors.New("failed to read config: ", file).Base(err)
|
|
}
|
|
c, err := ReaderDecoderByFormat[formats[i]](r)
|
|
if err != nil {
|
|
return nil, errors.New("failed to decode config: ", file).Base(err)
|
|
}
|
|
if i == 0 {
|
|
*cf = *c
|
|
continue
|
|
}
|
|
cf.Override(c, file)
|
|
}
|
|
return cf, nil
|
|
}
|
|
|
|
func BuildConfig(files []string, formats []string) (*core.Config, error) {
|
|
config, err := mergeConfigs(files, formats)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return config.Build()
|
|
}
|
|
|
|
type readerDecoder func(io.Reader) (*conf.Config, error)
|
|
|
|
var ReaderDecoderByFormat = make(map[string]readerDecoder)
|
|
|
|
func init() {
|
|
ReaderDecoderByFormat["json"] = DecodeJSONConfig
|
|
ReaderDecoderByFormat["yaml"] = DecodeYAMLConfig
|
|
ReaderDecoderByFormat["toml"] = DecodeTOMLConfig
|
|
|
|
core.ConfigBuilderForFiles = BuildConfig
|
|
core.ConfigMergedFormFiles = MergeConfigFromFiles
|
|
}
|