handler_cache.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package internal
  2. import (
  3. "errors"
  4. "github.com/v2ray/v2ray-core/app"
  5. "github.com/v2ray/v2ray-core/proxy"
  6. "github.com/v2ray/v2ray-core/proxy/internal/config"
  7. )
  8. var (
  9. inboundFactories = make(map[string]InboundConnectionHandlerCreator)
  10. outboundFactories = make(map[string]OutboundConnectionHandlerCreator)
  11. ErrorProxyNotFound = errors.New("Proxy not found.")
  12. ErrorNameExists = errors.New("Proxy with the same name already exists.")
  13. ErrorBadConfiguration = errors.New("Bad proxy configuration.")
  14. )
  15. func RegisterInboundConnectionHandlerFactory(name string, creator 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 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, rawConfig []byte) (proxy.InboundConnectionHandler, error) {
  30. creator, found := inboundFactories[name]
  31. if !found {
  32. return nil, ErrorProxyNotFound
  33. }
  34. if len(rawConfig) > 0 {
  35. proxyConfig, err := config.CreateInboundConnectionConfig(name, rawConfig)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return creator(space, proxyConfig)
  40. }
  41. return creator(space, nil)
  42. }
  43. func CreateOutboundConnectionHandler(name string, space app.Space, rawConfig []byte) (proxy.OutboundConnectionHandler, error) {
  44. creator, found := outboundFactories[name]
  45. if !found {
  46. return nil, ErrorNameExists
  47. }
  48. if len(rawConfig) > 0 {
  49. proxyConfig, err := config.CreateOutboundConnectionConfig(name, rawConfig)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return creator(space, proxyConfig)
  54. }
  55. return creator(space, nil)
  56. }