2021-09-04 10:21:58 -04:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2021-09-04 14:22:41 -04:00
|
|
|
"bytes"
|
2021-09-05 10:08:34 -04:00
|
|
|
"context"
|
2021-10-28 06:34:19 -04:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-09-04 14:22:41 -04:00
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-10-28 06:34:19 -04:00
|
|
|
protov2 "google.golang.org/protobuf/proto"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protoext"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protofilter"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
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-07 09:48:44 -04:00
|
|
|
interfaceType := opt.GetType()
|
|
|
|
for _, v := range interfaceType {
|
|
|
|
i.registerSingleImplementation(v, name, opt, loader)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *implementationRegistry) registerSingleImplementation(interfaceType, name string, opt *protoext.MessageOpt, loader CustomLoader) {
|
2021-09-04 10:21:58 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-09-04 14:22:41 -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
|
|
|
}
|
2021-09-04 14:22:41 -04:00
|
|
|
return implSet.findImplementationByAlias(alias)
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:08:34 -04:00
|
|
|
func (i *implementationRegistry) LoadImplementationByAlias(ctx context.Context, interfaceType, alias string, data []byte) (proto.Message, error) {
|
2021-09-04 14:22:41 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-05 10:08:34 -04:00
|
|
|
implementationConfigInstancev2 := proto.MessageV2(implementationConfigInstance)
|
|
|
|
if err := protofilter.FilterProtoConfig(ctx, implementationConfigInstancev2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-04 14:22:41 -04:00
|
|
|
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 19:46:25 -04:00
|
|
|
var initialized = &sync.Once{}
|
|
|
|
|
|
|
|
type registerRequest struct {
|
|
|
|
proto interface{}
|
|
|
|
loader CustomLoader
|
|
|
|
}
|
|
|
|
|
|
|
|
var registerRequests []registerRequest
|
|
|
|
|
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
|
2021-09-04 19:46:25 -04:00
|
|
|
func RegisterImplementation(proto interface{}, loader CustomLoader) error {
|
|
|
|
registerRequests = append(registerRequests, registerRequest{
|
|
|
|
proto: proto,
|
|
|
|
loader: loader,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerImplementation(proto interface{}, loader CustomLoader) error {
|
|
|
|
protoReflect := reflect.New(reflect.TypeOf(proto).Elem())
|
2021-11-27 01:32:07 -05:00
|
|
|
proto2 := protoReflect.Interface().(protov2.Message)
|
2021-09-04 19:46:25 -04:00
|
|
|
msgDesc := proto2.ProtoReflect().Descriptor()
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-04 14:22:41 -04:00
|
|
|
type LoadByAlias interface {
|
2021-09-05 10:08:34 -04:00
|
|
|
LoadImplementationByAlias(ctx context.Context, interfaceType, alias string, data []byte) (proto.Message, error)
|
2021-09-04 14:22:41 -04:00
|
|
|
}
|
|
|
|
|
2021-09-05 10:08:34 -04:00
|
|
|
func LoadImplementationByAlias(ctx context.Context, interfaceType, alias string, data []byte) (proto.Message, error) {
|
2021-09-04 19:46:25 -04:00
|
|
|
initialized.Do(func() {
|
|
|
|
for _, v := range registerRequests {
|
|
|
|
registerImplementation(v.proto, v.loader)
|
|
|
|
}
|
|
|
|
})
|
2021-09-05 10:08:34 -04:00
|
|
|
return globalImplementationRegistry.LoadImplementationByAlias(ctx, interfaceType, alias, data)
|
2021-09-04 10:21:58 -04:00
|
|
|
}
|