session.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package mux
  2. import (
  3. "context"
  4. "io"
  5. "runtime"
  6. "sync"
  7. "time"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/buf"
  10. "github.com/xtls/xray-core/common/errors"
  11. "github.com/xtls/xray-core/common/net"
  12. "github.com/xtls/xray-core/common/protocol"
  13. "github.com/xtls/xray-core/common/signal/done"
  14. "github.com/xtls/xray-core/transport/pipe"
  15. )
  16. type SessionManager struct {
  17. sync.RWMutex
  18. sessions map[uint16]*Session
  19. count uint16
  20. closed bool
  21. }
  22. func NewSessionManager() *SessionManager {
  23. return &SessionManager{
  24. count: 0,
  25. sessions: make(map[uint16]*Session, 16),
  26. }
  27. }
  28. func (m *SessionManager) Closed() bool {
  29. m.RLock()
  30. defer m.RUnlock()
  31. return m.closed
  32. }
  33. func (m *SessionManager) Size() int {
  34. m.RLock()
  35. defer m.RUnlock()
  36. return len(m.sessions)
  37. }
  38. func (m *SessionManager) Count() int {
  39. m.RLock()
  40. defer m.RUnlock()
  41. return int(m.count)
  42. }
  43. func (m *SessionManager) Allocate(Strategy *ClientStrategy) *Session {
  44. m.Lock()
  45. defer m.Unlock()
  46. MaxConcurrency := int(Strategy.MaxConcurrency)
  47. MaxConnection := uint16(Strategy.MaxConnection)
  48. if m.closed || (MaxConcurrency > 0 && len(m.sessions) >= MaxConcurrency) || (MaxConnection > 0 && m.count >= MaxConnection) {
  49. return nil
  50. }
  51. m.count++
  52. s := &Session{
  53. ID: m.count,
  54. parent: m,
  55. done: done.New(),
  56. }
  57. m.sessions[s.ID] = s
  58. return s
  59. }
  60. func (m *SessionManager) Add(s *Session) bool {
  61. m.Lock()
  62. defer m.Unlock()
  63. if m.closed {
  64. return false
  65. }
  66. m.count++
  67. m.sessions[s.ID] = s
  68. return true
  69. }
  70. func (m *SessionManager) Remove(locked bool, id uint16) {
  71. if !locked {
  72. m.Lock()
  73. defer m.Unlock()
  74. }
  75. locked = true
  76. if m.closed {
  77. return
  78. }
  79. delete(m.sessions, id)
  80. /*
  81. if len(m.sessions) == 0 {
  82. m.sessions = make(map[uint16]*Session, 16)
  83. }
  84. */
  85. }
  86. func (m *SessionManager) Get(id uint16) (*Session, bool) {
  87. m.RLock()
  88. defer m.RUnlock()
  89. if m.closed {
  90. return nil, false
  91. }
  92. s, found := m.sessions[id]
  93. return s, found
  94. }
  95. func (m *SessionManager) CloseIfNoSessionAndIdle(checkSize int, checkCount int) bool {
  96. m.Lock()
  97. defer m.Unlock()
  98. if m.closed {
  99. return true
  100. }
  101. if len(m.sessions) != 0 || checkSize != 0 || checkCount != int(m.count) {
  102. return false
  103. }
  104. m.closed = true
  105. m.sessions = nil
  106. return true
  107. }
  108. func (m *SessionManager) Close() error {
  109. m.Lock()
  110. defer m.Unlock()
  111. if m.closed {
  112. return nil
  113. }
  114. m.closed = true
  115. for _, s := range m.sessions {
  116. s.Close(true)
  117. }
  118. m.sessions = nil
  119. return nil
  120. }
  121. // Session represents a client connection in a Mux connection.
  122. type Session struct {
  123. input buf.Reader
  124. output buf.Writer
  125. parent *SessionManager
  126. ID uint16
  127. transferType protocol.TransferType
  128. closed bool
  129. done *done.Instance
  130. XUDP *XUDP
  131. }
  132. // Close closes all resources associated with this session.
  133. func (s *Session) Close(locked bool) error {
  134. if !locked {
  135. s.parent.Lock()
  136. defer s.parent.Unlock()
  137. }
  138. locked = true
  139. if s.closed {
  140. return nil
  141. }
  142. s.closed = true
  143. if s.done != nil {
  144. s.done.Close()
  145. }
  146. if s.XUDP == nil {
  147. common.Interrupt(s.input)
  148. common.Close(s.output)
  149. } else {
  150. // Stop existing handle(), then trigger writer.Close().
  151. // Note that s.output may be dispatcher.SizeStatWriter.
  152. s.input.(*pipe.Reader).ReturnAnError(io.EOF)
  153. runtime.Gosched()
  154. // If the error set by ReturnAnError still exists, clear it.
  155. s.input.(*pipe.Reader).Recover()
  156. XUDPManager.Lock()
  157. if s.XUDP.Status == Active {
  158. s.XUDP.Expire = time.Now().Add(time.Minute)
  159. s.XUDP.Status = Expiring
  160. errors.LogDebug(context.Background(), "XUDP put ", s.XUDP.GlobalID)
  161. }
  162. XUDPManager.Unlock()
  163. }
  164. s.parent.Remove(locked, s.ID)
  165. return nil
  166. }
  167. // NewReader creates a buf.Reader based on the transfer type of this Session.
  168. func (s *Session) NewReader(reader *buf.BufferedReader, dest *net.Destination) buf.Reader {
  169. if s.transferType == protocol.TransferTypeStream {
  170. return NewStreamReader(reader)
  171. }
  172. return NewPacketReader(reader, dest)
  173. }
  174. const (
  175. Initializing = 0
  176. Active = 1
  177. Expiring = 2
  178. )
  179. type XUDP struct {
  180. GlobalID [8]byte
  181. Status uint64
  182. Expire time.Time
  183. Mux *Session
  184. }
  185. func (x *XUDP) Interrupt() {
  186. common.Interrupt(x.Mux.input)
  187. common.Close(x.Mux.output)
  188. }
  189. var XUDPManager struct {
  190. sync.Mutex
  191. Map map[[8]byte]*XUDP
  192. }
  193. func init() {
  194. XUDPManager.Map = make(map[[8]byte]*XUDP)
  195. go func() {
  196. for {
  197. time.Sleep(time.Minute)
  198. now := time.Now()
  199. XUDPManager.Lock()
  200. for id, x := range XUDPManager.Map {
  201. if x.Status == Expiring && now.After(x.Expire) {
  202. x.Interrupt()
  203. delete(XUDPManager.Map, id)
  204. errors.LogDebug(context.Background(), "XUDP del ", id)
  205. }
  206. }
  207. XUDPManager.Unlock()
  208. }
  209. }()
  210. }