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

47 lines
1.1 KiB

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