authenticator.go 2.5 KB

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