v2ray-core/proxy/registry/config_cache.go

51 lines
1.3 KiB
Go
Raw Normal View History

package registry
2016-06-10 20:26:39 +00:00
2016-10-13 09:14:24 +00:00
import (
"v2ray.com/core/common/loader"
2016-06-10 20:26:39 +00:00
2016-10-13 09:14:24 +00:00
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
)
2016-09-21 12:39:07 +00:00
2016-10-13 09:14:24 +00:00
var (
inboundConfigCreatorCache = loader.ConfigCreatorCache{}
2016-09-21 12:39:07 +00:00
outboundConfigCreatorCache = loader.ConfigCreatorCache{}
2016-06-10 20:26:39 +00:00
)
func RegisterInboundConfig(protocol string, creator loader.ConfigCreator) error {
2016-09-21 12:39:07 +00:00
return inboundConfigCreatorCache.RegisterCreator(protocol, creator)
2016-06-10 20:26:39 +00:00
}
func RegisterOutboundConfig(protocol string, creator loader.ConfigCreator) error {
2016-09-21 12:39:07 +00:00
return outboundConfigCreatorCache.RegisterCreator(protocol, creator)
2016-06-10 20:26:39 +00:00
}
2016-10-13 09:14:24 +00:00
func MarshalInboundConfig(protocol string, settings *any.Any) (interface{}, error) {
config, err := inboundConfigCreatorCache.CreateConfig(protocol)
if err != nil {
return nil, err
}
2016-10-14 19:19:45 +00:00
if settings == nil {
return config, nil
}
2016-10-13 09:14:24 +00:00
if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
return nil, err
}
return config, nil
2016-06-10 20:26:39 +00:00
}
2016-10-13 09:14:24 +00:00
func MarshalOutboundConfig(protocol string, settings *any.Any) (interface{}, error) {
config, err := outboundConfigCreatorCache.CreateConfig(protocol)
if err != nil {
return nil, err
}
2016-10-14 19:19:45 +00:00
if settings == nil {
return config, nil
}
2016-10-13 09:14:24 +00:00
if err := ptypes.UnmarshalAny(settings, config.(proto.Message)); err != nil {
return nil, err
}
return config, nil
2016-06-10 20:26:39 +00:00
}