conn_authenticator.go 852 B

123456789101112131415161718192021222324252627282930313233343536
  1. package internet
  2. import (
  3. "net"
  4. "v2ray.com/core/common"
  5. )
  6. type ConnectionAuthenticator interface {
  7. Client(net.Conn) net.Conn
  8. Server(net.Conn) net.Conn
  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. }