inbound.go 4.6 KB

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