mirror of https://github.com/v2ray/v2ray-core
parent
94125236e6
commit
2a07838bb9
@ -0,0 +1,87 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
)
|
||||
|
||||
type ConfigFormat struct {
|
||||
Name string
|
||||
Extension []string
|
||||
Loader ConfigLoader
|
||||
}
|
||||
|
||||
// ConfigLoader is an utility to load V2Ray config from external source.
|
||||
type ConfigLoader func(input io.Reader) (*Config, error)
|
||||
|
||||
var (
|
||||
configLoaderByName = make(map[string]*ConfigFormat)
|
||||
configLoaderByExt = make(map[string]*ConfigFormat)
|
||||
)
|
||||
|
||||
// RegisterConfigLoader add a new ConfigLoader.
|
||||
func RegisterConfigLoader(format *ConfigFormat) error {
|
||||
name := strings.ToLower(format.Name)
|
||||
if _, found := configLoaderByName[name]; found {
|
||||
return newError(format.Name, " already registered.")
|
||||
}
|
||||
configLoaderByName[name] = format
|
||||
|
||||
for _, ext := range format.Extension {
|
||||
lext := strings.ToLower(ext)
|
||||
if f, found := configLoaderByExt[lext]; found {
|
||||
return newError(ext, " already registered to ", f.Name)
|
||||
}
|
||||
configLoaderByExt[lext] = format
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getExtension(filename string) string {
|
||||
idx := strings.LastIndexByte(filename, '.')
|
||||
if idx == -1 {
|
||||
return ""
|
||||
}
|
||||
return filename[idx+1:]
|
||||
}
|
||||
|
||||
// LoadConfig loads config with given format from given source.
|
||||
func LoadConfig(formatName string, filename string, input io.Reader) (*Config, error) {
|
||||
ext := getExtension(filename)
|
||||
if len(ext) > 0 {
|
||||
if f, found := configLoaderByExt[ext]; found {
|
||||
return f.Loader(input)
|
||||
}
|
||||
}
|
||||
|
||||
if f, found := configLoaderByName[formatName]; found {
|
||||
return f.Loader(input)
|
||||
}
|
||||
|
||||
return nil, newError("Unable to load config in ", formatName).AtWarning()
|
||||
}
|
||||
|
||||
func loadProtobufConfig(input io.Reader) (*Config, error) {
|
||||
config := new(Config)
|
||||
data, err := buf.ReadAllToBytes(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := proto.Unmarshal(data, config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(RegisterConfigLoader(&ConfigFormat{
|
||||
Name: "Protobuf",
|
||||
Extension: []string{"pb"},
|
||||
Loader: loadProtobufConfig,
|
||||
}))
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/buf"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
// 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, newError(ConfigFormat_name[int32(format)], " is not loadable.").AtWarning()
|
||||
}
|
||||
return loader(input)
|
||||
}
|
||||
|
||||
func loadProtobufConfig(input io.Reader) (*Config, error) {
|
||||
config := new(Config)
|
||||
data, err := buf.ReadAllToBytes(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := proto.Unmarshal(data, config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(RegisterConfigLoader(ConfigFormat_Protobuf, loadProtobufConfig))
|
||||
}
|
Loading…
Reference in new issue