forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 30
/
loader.go
43 lines (35 loc) · 1.1 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package core
import (
"io"
"io/ioutil"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common"
"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() {
common.Must(RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig))
}