1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

Fix: load auto format config with io.Reader (#1773)

This commit is contained in:
秋のかえで 2022-07-21 21:56:50 +08:00 committed by GitHub
parent e4cacab9d1
commit 5157e5fffe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 9 deletions

View File

@ -135,18 +135,38 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
// * string of a single filename/url(s) to open to read
// * io.Reader that reads a config content (the original way)
func loadSingleConfigAutoFormat(input interface{}) (*Config, error) {
if file, ok := input.(cmdarg.Arg); ok {
extension := getExtension(file.String())
if extension != "" {
lowerName := strings.ToLower(extension)
if f, found := configLoaderByExt[lowerName]; found {
return f.Loader(file)
}
return nil, newError("config loader not found for: ", extension).AtWarning()
switch v := input.(type) {
case cmdarg.Arg:
return loadSingleConfigAutoFormatFromFile(v.String())
case string:
return loadSingleConfigByTryingAllLoaders(v)
case io.Reader:
data, err := buf.ReadAllToBytes(v)
if err != nil {
return nil, err
}
return loadSingleConfigByTryingAllLoaders(data)
default:
return loadSingleConfigByTryingAllLoaders(v)
}
}
func loadSingleConfigAutoFormatFromFile(file string) (*Config, error) {
extension := getExtension(file)
if extension != "" {
lowerName := strings.ToLower(extension)
if f, found := configLoaderByExt[lowerName]; found {
return f.Loader(file)
}
return nil, newError("config loader not found for: ", extension).AtWarning()
}
return loadSingleConfigByTryingAllLoaders(file)
}
func loadSingleConfigByTryingAllLoaders(input interface{}) (*Config, error) {
var errorReasons strings.Builder
// no extension, try all loaders
for _, f := range configLoaders {
if f.Name[0] == FormatAuto {
continue
@ -157,6 +177,7 @@ func loadSingleConfigAutoFormat(input interface{}) (*Config, error) {
}
errorReasons.WriteString(fmt.Sprintf("unable to parse as %v:%v;", f.Name[0], err.Error()))
}
return nil, newError("tried all loaders but failed when attempting to parse: ", input, ";", errorReasons.String()).AtWarning()
}

View File

@ -57,6 +57,8 @@ func init() {
return nil, err
}
return loadJSONPB(bytes.NewReader(data))
case []byte:
return loadJSONPB(bytes.NewReader(v))
case io.Reader:
data, err := buf.ReadAllToBytes(v)
if err != nil {

View File

@ -56,6 +56,8 @@ func init() {
return nil, err
}
return loadV2JsonPb(data)
case []byte:
return loadV2JsonPb(v)
case io.Reader:
data, err := buf.ReadAllToBytes(v)
if err != nil {

View File

@ -40,6 +40,8 @@ func init() {
return nil, err
}
return loadJSONConfig(data)
case []byte:
return loadJSONConfig(v)
case io.Reader:
data, err := buf.ReadAllToBytes(&json.Reader{
Reader: v,