proxy.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "github.com/v2ray/v2ray-core/proxy/internal/config"
  9. )
  10. var (
  11. inboundFactories = make(map[string]internal.InboundConnectionHandlerCreator)
  12. outboundFactories = make(map[string]internal.OutboundConnectionHandlerCreator)
  13. ErrorProxyNotFound = errors.New("Proxy not found.")
  14. ErrorNameExists = errors.New("Proxy with the same name already exists.")
  15. ErrorBadConfiguration = errors.New("Bad proxy configuration.")
  16. )
  17. func RegisterInboundConnectionHandlerFactory(name string, creator internal.InboundConnectionHandlerCreator) error {
  18. if _, found := inboundFactories[name]; found {
  19. return ErrorNameExists
  20. }
  21. inboundFactories[name] = creator
  22. return nil
  23. }
  24. func RegisterOutboundConnectionHandlerFactory(name string, creator internal.OutboundConnectionHandlerCreator) error {
  25. if _, found := outboundFactories[name]; found {
  26. return ErrorNameExists
  27. }
  28. outboundFactories[name] = creator
  29. return nil
  30. }
  31. func CreateInboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.InboundConnectionHandler, error) {
  32. creator, found := inboundFactories[name]
  33. if !found {
  34. return nil, ErrorProxyNotFound
  35. }
  36. if len(rawConfig) > 0 {
  37. proxyConfig, err := config.CreateInboundConnectionConfig(name, rawConfig)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return creator(space, proxyConfig)
  42. }
  43. return creator(space, nil)
  44. }
  45. func CreateOutboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.OutboundConnectionHandler, error) {
  46. creator, found := outboundFactories[name]
  47. if !found {
  48. return nil, ErrorNameExists
  49. }
  50. if len(rawConfig) > 0 {
  51. proxyConfig, err := config.CreateOutboundConnectionConfig(name, rawConfig)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return creator(space, proxyConfig)
  56. }
  57. return creator(space, nil)
  58. }