inbound.go 4.6 KB

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