2017-04-12 16:31:11 -04:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol"
|
2017-04-12 16:31:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type SessionManager struct {
|
|
|
|
sync.RWMutex
|
|
|
|
sessions map[uint16]*Session
|
2017-04-19 15:27:21 -04:00
|
|
|
count uint16
|
2017-04-13 14:14:07 -04:00
|
|
|
closed bool
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionManager() *SessionManager {
|
|
|
|
return &SessionManager{
|
|
|
|
count: 0,
|
2017-10-27 05:40:18 -04:00
|
|
|
sessions: make(map[uint16]*Session, 16),
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 07:51:56 -04:00
|
|
|
func (m *SessionManager) Closed() bool {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
return m.closed
|
|
|
|
}
|
|
|
|
|
2017-04-12 16:31:11 -04:00
|
|
|
func (m *SessionManager) Size() int {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
return len(m.sessions)
|
|
|
|
}
|
|
|
|
|
2017-04-19 15:27:21 -04:00
|
|
|
func (m *SessionManager) Count() int {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
return int(m.count)
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:14:07 -04:00
|
|
|
func (m *SessionManager) Allocate() *Session {
|
2017-04-12 16:31:11 -04:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-04-13 14:14:07 -04:00
|
|
|
if m.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 16:31:11 -04:00
|
|
|
m.count++
|
2017-04-13 14:14:07 -04:00
|
|
|
s := &Session{
|
|
|
|
ID: m.count,
|
|
|
|
parent: m,
|
|
|
|
}
|
2017-04-12 16:31:11 -04:00
|
|
|
m.sessions[s.ID] = s
|
2017-04-13 14:14:07 -04:00
|
|
|
return s
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Add(s *Session) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-10-27 05:40:18 -04:00
|
|
|
if m.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-28 04:08:43 -04:00
|
|
|
m.count++
|
2017-04-12 16:31:11 -04:00
|
|
|
m.sessions[s.ID] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Remove(id uint16) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-10-27 05:40:18 -04:00
|
|
|
if m.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-12 16:31:11 -04:00
|
|
|
delete(m.sessions, id)
|
2018-04-07 17:07:30 -04:00
|
|
|
|
|
|
|
if len(m.sessions) == 0 {
|
|
|
|
m.sessions = make(map[uint16]*Session, 16)
|
|
|
|
}
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Get(id uint16) (*Session, bool) {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2017-04-13 14:14:07 -04:00
|
|
|
if m.closed {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-04-12 16:31:11 -04:00
|
|
|
s, found := m.sessions[id]
|
|
|
|
return s, found
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:14:07 -04:00
|
|
|
func (m *SessionManager) CloseIfNoSession() bool {
|
2017-04-19 15:27:21 -04:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2017-04-12 16:31:11 -04:00
|
|
|
|
2017-04-13 14:56:32 -04:00
|
|
|
if m.closed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-04-19 06:36:04 -04:00
|
|
|
if len(m.sessions) != 0 {
|
2017-04-13 14:14:07 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:56:32 -04:00
|
|
|
m.closed = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (m *SessionManager) Close() error {
|
2017-04-19 15:27:21 -04:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2017-04-13 14:56:32 -04:00
|
|
|
|
|
|
|
if m.closed {
|
2018-02-08 09:39:46 -05:00
|
|
|
return nil
|
2017-04-13 14:56:32 -04:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:14:07 -04:00
|
|
|
m.closed = true
|
|
|
|
|
2017-04-12 16:31:11 -04:00
|
|
|
for _, s := range m.sessions {
|
2020-10-11 07:22:46 -04:00
|
|
|
common.Close(s.input)
|
|
|
|
common.Close(s.output)
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
2017-04-13 14:14:07 -04:00
|
|
|
|
2017-10-27 05:40:18 -04:00
|
|
|
m.sessions = nil
|
2018-02-08 09:39:46 -05:00
|
|
|
return nil
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 07:36:31 -04:00
|
|
|
// Session represents a client connection in a Mux connection.
|
2017-04-12 16:31:11 -04:00
|
|
|
type Session struct {
|
2018-04-16 18:31:10 -04:00
|
|
|
input buf.Reader
|
|
|
|
output buf.Writer
|
2017-05-02 17:53:39 -04:00
|
|
|
parent *SessionManager
|
|
|
|
ID uint16
|
|
|
|
transferType protocol.TransferType
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
|
|
|
|
2017-10-21 14:40:31 -04:00
|
|
|
// Close closes all resources associated with this session.
|
2018-02-08 09:39:46 -05:00
|
|
|
func (s *Session) Close() error {
|
2020-10-11 07:22:46 -04:00
|
|
|
common.Close(s.output)
|
|
|
|
common.Close(s.input)
|
2017-05-02 17:53:39 -04:00
|
|
|
s.parent.Remove(s.ID)
|
2018-02-08 09:39:46 -05:00
|
|
|
return nil
|
2017-04-12 16:31:11 -04:00
|
|
|
}
|
2017-05-02 16:23:07 -04:00
|
|
|
|
2017-10-30 07:36:31 -04:00
|
|
|
// NewReader creates a buf.Reader based on the transfer type of this Session.
|
2017-11-24 19:05:30 -05:00
|
|
|
func (s *Session) NewReader(reader *buf.BufferedReader) buf.Reader {
|
2017-05-02 16:23:07 -04:00
|
|
|
if s.transferType == protocol.TransferTypeStream {
|
|
|
|
return NewStreamReader(reader)
|
|
|
|
}
|
|
|
|
return NewPacketReader(reader)
|
|
|
|
}
|