2017-04-12 20:31:11 +00:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"v2ray.com/core/transport/ray"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SessionManager struct {
|
|
|
|
sync.RWMutex
|
|
|
|
count uint16
|
|
|
|
sessions map[uint16]*Session
|
2017-04-13 18:14:07 +00:00
|
|
|
closed bool
|
2017-04-12 20:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSessionManager() *SessionManager {
|
|
|
|
return &SessionManager{
|
|
|
|
count: 0,
|
|
|
|
sessions: make(map[uint16]*Session, 32),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Size() int {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
return len(m.sessions)
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:14:07 +00:00
|
|
|
func (m *SessionManager) Allocate() *Session {
|
2017-04-12 20:31:11 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2017-04-13 18:14:07 +00:00
|
|
|
if m.closed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:31:11 +00:00
|
|
|
m.count++
|
2017-04-13 18:14:07 +00:00
|
|
|
s := &Session{
|
|
|
|
ID: m.count,
|
|
|
|
parent: m,
|
|
|
|
}
|
2017-04-12 20:31:11 +00:00
|
|
|
m.sessions[s.ID] = s
|
2017-04-13 18:14:07 +00:00
|
|
|
return s
|
2017-04-12 20:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Add(s *Session) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
m.sessions[s.ID] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Remove(id uint16) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
delete(m.sessions, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Get(id uint16) (*Session, bool) {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2017-04-13 18:14:07 +00:00
|
|
|
if m.closed {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:31:11 +00:00
|
|
|
s, found := m.sessions[id]
|
|
|
|
return s, found
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:14:07 +00:00
|
|
|
func (m *SessionManager) CloseIfNoSession() bool {
|
2017-04-12 20:31:11 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2017-04-13 18:56:32 +00:00
|
|
|
if m.closed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-04-19 10:36:04 +00:00
|
|
|
if len(m.sessions) != 0 {
|
2017-04-13 18:14:07 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:56:32 +00:00
|
|
|
m.closed = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SessionManager) Close() {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
if m.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:14:07 +00:00
|
|
|
m.closed = true
|
|
|
|
|
2017-04-12 20:31:11 +00:00
|
|
|
for _, s := range m.sessions {
|
2017-04-16 11:24:02 +00:00
|
|
|
s.input.Close()
|
|
|
|
s.output.Close()
|
2017-04-12 20:31:11 +00:00
|
|
|
}
|
2017-04-13 18:14:07 +00:00
|
|
|
|
|
|
|
m.sessions = make(map[uint16]*Session)
|
2017-04-12 20:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Session struct {
|
|
|
|
sync.Mutex
|
|
|
|
input ray.InputStream
|
|
|
|
output ray.OutputStream
|
|
|
|
parent *SessionManager
|
|
|
|
ID uint16
|
|
|
|
uplinkClosed bool
|
|
|
|
downlinkClosed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) CloseUplink() {
|
|
|
|
var allDone bool
|
|
|
|
s.Lock()
|
|
|
|
s.uplinkClosed = true
|
|
|
|
allDone = s.uplinkClosed && s.downlinkClosed
|
|
|
|
s.Unlock()
|
|
|
|
if allDone {
|
|
|
|
s.parent.Remove(s.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Session) CloseDownlink() {
|
|
|
|
var allDone bool
|
|
|
|
s.Lock()
|
|
|
|
s.downlinkClosed = true
|
|
|
|
allDone = s.uplinkClosed && s.downlinkClosed
|
|
|
|
s.Unlock()
|
|
|
|
if allDone {
|
|
|
|
s.parent.Remove(s.ID)
|
|
|
|
}
|
|
|
|
}
|