inbound.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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 implements inbound.Manager.
  47. func (m *Manager) GetHandler(ctx context.Context, tag string) (inbound.Handler, 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. // RemoveHandler implements inbound.Manager.
  57. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
  58. if tag == "" {
  59. return common.ErrNoClue
  60. }
  61. m.access.Lock()
  62. defer m.access.Unlock()
  63. if handler, found := m.taggedHandlers[tag]; found {
  64. if err := handler.Close(); err != nil {
  65. newError("failed to close handler ", tag).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
  66. }
  67. delete(m.taggedHandlers, tag)
  68. return nil
  69. }
  70. return common.ErrNoClue
  71. }
  72. // Start implements common.Runnable.
  73. func (m *Manager) Start() error {
  74. m.access.Lock()
  75. defer m.access.Unlock()
  76. m.running = true
  77. for _, handler := range m.taggedHandlers {
  78. if err := handler.Start(); err != nil {
  79. return err
  80. }
  81. }
  82. for _, handler := range m.untaggedHandler {
  83. if err := handler.Start(); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. // Close implements common.Closable.
  90. func (m *Manager) Close() error {
  91. m.access.Lock()
  92. defer m.access.Unlock()
  93. m.running = false
  94. var errors []interface{}
  95. for _, handler := range m.taggedHandlers {
  96. if err := handler.Close(); err != nil {
  97. errors = append(errors, err)
  98. }
  99. }
  100. for _, handler := range m.untaggedHandler {
  101. if err := handler.Close(); err != nil {
  102. errors = append(errors, err)
  103. }
  104. }
  105. if len(errors) > 0 {
  106. return newError("failed to close all handlers").Base(newError(serial.Concat(errors...)))
  107. }
  108. return nil
  109. }
  110. // NewHandler creates a new inbound.Handler based on the given config.
  111. func NewHandler(ctx context.Context, config *core.InboundHandlerConfig) (inbound.Handler, error) {
  112. rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
  113. if err != nil {
  114. return nil, err
  115. }
  116. proxySettings, err := config.ProxySettings.GetInstance()
  117. if err != nil {
  118. return nil, err
  119. }
  120. tag := config.Tag
  121. receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
  122. if !ok {
  123. return nil, newError("not a ReceiverConfig").AtError()
  124. }
  125. streamSettings := receiverSettings.StreamSettings
  126. if streamSettings != nil && streamSettings.SocketSettings != nil {
  127. ctx = session.ContextWithSockopt(ctx, &session.Sockopt{
  128. Mark: streamSettings.SocketSettings.Mark,
  129. })
  130. }
  131. allocStrategy := receiverSettings.AllocationStrategy
  132. if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
  133. return NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
  134. }
  135. if allocStrategy.Type == proxyman.AllocationStrategy_Random {
  136. return NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
  137. }
  138. return nil, newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).AtError()
  139. }
  140. func init() {
  141. common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  142. return New(ctx, config.(*proxyman.InboundConfig))
  143. }))
  144. common.Must(common.RegisterConfig((*core.InboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  145. return NewHandler(ctx, config.(*core.InboundHandlerConfig))
  146. }))
  147. }