mirror of https://github.com/v2ray/v2ray-core
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.
92 lines
2.0 KiB
92 lines
2.0 KiB
8 years ago
|
package conf
|
||
9 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
8 years ago
|
"errors"
|
||
9 years ago
|
|
||
8 years ago
|
"v2ray.com/core/common"
|
||
|
"v2ray.com/core/common/log"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
var (
|
||
|
ErrUnknownConfigID = errors.New("Unknown config ID.")
|
||
|
)
|
||
|
|
||
|
type ConfigCreator func() interface{}
|
||
|
|
||
|
type ConfigCreatorCache map[string]ConfigCreator
|
||
|
|
||
|
func (this ConfigCreatorCache) RegisterCreator(id string, creator ConfigCreator) error {
|
||
|
if _, found := this[id]; found {
|
||
|
return common.ErrDuplicatedName
|
||
|
}
|
||
|
|
||
|
this[id] = creator
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (this ConfigCreatorCache) CreateConfig(id string) (interface{}, error) {
|
||
|
creator, found := this[id]
|
||
|
if !found {
|
||
|
return nil, ErrUnknownConfigID
|
||
|
}
|
||
|
return creator(), nil
|
||
|
}
|
||
8 years ago
|
|
||
9 years ago
|
type JSONConfigLoader struct {
|
||
8 years ago
|
cache ConfigCreatorCache
|
||
9 years ago
|
idKey string
|
||
|
configKey string
|
||
|
}
|
||
|
|
||
8 years ago
|
func NewJSONConfigLoader(cache ConfigCreatorCache, idKey string, configKey string) *JSONConfigLoader {
|
||
9 years ago
|
return &JSONConfigLoader{
|
||
8 years ago
|
idKey: idKey,
|
||
|
configKey: configKey,
|
||
|
cache: cache,
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
func (this *JSONConfigLoader) LoadWithID(raw []byte, id string) (interface{}, error) {
|
||
8 years ago
|
creator, found := this.cache[id]
|
||
|
if !found {
|
||
|
return nil, ErrUnknownConfigID
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
config := creator()
|
||
9 years ago
|
if err := json.Unmarshal(raw, config); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return config, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
func (this *JSONConfigLoader) Load(raw []byte) (interface{}, string, error) {
|
||
9 years ago
|
var obj map[string]json.RawMessage
|
||
|
if err := json.Unmarshal(raw, &obj); err != nil {
|
||
8 years ago
|
return nil, "", err
|
||
9 years ago
|
}
|
||
|
rawID, found := obj[this.idKey]
|
||
|
if !found {
|
||
|
log.Error(this.idKey, " not found in JSON content.")
|
||
8 years ago
|
return nil, "", common.ErrObjectNotFound
|
||
9 years ago
|
}
|
||
|
var id string
|
||
9 years ago
|
if err := json.Unmarshal(rawID, &id); err != nil {
|
||
8 years ago
|
return nil, "", err
|
||
9 years ago
|
}
|
||
|
rawConfig := json.RawMessage(raw)
|
||
|
if len(this.configKey) > 0 {
|
||
|
configValue, found := obj[this.configKey]
|
||
|
if !found {
|
||
|
log.Error(this.configKey, " not found in JSON content.")
|
||
8 years ago
|
return nil, "", common.ErrObjectNotFound
|
||
9 years ago
|
}
|
||
|
rawConfig = configValue
|
||
|
}
|
||
8 years ago
|
config, err := this.LoadWithID([]byte(rawConfig), id)
|
||
|
if err != nil {
|
||
|
return nil, id, err
|
||
|
}
|
||
|
return config, id, nil
|
||
9 years ago
|
}
|