inbound.go 4.3 KB

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