proxy.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Package proxy contains all proxies used by V2Ray.
  2. package proxy
  3. import (
  4. "errors"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/proxy/common/connhandler"
  7. "github.com/v2ray/v2ray-core/proxy/internal"
  8. )
  9. var (
  10. inboundFactories = make(map[string]internal.InboundConnectionHandlerCreator)
  11. outboundFactories = make(map[string]internal.OutboundConnectionHandlerCreator)
  12. ErrorProxyNotFound = errors.New("Proxy not found.")
  13. ErrorNameExists = errors.New("Proxy with the same name already exists.")
  14. )
  15. func RegisterInboundConnectionHandlerFactory(name string, creator internal.InboundConnectionHandlerCreator) error {
  16. if _, found := inboundFactories[name]; found {
  17. return ErrorNameExists
  18. }
  19. inboundFactories[name] = creator
  20. return nil
  21. }
  22. func RegisterOutboundConnectionHandlerFactory(name string, creator internal.OutboundConnectionHandlerCreator) error {
  23. if _, found := outboundFactories[name]; found {
  24. return ErrorNameExists
  25. }
  26. outboundFactories[name] = creator
  27. return nil
  28. }
  29. func CreateInboundConnectionHandler(name string, space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) {
  30. if creator, found := inboundFactories[name]; !found {
  31. return nil, ErrorProxyNotFound
  32. } else {
  33. return creator(space, config)
  34. }
  35. }
  36. func CreateOutboundConnectionHandler(name string, space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) {
  37. if creator, found := outboundFactories[name]; !found {
  38. return nil, ErrorNameExists
  39. } else {
  40. return creator(space, config)
  41. }
  42. }