From 653bbba8434634c5ab99b59f38876461db663b41 Mon Sep 17 00:00:00 2001 From: Shelikhoo Date: Sat, 4 Sep 2021 22:55:12 +0100 Subject: [PATCH] use two-step register to register explicitly --- common/registry/registry.go | 6 +++--- common/type.go | 3 --- proxy/freedom/freedom.go | 3 +++ proxy/socks/simplified/config.go | 3 +++ 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/common/registry/registry.go b/common/registry/registry.go index a1e3c3ad0..26280b6c6 100644 --- a/common/registry/registry.go +++ b/common/registry/registry.go @@ -6,7 +6,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/v2fly/v2ray-core/v4/common/protoext" "github.com/v2fly/v2ray-core/v4/common/serial" - protov2 "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" "strings" ) @@ -71,8 +71,8 @@ var globalImplementationRegistry = newImplementationRegistry() // 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 interface{}, loader CustomLoader) error { - msgDesc := proto.(protov2.Message).ProtoReflect().Type().Descriptor() +func RegisterImplementation(proto protoreflect.MessageDescriptor, loader CustomLoader) error { + msgDesc := proto fullName := string(msgDesc.FullName()) msgOpts, err := protoext.GetMessageOptions(msgDesc) if err != nil { diff --git a/common/type.go b/common/type.go index 943ffabff..1f85b4ef1 100644 --- a/common/type.go +++ b/common/type.go @@ -2,7 +2,6 @@ package common import ( "context" - "github.com/v2fly/v2ray-core/v4/common/registry" "reflect" ) @@ -18,8 +17,6 @@ func RegisterConfig(config interface{}, configCreator ConfigCreator) error { return newError(configType.Name() + " is already registered").AtError() } typeCreatorRegistry[configType] = configCreator - - registry.RegisterImplementation(config, nil) return nil } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 6e14b3bd2..2a9422002 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -4,6 +4,7 @@ package freedom import ( "context" + "github.com/v2fly/v2ray-core/v4/common/registry" "time" core "github.com/v2fly/v2ray-core/v4" @@ -38,6 +39,8 @@ func init() { fullConfig := &Config{} return common.CreateObject(ctx, fullConfig) })) + + common.Must(registry.RegisterImplementation(new(SimplifiedConfig).ProtoReflect().Descriptor(), nil)) } // Handler handles Freedom connections. diff --git a/proxy/socks/simplified/config.go b/proxy/socks/simplified/config.go index 30dcdf4b7..109eb5c15 100644 --- a/proxy/socks/simplified/config.go +++ b/proxy/socks/simplified/config.go @@ -4,6 +4,7 @@ import ( "context" "github.com/v2fly/v2ray-core/v4/common" "github.com/v2fly/v2ray-core/v4/common/protocol" + "github.com/v2fly/v2ray-core/v4/common/registry" "github.com/v2fly/v2ray-core/v4/proxy/socks" ) @@ -17,6 +18,7 @@ func init() { } return common.CreateObject(ctx, fullServer) })) + common.Must(registry.RegisterImplementation(new(ServerConfig).ProtoReflect().Descriptor(), nil)) common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) { simplifiedClient := config.(*ClientConfig) @@ -30,4 +32,5 @@ func init() { } return common.CreateObject(ctx, fullClient) })) + common.Must(registry.RegisterImplementation(new(ClientConfig).ProtoReflect().Descriptor(), nil)) }