registry.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package registry
  2. import (
  3. "bytes"
  4. "github.com/golang/protobuf/jsonpb"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/v2fly/v2ray-core/v4/common/protoext"
  7. "github.com/v2fly/v2ray-core/v4/common/serial"
  8. protov2 "google.golang.org/protobuf/proto"
  9. "reflect"
  10. "strings"
  11. "sync"
  12. )
  13. type implementationRegistry struct {
  14. implSet map[string]*implementationSet
  15. }
  16. func (i *implementationRegistry) RegisterImplementation(name string, opt *protoext.MessageOpt, loader CustomLoader) {
  17. interfaceType := opt.GetType()[0]
  18. implSet, found := i.implSet[interfaceType]
  19. if !found {
  20. implSet = newImplementationSet()
  21. i.implSet[interfaceType] = implSet
  22. }
  23. implSet.RegisterImplementation(name, opt, loader)
  24. }
  25. func (i *implementationRegistry) findImplementationByAlias(interfaceType, alias string) (string, CustomLoader, error) {
  26. implSet, found := i.implSet[interfaceType]
  27. if !found {
  28. return "", nil, newError("cannot find implemention unknown interface type")
  29. }
  30. return implSet.findImplementationByAlias(alias)
  31. }
  32. func (i *implementationRegistry) LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error) {
  33. var implementationFullName string
  34. if strings.HasPrefix(alias, "#") {
  35. // skip resolution for full name
  36. implementationFullName = alias
  37. } else {
  38. registryResult, customLoader, err := i.findImplementationByAlias(interfaceType, alias)
  39. if err != nil {
  40. return nil, newError("unable to find implementation").Base(err)
  41. }
  42. if customLoader != nil {
  43. return customLoader(data, i)
  44. }
  45. implementationFullName = registryResult
  46. }
  47. implementationConfigInstance, err := serial.GetInstance(implementationFullName)
  48. if err != nil {
  49. return nil, newError("unable to create implementation config instance").Base(err)
  50. }
  51. unmarshaler := jsonpb.Unmarshaler{AllowUnknownFields: false}
  52. err = unmarshaler.Unmarshal(bytes.NewReader(data), implementationConfigInstance.(proto.Message))
  53. if err != nil {
  54. return nil, newError("unable to parse json content").Base(err)
  55. }
  56. return implementationConfigInstance.(proto.Message), nil
  57. }
  58. func newImplementationRegistry() *implementationRegistry {
  59. return &implementationRegistry{implSet: map[string]*implementationSet{}}
  60. }
  61. var globalImplementationRegistry = newImplementationRegistry()
  62. var initialized = &sync.Once{}
  63. type registerRequest struct {
  64. proto interface{}
  65. loader CustomLoader
  66. }
  67. var registerRequests []registerRequest
  68. // RegisterImplementation register an implementation of a type of interface
  69. // loader(CustomLoader) is a private API, its interface is subject to breaking changes
  70. func RegisterImplementation(proto interface{}, loader CustomLoader) error {
  71. registerRequests = append(registerRequests, registerRequest{
  72. proto: proto,
  73. loader: loader,
  74. })
  75. return nil
  76. }
  77. func registerImplementation(proto interface{}, loader CustomLoader) error {
  78. protoReflect := reflect.New(reflect.TypeOf(proto).Elem())
  79. var proto2 protov2.Message
  80. assignMessage := func(msg protov2.Message) {
  81. proto2 = msg
  82. }
  83. reflect.ValueOf(assignMessage).Call([]reflect.Value{protoReflect})
  84. msgDesc := proto2.ProtoReflect().Descriptor()
  85. fullName := string(msgDesc.FullName())
  86. msgOpts, err := protoext.GetMessageOptions(msgDesc)
  87. if err != nil {
  88. return newError("unable to find message options").Base(err)
  89. }
  90. globalImplementationRegistry.RegisterImplementation(fullName, msgOpts, loader)
  91. return nil
  92. }
  93. type LoadByAlias interface {
  94. LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error)
  95. }
  96. func LoadImplementationByAlias(interfaceType, alias string, data []byte) (proto.Message, error) {
  97. initialized.Do(func() {
  98. for _, v := range registerRequests {
  99. registerImplementation(v.proto, v.loader)
  100. }
  101. })
  102. return globalImplementationRegistry.LoadImplementationByAlias(interfaceType, alias, data)
  103. }