inbound.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package inbound
  2. //go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg inbound -path App,Proxyman,Inbound
  3. import (
  4. "context"
  5. "v2ray.com/core/app/proxyman"
  6. "v2ray.com/core/common"
  7. )
  8. // Manager is to manage all inbound handlers.
  9. type Manager struct {
  10. handlers []proxyman.InboundHandler
  11. taggedHandlers map[string]proxyman.InboundHandler
  12. }
  13. func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
  14. return &Manager{
  15. taggedHandlers: make(map[string]proxyman.InboundHandler),
  16. }, nil
  17. }
  18. func (m *Manager) AddHandler(ctx context.Context, config *proxyman.InboundHandlerConfig) error {
  19. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  20. if err != nil {
  21. return err
  22. }
  23. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  24. if !ok {
  25. return newError("not a ReceiverConfig").AtError()
  26. }
  27. proxySettings, err := config.ProxySettings.GetInstance()
  28. if err != nil {
  29. return err
  30. }
  31. var handler proxyman.InboundHandler
  32. tag := config.Tag
  33. allocStrategy := receiverSettings.AllocationStrategy
  34. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  35. h, err := NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  36. if err != nil {
  37. return err
  38. }
  39. handler = h
  40. } else if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  41. h, err := NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  42. if err != nil {
  43. return err
  44. }
  45. handler = h
  46. }
  47. if handler == nil {
  48. return newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  49. }
  50. m.handlers = append(m.handlers, handler)
  51. if len(tag) > 0 {
  52. m.taggedHandlers[tag] = handler
  53. }
  54. return nil
  55. }
  56. func (m *Manager) GetHandler(ctx context.Context, tag string) (proxyman.InboundHandler, error) {
  57. handler, found := m.taggedHandlers[tag]
  58. if !found {
  59. return nil, newError("handler not found: ", tag)
  60. }
  61. return handler, nil
  62. }
  63. func (m *Manager) Start() error {
  64. for _, handler := range m.handlers {
  65. if err := handler.Start(); err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func (m *Manager) Close() {
  72. for _, handler := range m.handlers {
  73. handler.Close()
  74. }
  75. }
  76. func (m *Manager) Interface() interface{} {
  77. return (*proxyman.InboundHandlerManager)(nil)
  78. }
  79. func init() {
  80. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  81. return New(ctx, config.(*proxyman.InboundConfig))
  82. }))
  83. }