You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/loader.go

43 lines
1.1 KiB

package core
import (
"io"
"io/ioutil"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/errors"
)
// ConfigLoader is an utility to load V2Ray config from external source.
type ConfigLoader func(input io.Reader) (*Config, error)
var configLoaderCache = make(map[ConfigFormat]ConfigLoader)
// RegisterConfigLoader add a new ConfigLoader.
func RegisterConfigLoader(format ConfigFormat, loader ConfigLoader) error {
configLoaderCache[format] = loader
return nil
}
// LoadConfig loads config with given format from given source.
func LoadConfig(format ConfigFormat, input io.Reader) (*Config, error) {
loader, found := configLoaderCache[format]
if !found {
return nil, errors.New("Core: ", ConfigFormat_name[int32(format)], " is not loadable.")
}
return loader(input)
}
func loadProtobufConfig(input io.Reader) (*Config, error) {
config := new(Config)
data, _ := ioutil.ReadAll(input)
if err := proto.Unmarshal(data, config); err != nil {
return nil, err
}
return config, nil
}
func init() {
RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig)
}