handler_cache.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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]InboundHandlerCreator)
  10. outboundFactories = make(map[string]OutboundHandlerCreator)
  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 RegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) error {
  16. if _, found := inboundFactories[name]; found {
  17. return ErrorNameExists
  18. }
  19. inboundFactories[name] = creator
  20. return nil
  21. }
  22. func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) {
  23. if err := RegisterInboundHandlerCreator(name, creator); err != nil {
  24. panic(err)
  25. }
  26. }
  27. func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) error {
  28. if _, found := outboundFactories[name]; found {
  29. return ErrorNameExists
  30. }
  31. outboundFactories[name] = creator
  32. return nil
  33. }
  34. func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) {
  35. if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
  36. panic(err)
  37. }
  38. }
  39. func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  40. creator, found := inboundFactories[name]
  41. if !found {
  42. return nil, ErrorProxyNotFound
  43. }
  44. if len(rawConfig) > 0 {
  45. proxyConfig, err := config.CreateInboundConfig(name, rawConfig)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return creator(space, proxyConfig, meta)
  50. }
  51. return creator(space, nil, meta)
  52. }
  53. func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  54. creator, found := outboundFactories[name]
  55. if !found {
  56. return nil, ErrorNameExists
  57. }
  58. if len(rawConfig) > 0 {
  59. proxyConfig, err := config.CreateOutboundConfig(name, rawConfig)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return creator(space, proxyConfig, meta)
  64. }
  65. return creator(space, nil, meta)
  66. }