forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
70 lines (59 loc) · 1.43 KB
/
authenticator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package internet
import (
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
)
type Authenticator interface {
Seal(*alloc.Buffer)
Open(*alloc.Buffer) bool
Overhead() int
}
type AuthenticatorFactory interface {
Create(interface{}) Authenticator
}
var (
authenticatorCache = make(map[string]AuthenticatorFactory)
)
func RegisterAuthenticator(name string, factory AuthenticatorFactory) error {
if _, found := authenticatorCache[name]; found {
return common.ErrDuplicatedName
}
authenticatorCache[name] = factory
return nil
}
func CreateAuthenticator(name string, config interface{}) (Authenticator, error) {
factory, found := authenticatorCache[name]
if !found {
return nil, common.ErrObjectNotFound
}
return factory.Create(config), nil
}
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)
}
}