inbound.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "sync"
  6. "v2ray.com/core"
  7. "v2ray.com/core/app/proxyman"
  8. "v2ray.com/core/common"
  9. )
  10. // Manager is to manage all inbound handlers.
  11. type Manager struct {
  12. access sync.RWMutex
  13. untaggedHandler []core.InboundHandler
  14. taggedHandlers map[string]core.InboundHandler
  15. running bool
  16. }
  17. // New returns a new Manager for inbound handlers.
  18. func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
  19. m := &Manager{
  20. taggedHandlers: make(map[string]core.InboundHandler),
  21. }
  22. v := core.MustFromContext(ctx)
  23. if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
  24. return nil, newError("unable to register InboundHandlerManager").Base(err)
  25. }
  26. return m, nil
  27. }
  28. // AddHandler implements core.InboundHandlerManager.
  29. func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
  30. m.access.Lock()
  31. defer m.access.Unlock()
  32. tag := handler.Tag()
  33. if len(tag) > 0 {
  34. m.taggedHandlers[tag] = handler
  35. } else {
  36. m.untaggedHandler = append(m.untaggedHandler, handler)
  37. }
  38. if m.running {
  39. return handler.Start()
  40. }
  41. return nil
  42. }
  43. // GetHandler implements core.InboundHandlerManager.
  44. func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
  45. m.access.RLock()
  46. defer m.access.RUnlock()
  47. handler, found := m.taggedHandlers[tag]
  48. if !found {
  49. return nil, newError("handler not found: ", tag)
  50. }
  51. return handler, nil
  52. }
  53. // RemoveHandler implements core.InboundHandlerManager.
  54. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
  55. if len(tag) == 0 {
  56. return core.ErrNoClue
  57. }
  58. m.access.Lock()
  59. defer m.access.Unlock()
  60. if handler, found := m.taggedHandlers[tag]; found {
  61. if err := handler.Close(); err != nil {
  62. newError("failed to close handler ", tag).Base(err).AtWarning().WithContext(ctx).WriteToLog()
  63. }
  64. delete(m.taggedHandlers, tag)
  65. return nil
  66. }
  67. return core.ErrNoClue
  68. }
  69. // Start implements common.Runnable.
  70. func (m *Manager) Start() error {
  71. m.access.Lock()
  72. defer m.access.Unlock()
  73. m.running = true
  74. for _, handler := range m.taggedHandlers {
  75. if err := handler.Start(); err != nil {
  76. return err
  77. }
  78. }
  79. for _, handler := range m.untaggedHandler {
  80. if err := handler.Start(); err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }
  86. // Close implements common.Closable.
  87. func (m *Manager) Close() error {
  88. m.access.Lock()
  89. defer m.access.Unlock()
  90. m.running = false
  91. for _, handler := range m.taggedHandlers {
  92. handler.Close()
  93. }
  94. for _, handler := range m.untaggedHandler {
  95. handler.Close()
  96. }
  97. return nil
  98. }
  99. // NewHandler creates a new core.InboundHandler based on the given config.
  100. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  101. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  102. if err != nil {
  103. return nil, err
  104. }
  105. proxySettings, err := config.ProxySettings.GetInstance()
  106. if err != nil {
  107. return nil, err
  108. }
  109. tag := config.Tag
  110. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  111. if !ok {
  112. receiverSettings, ok := rawReceiverSettings.(*proxyman.UnixReceiverConfig)
  113. if ok {
  114. return NewUnixInboundHandler(ctx, tag, receiverSettings, proxySettings)
  115. }
  116. return nil, newError("not a ReceiverConfig").AtError()
  117. }
  118. allocStrategy := receiverSettings.AllocationStrategy
  119. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  120. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  121. }
  122. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  123. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  124. }
  125. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  126. }
  127. func init() {
  128. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  129. return New(ctx, config.(*proxyman.InboundConfig))
  130. }))
  131. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  132. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  133. }))
  134. }