mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
Fix: load auto format config with io.Reader (#1773)
This commit is contained in:
parent
e4cacab9d1
commit
5157e5fffe
39
config.go
39
config.go
@ -135,18 +135,38 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
|
|||||||
// * string of a single filename/url(s) to open to read
|
// * string of a single filename/url(s) to open to read
|
||||||
// * io.Reader that reads a config content (the original way)
|
// * io.Reader that reads a config content (the original way)
|
||||||
func loadSingleConfigAutoFormat(input interface{}) (*Config, error) {
|
func loadSingleConfigAutoFormat(input interface{}) (*Config, error) {
|
||||||
if file, ok := input.(cmdarg.Arg); ok {
|
switch v := input.(type) {
|
||||||
extension := getExtension(file.String())
|
case cmdarg.Arg:
|
||||||
if extension != "" {
|
return loadSingleConfigAutoFormatFromFile(v.String())
|
||||||
lowerName := strings.ToLower(extension)
|
case string:
|
||||||
if f, found := configLoaderByExt[lowerName]; found {
|
return loadSingleConfigByTryingAllLoaders(v)
|
||||||
return f.Loader(file)
|
case io.Reader:
|
||||||
}
|
data, err := buf.ReadAllToBytes(v)
|
||||||
return nil, newError("config loader not found for: ", extension).AtWarning()
|
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
|
var errorReasons strings.Builder
|
||||||
// no extension, try all loaders
|
|
||||||
for _, f := range configLoaders {
|
for _, f := range configLoaders {
|
||||||
if f.Name[0] == FormatAuto {
|
if f.Name[0] == FormatAuto {
|
||||||
continue
|
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()))
|
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()
|
return nil, newError("tried all loaders but failed when attempting to parse: ", input, ";", errorReasons.String()).AtWarning()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,8 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return loadJSONPB(bytes.NewReader(data))
|
return loadJSONPB(bytes.NewReader(data))
|
||||||
|
case []byte:
|
||||||
|
return loadJSONPB(bytes.NewReader(v))
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
data, err := buf.ReadAllToBytes(v)
|
data, err := buf.ReadAllToBytes(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -56,6 +56,8 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return loadV2JsonPb(data)
|
return loadV2JsonPb(data)
|
||||||
|
case []byte:
|
||||||
|
return loadV2JsonPb(v)
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
data, err := buf.ReadAllToBytes(v)
|
data, err := buf.ReadAllToBytes(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -40,6 +40,8 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return loadJSONConfig(data)
|
return loadJSONConfig(data)
|
||||||
|
case []byte:
|
||||||
|
return loadJSONConfig(v)
|
||||||
case io.Reader:
|
case io.Reader:
|
||||||
data, err := buf.ReadAllToBytes(&json.Reader{
|
data, err := buf.ReadAllToBytes(&json.Reader{
|
||||||
Reader: v,
|
Reader: v,
|
||||||
|
Loading…
Reference in New Issue
Block a user