inbound.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. sync.RWMutex
  13. untaggedHandler []core.InboundHandler
  14. taggedHandlers map[string]core.InboundHandler
  15. }
  16. func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
  17. m := &Manager{
  18. taggedHandlers: make(map[string]core.InboundHandler),
  19. }
  20. v := core.FromContext(ctx)
  21. if v == nil {
  22. return nil, newError("V is not in context")
  23. }
  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. func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
  30. m.Lock()
  31. defer m.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. return nil
  39. }
  40. func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
  41. m.RLock()
  42. defer m.RUnlock()
  43. handler, found := m.taggedHandlers[tag]
  44. if !found {
  45. return nil, newError("handler not found: ", tag)
  46. }
  47. return handler, nil
  48. }
  49. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
  50. if len(tag) == 0 {
  51. return core.ErrNoClue
  52. }
  53. m.Lock()
  54. defer m.Unlock()
  55. if handler, found := m.taggedHandlers[tag]; found {
  56. handler.Close()
  57. delete(m.taggedHandlers, tag)
  58. return nil
  59. }
  60. return core.ErrNoClue
  61. }
  62. func (m *Manager) Start() error {
  63. for _, handler := range m.taggedHandlers {
  64. if err := handler.Start(); err != nil {
  65. return err
  66. }
  67. }
  68. for _, handler := range m.untaggedHandler {
  69. if err := handler.Start(); err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. func (m *Manager) Close() {
  76. for _, handler := range m.taggedHandlers {
  77. handler.Close()
  78. }
  79. for _, handler := range m.untaggedHandler {
  80. handler.Close()
  81. }
  82. }
  83. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (core.InboundHandler, error) {
  84. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  85. if err != nil {
  86. return nil, err
  87. }
  88. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  89. if !ok {
  90. return nil, newError("not a ReceiverConfig").AtError()
  91. }
  92. proxySettings, err := config.ProxySettings.GetInstance()
  93. if err != nil {
  94. return nil, err
  95. }
  96. tag := config.Tag
  97. allocStrategy := receiverSettings.AllocationStrategy
  98. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  99. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  100. }
  101. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  102. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  103. }
  104. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  105. }
  106. func init() {
  107. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  108. return New(ctx, config.(*proxyman.InboundConfig))
  109. }))
  110. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  111. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  112. }))
  113. }