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

65 lines
1.6 KiB
Go
Raw Normal View History

package serial
import (
2021-09-20 13:00:55 +00:00
"io"
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); ok {
return j, nil
}
return "", newError("marshal to json failed.").AtError()
}
func mergeConfigs(files []string, formats []string) (*conf.Config, error) {
cf := &conf.Config{}
for i, file := range files {
newError("Reading config: ", file).AtInfo().WriteToLog()
r, err := confloader.LoadConfig(file)
if err != nil {
return nil, newError("failed to read config: ", file).Base(err)
}
c, err := ReaderDecoderByFormat[formats[i]](r)
if err != nil {
return nil, newError("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)
2021-10-19 16:57:14 +00:00
var ReaderDecoderByFormat = make(map[string]readerDecoder)
func init() {
ReaderDecoderByFormat["json"] = DecodeJSONConfig
ReaderDecoderByFormat["yaml"] = DecodeYAMLConfig
ReaderDecoderByFormat["toml"] = DecodeTOMLConfig
core.ConfigBuilderForFiles = BuildConfig
core.ConfigMergedFormFiles = MergeConfigFromFiles
}