inbound.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. handler.Close()
  62. delete(m.taggedHandlers, tag)
  63. return nil
  64. }
  65. return core.ErrNoClue
  66. }
  67. // Start implements common.Runnable.
  68. func (m *Manager) Start() error {
  69. m.access.Lock()
  70. defer m.access.Unlock()
  71. m.running = true
  72. for _, handler := range m.taggedHandlers {
  73. if err := handler.Start(); err != nil {
  74. return err
  75. }
  76. }
  77. for _, handler := range m.untaggedHandler {
  78. if err := handler.Start(); err != nil {
  79. return err
  80. }
  81. }
  82. return nil
  83. }
  84. // Close implements common.Closable.
  85. func (m *Manager) Close() error {
  86. m.access.Lock()
  87. defer m.access.Unlock()
  88. m.running = false
  89. for _, handler := range m.taggedHandlers {
  90. handler.Close()
  91. }
  92. for _, handler := range m.untaggedHandler {
  93. handler.Close()
  94. }
  95. return nil
  96. }
  97. // NewHandler creates a new core.InboundHandler based on the given config.
  98. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  99. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  100. if err != nil {
  101. return nil, err
  102. }
  103. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  104. if !ok {
  105. return nil, newError("not a ReceiverConfig").AtError()
  106. }
  107. proxySettings, err := config.ProxySettings.GetInstance()
  108. if err != nil {
  109. return nil, err
  110. }
  111. tag := config.Tag
  112. allocStrategy := receiverSettings.AllocationStrategy
  113. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  114. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  115. }
  116. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  117. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  118. }
  119. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  120. }
  121. func init() {
  122. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  123. return New(ctx, config.(*proxyman.InboundConfig))
  124. }))
  125. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  126. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  127. }))
  128. }