handler_cache.go 2.2 KB

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