2016-08-06 15:59:22 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2016-09-21 08:39:07 -04:00
|
|
|
"errors"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/alloc"
|
2016-09-21 08:39:07 -04:00
|
|
|
"v2ray.com/core/common/loader"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2016-08-06 15:59:22 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Authenticator interface {
|
|
|
|
Seal(*alloc.Buffer)
|
|
|
|
Open(*alloc.Buffer) bool
|
|
|
|
Overhead() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticatorFactory interface {
|
2016-09-21 08:39:07 -04:00
|
|
|
Create(interface{}) Authenticator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthenticatorConfig) GetInternalConfig() (interface{}, error) {
|
2016-10-12 16:43:17 -04:00
|
|
|
config, err := configCache.CreateConfig(this.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := ptypes.UnmarshalAny(this.Settings, config.(proto.Message)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config, nil
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
|
2016-09-21 08:39:07 -04:00
|
|
|
func NewAuthenticatorConfig(name string, config interface{}) (*AuthenticatorConfig, error) {
|
|
|
|
pbMsg, ok := config.(proto.Message)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Internet|Authenticator: Failed to convert config into proto message.")
|
|
|
|
}
|
|
|
|
anyConfig, err := ptypes.MarshalAny(pbMsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &AuthenticatorConfig{
|
|
|
|
Name: name,
|
|
|
|
Settings: anyConfig,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthenticatorConfig) CreateAuthenticator() (Authenticator, error) {
|
|
|
|
config, err := this.GetInternalConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return CreateAuthenticator(this.Name, config)
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
authenticatorCache = make(map[string]AuthenticatorFactory)
|
2016-09-21 08:39:07 -04:00
|
|
|
configCache = loader.ConfigCreatorCache{}
|
2016-08-06 15:59:22 -04:00
|
|
|
)
|
|
|
|
|
2016-08-25 07:25:59 -04:00
|
|
|
func RegisterAuthenticator(name string, factory AuthenticatorFactory) error {
|
2016-08-06 15:59:22 -04:00
|
|
|
if _, found := authenticatorCache[name]; found {
|
2016-08-18 02:34:21 -04:00
|
|
|
return common.ErrDuplicatedName
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
authenticatorCache[name] = factory
|
2016-08-25 07:25:59 -04:00
|
|
|
return nil
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
|
2016-09-21 08:39:07 -04:00
|
|
|
func RegisterAuthenticatorConfig(name string, configCreator loader.ConfigCreator) error {
|
|
|
|
return configCache.RegisterCreator(name, configCreator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAuthenticator(name string, config interface{}) (Authenticator, error) {
|
2016-08-06 15:59:22 -04:00
|
|
|
factory, found := authenticatorCache[name]
|
|
|
|
if !found {
|
2016-08-18 02:34:21 -04:00
|
|
|
return nil, common.ErrObjectNotFound
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
2016-08-08 16:47:59 -04:00
|
|
|
return factory.Create(config), nil
|
2016-08-06 15:59:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthenticatorChain struct {
|
|
|
|
authenticators []Authenticator
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuthenticatorChain(auths ...Authenticator) Authenticator {
|
|
|
|
return &AuthenticatorChain{
|
|
|
|
authenticators: auths,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthenticatorChain) Overhead() int {
|
|
|
|
total := 0
|
|
|
|
for _, auth := range this.authenticators {
|
|
|
|
total += auth.Overhead()
|
|
|
|
}
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthenticatorChain) Open(payload *alloc.Buffer) bool {
|
|
|
|
for _, auth := range this.authenticators {
|
|
|
|
if !auth.Open(payload) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AuthenticatorChain) Seal(payload *alloc.Buffer) {
|
|
|
|
for i := len(this.authenticators) - 1; i >= 0; i-- {
|
|
|
|
auth := this.authenticators[i]
|
|
|
|
auth.Seal(payload)
|
|
|
|
}
|
|
|
|
}
|