conn_authenticator.go 859 B

1234567891011121314151617181920212223242526272829303132333435
  1. package internet
  2. import (
  3. "io"
  4. "v2ray.com/core/common"
  5. )
  6. type ConnectionAuthenticator interface {
  7. Seal(io.Writer) io.Writer
  8. Open(io.Reader) (io.Reader, error)
  9. }
  10. type ConnectionAuthenticatorFactory interface {
  11. Create(interface{}) ConnectionAuthenticator
  12. }
  13. var (
  14. connectionAuthenticatorCache = make(map[string]ConnectionAuthenticatorFactory)
  15. )
  16. func RegisterConnectionAuthenticator(name string, factory ConnectionAuthenticatorFactory) error {
  17. if _, found := connectionAuthenticatorCache[name]; found {
  18. return common.ErrDuplicatedName
  19. }
  20. connectionAuthenticatorCache[name] = factory
  21. return nil
  22. }
  23. func CreateConnectionAuthenticator(name string, config interface{}) (ConnectionAuthenticator, error) {
  24. factory, found := connectionAuthenticatorCache[name]
  25. if !found {
  26. return nil, common.ErrObjectNotFound
  27. }
  28. return factory.Create(config), nil
  29. }