2021-02-12 14:12:58 +00:00
|
|
|
package serial
|
|
|
|
|
|
|
|
import (
|
2024-06-29 18:32:57 +00:00
|
|
|
"context"
|
2021-09-20 13:00:55 +00:00
|
|
|
"io"
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2023-12-29 16:16:48 +00:00
|
|
|
creflect "github.com/xtls/xray-core/common/reflect"
|
2021-02-12 14:12:58 +00:00
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/infra/conf"
|
|
|
|
"github.com/xtls/xray-core/main/confloader"
|
|
|
|
)
|
|
|
|
|
2024-08-16 05:32:05 +00:00
|
|
|
func MergeConfigFromFiles(files []*core.ConfigSource) (string, error) {
|
|
|
|
c, err := mergeConfigs(files)
|
2023-12-29 16:16:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-08-08 14:35:46 +00:00
|
|
|
if j, ok := creflect.MarshalToJson(c, true); ok {
|
2023-12-29 16:16:48 +00:00
|
|
|
return j, nil
|
|
|
|
}
|
2024-06-29 18:32:57 +00:00
|
|
|
return "", errors.New("marshal to json failed.").AtError()
|
2023-12-29 16:16:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 05:32:05 +00:00
|
|
|
func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) {
|
2021-02-12 14:12:58 +00:00
|
|
|
cf := &conf.Config{}
|
2023-01-07 15:11:23 +00:00
|
|
|
for i, file := range files {
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(context.Background(), "Reading config: ", file)
|
2024-08-16 05:32:05 +00:00
|
|
|
r, err := confloader.LoadConfig(file.Name)
|
2021-02-12 14:12:58 +00:00
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("failed to read config: ", file).Base(err)
|
2021-02-12 14:12:58 +00:00
|
|
|
}
|
2024-08-16 05:32:05 +00:00
|
|
|
c, err := ReaderDecoderByFormat[file.Format](r)
|
2021-02-12 14:12:58 +00:00
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("failed to decode config: ", file).Base(err)
|
2021-02-12 14:12:58 +00:00
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
*cf = *c
|
|
|
|
continue
|
|
|
|
}
|
2024-08-16 05:32:05 +00:00
|
|
|
cf.Override(c, file.Name)
|
2021-02-12 14:12:58 +00:00
|
|
|
}
|
2023-12-29 16:16:48 +00:00
|
|
|
return cf, nil
|
|
|
|
}
|
|
|
|
|
2024-08-16 05:32:05 +00:00
|
|
|
func BuildConfig(files []*core.ConfigSource) (*core.Config, error) {
|
|
|
|
config, err := mergeConfigs(files)
|
2023-12-29 16:16:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Build()
|
2021-02-12 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type readerDecoder func(io.Reader) (*conf.Config, error)
|
|
|
|
|
2021-10-19 16:57:14 +00:00
|
|
|
var ReaderDecoderByFormat = make(map[string]readerDecoder)
|
2021-02-12 14:12:58 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
ReaderDecoderByFormat["json"] = DecodeJSONConfig
|
|
|
|
ReaderDecoderByFormat["yaml"] = DecodeYAMLConfig
|
|
|
|
ReaderDecoderByFormat["toml"] = DecodeTOMLConfig
|
|
|
|
|
|
|
|
core.ConfigBuilderForFiles = BuildConfig
|
2023-12-29 16:16:48 +00:00
|
|
|
core.ConfigMergedFormFiles = MergeConfigFromFiles
|
2021-02-12 14:12:58 +00:00
|
|
|
}
|