inbound.go 4.3 KB

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