Browse Source

use two-step register to register explicitly

Shelikhoo 4 years ago
parent
commit
653bbba843
4 changed files with 9 additions and 6 deletions
  1. 3 3
      common/registry/registry.go
  2. 0 3
      common/type.go
  3. 3 0
      proxy/freedom/freedom.go
  4. 3 0
      proxy/socks/simplified/config.go

+ 3 - 3
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 {

+ 0 - 3
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
 }
 

+ 3 - 0
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.

+ 3 - 0
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))
 }