1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 02:46:33 -05:00
v2fly/common/registry/registry.go

92 lines
3.0 KiB
Go
Raw Normal View History

2021-09-04 10:21:58 -04:00
package registry
import (
"bytes"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
2021-09-04 10:21:58 -04:00
"github.com/v2fly/v2ray-core/v4/common/protoext"
"github.com/v2fly/v2ray-core/v4/common/serial"
"google.golang.org/protobuf/reflect/protoreflect"
"strings"
2021-09-04 10:21:58 -04:00
)
type implementationRegistry struct {
implSet map[string]*implementationSet
}
2021-09-04 14:06:49 -04:00
func (i *implementationRegistry) RegisterImplementation(name string, opt *protoext.MessageOpt, loader CustomLoader) {
2021-09-04 10:21:58 -04:00
interfaceType := opt.GetType()[0]
implSet, found := i.implSet[interfaceType]
if !found {
implSet = newImplementationSet()
i.implSet[interfaceType] = implSet
}
2021-09-04 14:06:49 -04:00
implSet.RegisterImplementation(name, opt, loader)
2021-09-04 10:21:58 -04:00
}
func (i *implementationRegistry) findImplementationByAlias(interfaceType, alias string) (string, CustomLoader, error) {
2021-09-04 10:21:58 -04:00
implSet, found := i.implSet[interfaceType]
if !found {
2021-09-04 14:06:49 -04:00
return "", nil, newError("cannot find implemention unknown interface type")
2021-09-04 10:21:58 -04:00
}
return implSet.findImplementationByAlias(alias)
}
func (i *implementationRegistry) LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error) {
var implementationFullName string
if strings.HasPrefix(alias, "#") {
// skip resolution for full name
implementationFullName = alias
} else {
registryResult, customLoader, err := i.findImplementationByAlias(interfaceType, alias)
if err != nil {
return nil, newError("unable to find implementation").Base(err)
}
if customLoader != nil {
return customLoader(data, i)
}
implementationFullName = registryResult
}
implementationConfigInstance, err := serial.GetInstance(implementationFullName)
if err != nil {
return nil, newError("unable to create implementation config instance").Base(err)
}
unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: false}
err = unmarshaler.Unmarshal(bytes.NewReader(data), implementationConfigInstance.(proto.Message))
if err != nil {
return nil, newError("unable to parse json content").Base(err)
}
return implementationConfigInstance.(proto.Message), nil
2021-09-04 10:21:58 -04:00
}
func newImplementationRegistry() *implementationRegistry {
return &implementationRegistry{implSet: map[string]*implementationSet{}}
}
var globalImplementationRegistry = newImplementationRegistry()
2021-09-04 14:06:49 -04:00
// RegisterImplementation register an implementation of a type of interface
// loader(CustomLoader) is a private API, its interface is subject to breaking changes
func RegisterImplementation(proto protoreflect.MessageDescriptor, loader CustomLoader) error {
msgDesc := proto
2021-09-04 10:21:58 -04:00
fullName := string(msgDesc.FullName())
msgOpts, err := protoext.GetMessageOptions(msgDesc)
if err != nil {
return newError("unable to find message options").Base(err)
}
2021-09-04 14:06:49 -04:00
globalImplementationRegistry.RegisterImplementation(fullName, msgOpts, loader)
2021-09-04 10:21:58 -04:00
return nil
}
type LoadByAlias interface {
LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error)
}
func LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error) {
return globalImplementationRegistry.LoadImplementationByAlias(interfaceType, alias, data)
2021-09-04 10:21:58 -04:00
}