session.go 4.7 KB

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