inbound.go 4.3 KB

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