handler_cache.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package internal
  2. import (
  3. "errors"
  4. "github.com/v2ray/v2ray-core/app"
  5. "github.com/v2ray/v2ray-core/proxy"
  6. )
  7. var (
  8. inboundFactories = make(map[string]InboundHandlerCreator)
  9. outboundFactories = make(map[string]OutboundHandlerCreator)
  10. ErrorProxyNotFound = errors.New("Proxy not found.")
  11. ErrorNameExists = errors.New("Proxy with the same name already exists.")
  12. ErrorBadConfiguration = errors.New("Bad proxy configuration.")
  13. )
  14. func RegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) error {
  15. if _, found := inboundFactories[name]; found {
  16. return ErrorNameExists
  17. }
  18. inboundFactories[name] = creator
  19. return nil
  20. }
  21. func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) {
  22. if err := RegisterInboundHandlerCreator(name, creator); err != nil {
  23. panic(err)
  24. }
  25. }
  26. func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) error {
  27. if _, found := outboundFactories[name]; found {
  28. return ErrorNameExists
  29. }
  30. outboundFactories[name] = creator
  31. return nil
  32. }
  33. func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) {
  34. if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
  35. panic(err)
  36. }
  37. }
  38. func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
  39. creator, found := inboundFactories[name]
  40. if !found {
  41. return nil, ErrorProxyNotFound
  42. }
  43. if len(rawConfig) > 0 {
  44. proxyConfig, err := CreateInboundConfig(name, rawConfig)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return creator(space, proxyConfig, meta)
  49. }
  50. return creator(space, nil, meta)
  51. }
  52. func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
  53. creator, found := outboundFactories[name]
  54. if !found {
  55. return nil, ErrorProxyNotFound
  56. }
  57. if len(rawConfig) > 0 {
  58. proxyConfig, err := CreateOutboundConfig(name, rawConfig)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return creator(space, proxyConfig, meta)
  63. }
  64. return creator(space, nil, meta)
  65. }