| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package internet
- import (
- "v2ray.com/core/common"
- "v2ray.com/core/common/buf"
- )
- type Authenticator interface {
- Seal(*buf.Buffer)
- Open(*buf.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 (v *AuthenticatorChain) Overhead() int {
- total := 0
- for _, auth := range v.authenticators {
- total += auth.Overhead()
- }
- return total
- }
- func (v *AuthenticatorChain) Open(payload *buf.Buffer) bool {
- for _, auth := range v.authenticators {
- if !auth.Open(payload) {
- return false
- }
- }
- return true
- }
- func (v *AuthenticatorChain) Seal(payload *buf.Buffer) {
- for i := len(v.authenticators) - 1; i >= 0; i-- {
- auth := v.authenticators[i]
- auth.Seal(payload)
- }
- }
|