From e4e59f7302356f5874d8ff768bb93fca6a4ddf35 Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Fri, 16 Aug 2024 11:21:16 +1000 Subject: [PATCH 1/2] refactor file & format array to an struct array --- core/config.go | 30 +++++++++++++++++++----------- infra/conf/serial/builder.go | 16 ++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/core/config.go b/core/config.go index aa9b63c93be9..1ee70a2baba7 100644 --- a/core/config.go +++ b/core/config.go @@ -19,14 +19,19 @@ type ConfigFormat struct { Loader ConfigLoader } +type ConfigSource struct { + File string + Format string +} + // ConfigLoader is a utility to load Xray config from external source. type ConfigLoader func(input interface{}) (*Config, error) // ConfigBuilder is a builder to build core.Config from filenames and formats -type ConfigBuilder func(files []string, formats []string) (*Config, error) +type ConfigBuilder func(files []*ConfigSource) (*Config, error) // ConfigsMerger merge multiple json configs into on config -type ConfigsMerger func(files []string, formats []string) (string, error) +type ConfigsMerger func(files []*ConfigSource) (string, error) var ( configLoaderByName = make(map[string]*ConfigFormat) @@ -55,20 +60,21 @@ func RegisterConfigLoader(format *ConfigFormat) error { } func GetMergedConfig(args cmdarg.Arg) (string, error) { - files := make([]string, 0) - formats := make([]string, 0) + var files []*ConfigSource supported := []string{"json", "yaml", "toml"} for _, file := range args { format := getFormat(file) for _, s := range supported { if s == format { - files = append(files, file) - formats = append(formats, format) + files = append(files, &ConfigSource{ + File: file, + Format: format, + }) break } } } - return ConfigMergedFormFiles(files, formats) + return ConfigMergedFormFiles(files) } func GetFormatByExtension(ext string) string { @@ -101,7 +107,7 @@ func getFormat(filename string) string { func LoadConfig(formatName string, input interface{}) (*Config, error) { switch v := input.(type) { case cmdarg.Arg: - formats := make([]string, len(v)) + sources := make([]*ConfigSource, len(v)) hasProtobuf := false for i, file := range v { var f string @@ -123,7 +129,10 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) { if f == "protobuf" { hasProtobuf = true } - formats[i] = f + sources[i] = &ConfigSource{ + File: file, + Format: f, + } } // only one protobuf config file is allowed @@ -136,8 +145,7 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) { } // to avoid import cycle - return ConfigBuilderForFiles(v, formats) - + return ConfigBuilderForFiles(sources) case io.Reader: if f, found := configLoaderByName[formatName]; found { return f.Loader(v) diff --git a/infra/conf/serial/builder.go b/infra/conf/serial/builder.go index a51648a86da8..3ef6d15b6b26 100644 --- a/infra/conf/serial/builder.go +++ b/infra/conf/serial/builder.go @@ -11,8 +11,8 @@ import ( "github.com/xtls/xray-core/main/confloader" ) -func MergeConfigFromFiles(files []string, formats []string) (string, error) { - c, err := mergeConfigs(files, formats) +func MergeConfigFromFiles(files []*core.ConfigSource) (string, error) { + c, err := mergeConfigs(files) if err != nil { return "", err } @@ -23,15 +23,15 @@ func MergeConfigFromFiles(files []string, formats []string) (string, error) { return "", errors.New("marshal to json failed.").AtError() } -func mergeConfigs(files []string, formats []string) (*conf.Config, error) { +func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) { cf := &conf.Config{} for i, file := range files { errors.LogInfo(context.Background(), "Reading config: ", file) - r, err := confloader.LoadConfig(file) + r, err := confloader.LoadConfig(file.File) if err != nil { return nil, errors.New("failed to read config: ", file).Base(err) } - c, err := ReaderDecoderByFormat[formats[i]](r) + c, err := ReaderDecoderByFormat[file.Format](r) if err != nil { return nil, errors.New("failed to decode config: ", file).Base(err) } @@ -39,13 +39,13 @@ func mergeConfigs(files []string, formats []string) (*conf.Config, error) { *cf = *c continue } - cf.Override(c, file) + cf.Override(c, file.File) } return cf, nil } -func BuildConfig(files []string, formats []string) (*core.Config, error) { - config, err := mergeConfigs(files, formats) +func BuildConfig(files []*core.ConfigSource) (*core.Config, error) { + config, err := mergeConfigs(files) if err != nil { return nil, err } From 2bf29660bc759f34e83ada2da75f16f1d2f83dec Mon Sep 17 00:00:00 2001 From: LiYuqing Date: Fri, 16 Aug 2024 11:25:30 +1000 Subject: [PATCH 2/2] Fix typo --- core/config.go | 12 ++++++------ infra/conf/serial/builder.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/config.go b/core/config.go index 1ee70a2baba7..e101c9205e51 100644 --- a/core/config.go +++ b/core/config.go @@ -20,7 +20,7 @@ type ConfigFormat struct { } type ConfigSource struct { - File string + Name string Format string } @@ -67,7 +67,7 @@ func GetMergedConfig(args cmdarg.Arg) (string, error) { for _, s := range supported { if s == format { files = append(files, &ConfigSource{ - File: file, + Name: file, Format: format, }) break @@ -107,7 +107,7 @@ func getFormat(filename string) string { func LoadConfig(formatName string, input interface{}) (*Config, error) { switch v := input.(type) { case cmdarg.Arg: - sources := make([]*ConfigSource, len(v)) + files := make([]*ConfigSource, len(v)) hasProtobuf := false for i, file := range v { var f string @@ -129,8 +129,8 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) { if f == "protobuf" { hasProtobuf = true } - sources[i] = &ConfigSource{ - File: file, + files[i] = &ConfigSource{ + Name: file, Format: f, } } @@ -145,7 +145,7 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) { } // to avoid import cycle - return ConfigBuilderForFiles(sources) + return ConfigBuilderForFiles(files) case io.Reader: if f, found := configLoaderByName[formatName]; found { return f.Loader(v) diff --git a/infra/conf/serial/builder.go b/infra/conf/serial/builder.go index 3ef6d15b6b26..3ae980251ea5 100644 --- a/infra/conf/serial/builder.go +++ b/infra/conf/serial/builder.go @@ -27,7 +27,7 @@ func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) { cf := &conf.Config{} for i, file := range files { errors.LogInfo(context.Background(), "Reading config: ", file) - r, err := confloader.LoadConfig(file.File) + r, err := confloader.LoadConfig(file.Name) if err != nil { return nil, errors.New("failed to read config: ", file).Base(err) } @@ -39,7 +39,7 @@ func mergeConfigs(files []*core.ConfigSource) (*conf.Config, error) { *cf = *c continue } - cf.Override(c, file.File) + cf.Override(c, file.Name) } return cf, nil }