session.go 4.1 KB

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