inbound.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
  18. m := &Manager{
  19. taggedHandlers: make(map[string]core.InboundHandler),
  20. }
  21. v := core.FromContext(ctx)
  22. if v == nil {
  23. return nil, newError("V is not in context")
  24. }
  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. 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. 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. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
  54. if len(tag) == 0 {
  55. return core.ErrNoClue
  56. }
  57. m.access.Lock()
  58. defer m.access.Unlock()
  59. if handler, found := m.taggedHandlers[tag]; found {
  60. handler.Close()
  61. delete(m.taggedHandlers, tag)
  62. return nil
  63. }
  64. return core.ErrNoClue
  65. }
  66. func (m *Manager) Start() error {
  67. m.access.Lock()
  68. defer m.access.Unlock()
  69. m.running = true
  70. for _, handler := range m.taggedHandlers {
  71. if err := handler.Start(); err != nil {
  72. return err
  73. }
  74. }
  75. for _, handler := range m.untaggedHandler {
  76. if err := handler.Start(); err != nil {
  77. return err
  78. }
  79. }
  80. return nil
  81. }
  82. func (m *Manager) Close() {
  83. m.access.Lock()
  84. defer m.access.Unlock()
  85. m.running = false
  86. for _, handler := range m.taggedHandlers {
  87. handler.Close()
  88. }
  89. for _, handler := range m.untaggedHandler {
  90. handler.Close()
  91. }
  92. }
  93. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  94. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  95. if err != nil {
  96. return nil, err
  97. }
  98. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  99. if !ok {
  100. return nil, newError("not a ReceiverConfig").AtError()
  101. }
  102. proxySettings, err := config.ProxySettings.GetInstance()
  103. if err != nil {
  104. return nil, err
  105. }
  106. tag := config.Tag
  107. allocStrategy := receiverSettings.AllocationStrategy
  108. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  109. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  110. }
  111. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  112. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  113. }
  114. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  115. }
  116. func init() {
  117. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  118. return New(ctx, config.(*proxyman.InboundConfig))
  119. }))
  120. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  121. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  122. }))
  123. }