authenticator.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package internet
  2. import (
  3. "errors"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/alloc"
  6. "v2ray.com/core/common/loader"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/golang/protobuf/ptypes"
  9. )
  10. type Authenticator interface {
  11. Seal(*alloc.Buffer)
  12. Open(*alloc.Buffer) bool
  13. Overhead() int
  14. }
  15. type AuthenticatorFactory interface {
  16. Create(interface{}) Authenticator
  17. }
  18. func (this *AuthenticatorConfig) GetInternalConfig() (interface{}, error) {
  19. config, err := configCache.CreateConfig(this.Name)
  20. if err != nil {
  21. return nil, err
  22. }
  23. if err := ptypes.UnmarshalAny(this.Settings, config.(proto.Message)); err != nil {
  24. return nil, err
  25. }
  26. return config, nil
  27. }
  28. func NewAuthenticatorConfig(name string, config interface{}) (*AuthenticatorConfig, error) {
  29. pbMsg, ok := config.(proto.Message)
  30. if !ok {
  31. return nil, errors.New("Internet|Authenticator: Failed to convert config into proto message.")
  32. }
  33. anyConfig, err := ptypes.MarshalAny(pbMsg)
  34. if err != nil {
  35. return nil, err
  36. }
  37. return &AuthenticatorConfig{
  38. Name: name,
  39. Settings: anyConfig,
  40. }, nil
  41. }
  42. func (this *AuthenticatorConfig) CreateAuthenticator() (Authenticator, error) {
  43. config, err := this.GetInternalConfig()
  44. if err != nil {
  45. return nil, err
  46. }
  47. return CreateAuthenticator(this.Name, config)
  48. }
  49. var (
  50. authenticatorCache = make(map[string]AuthenticatorFactory)
  51. configCache = loader.ConfigCreatorCache{}
  52. )
  53. func RegisterAuthenticator(name string, factory AuthenticatorFactory) error {
  54. if _, found := authenticatorCache[name]; found {
  55. return common.ErrDuplicatedName
  56. }
  57. authenticatorCache[name] = factory
  58. return nil
  59. }
  60. func RegisterAuthenticatorConfig(name string, configCreator loader.ConfigCreator) error {
  61. return configCache.RegisterCreator(name, configCreator)
  62. }
  63. func CreateAuthenticator(name string, config interface{}) (Authenticator, error) {
  64. factory, found := authenticatorCache[name]
  65. if !found {
  66. return nil, common.ErrObjectNotFound
  67. }
  68. return factory.Create(config), nil
  69. }
  70. type AuthenticatorChain struct {
  71. authenticators []Authenticator
  72. }
  73. func NewAuthenticatorChain(auths ...Authenticator) Authenticator {
  74. return &AuthenticatorChain{
  75. authenticators: auths,
  76. }
  77. }
  78. func (this *AuthenticatorChain) Overhead() int {
  79. total := 0
  80. for _, auth := range this.authenticators {
  81. total += auth.Overhead()
  82. }
  83. return total
  84. }
  85. func (this *AuthenticatorChain) Open(payload *alloc.Buffer) bool {
  86. for _, auth := range this.authenticators {
  87. if !auth.Open(payload) {
  88. return false
  89. }
  90. }
  91. return true
  92. }
  93. func (this *AuthenticatorChain) Seal(payload *alloc.Buffer) {
  94. for i := len(this.authenticators) - 1; i >= 0; i-- {
  95. auth := this.authenticators[i]
  96. auth.Seal(payload)
  97. }
  98. }