inbound.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.FromContext(ctx)
  23. if v == nil {
  24. return nil, newError("V is not in context")
  25. }
  26. if err := v.RegisterFeature((*core.InboundHandlerManager)(nil), m); err != nil {
  27. return nil, newError("unable to register InboundHandlerManager").Base(err)
  28. }
  29. return m, nil
  30. }
  31. // AddHandler implements core.InboundHandlerManager.
  32. func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
  33. m.access.Lock()
  34. defer m.access.Unlock()
  35. tag := handler.Tag()
  36. if len(tag) > 0 {
  37. m.taggedHandlers[tag] = handler
  38. } else {
  39. m.untaggedHandler = append(m.untaggedHandler, handler)
  40. }
  41. if m.running {
  42. return handler.Start()
  43. }
  44. return nil
  45. }
  46. // GetHandler returns core.InboundHandlerManager.
  47. func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
  48. m.access.RLock()
  49. defer m.access.RUnlock()
  50. handler, found := m.taggedHandlers[tag]
  51. if !found {
  52. return nil, newError("handler not found: ", tag)
  53. }
  54. return handler, nil
  55. }
  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. handler.Close()
  64. delete(m.taggedHandlers, tag)
  65. return nil
  66. }
  67. return core.ErrNoClue
  68. }
  69. func (m *Manager) Start() error {
  70. m.access.Lock()
  71. defer m.access.Unlock()
  72. m.running = true
  73. for _, handler := range m.taggedHandlers {
  74. if err := handler.Start(); err != nil {
  75. return err
  76. }
  77. }
  78. for _, handler := range m.untaggedHandler {
  79. if err := handler.Start(); err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  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. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  98. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  99. if err != nil {
  100. return nil, err
  101. }
  102. proxySettings, err := config.ProxySettings.GetInstance()
  103. if err != nil {
  104. return nil, err
  105. }
  106. tag := config.Tag
  107. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  108. if !ok {
  109. receiverSettings, ok := rawReceiverSettings.(*proxyman.UnixReceiverConfig)
  110. if ok {
  111. return NewUnixInboundHandler(ctx, tag, receiverSettings, proxySettings)
  112. }
  113. return nil, newError("not a ReceiverConfig").AtError()
  114. }
  115. allocStrategy := receiverSettings.AllocationStrategy
  116. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  117. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  118. }
  119. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  120. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  121. }
  122. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  123. }
  124. func (m *Manager) asUnixReceiverConfig(ctx context.Context, config *core.InboundHandlerConfig, unixrx proxyman.UnixReceiverConfig) {
  125. return
  126. }
  127. func init() {
  128. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  129. return New(ctx, config.(*proxyman.InboundConfig))
  130. }))
  131. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  132. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  133. }))
  134. }