Cloak/internal/server/activeuser.go

72 lines
1.4 KiB
Go
Raw Normal View History

package server
import (
"sync"
mux "github.com/cbeuw/Cloak/internal/multiplex"
)
type ActiveUser struct {
panel *userPanel
arrUID [16]byte
valve mux.Valve
2019-08-04 20:10:59 +00:00
bypass bool
sessionsM sync.RWMutex
sessions map[uint32]*mux.Session
}
2019-08-04 20:10:59 +00:00
func (u *ActiveUser) DeleteSession(sessionID uint32, reason string) {
u.sessionsM.Lock()
2019-08-04 20:10:59 +00:00
sesh, existing := u.sessions[sessionID]
if existing {
delete(u.sessions, sessionID)
sesh.SetTerminalMsg(reason)
sesh.Close()
}
if len(u.sessions) == 0 {
2019-08-04 20:10:59 +00:00
u.panel.DeleteActiveUser(u)
}
u.sessionsM.Unlock()
}
2019-08-11 23:22:15 +00:00
func (u *ActiveUser) GetSession(sessionID uint32, config *mux.SessionConfig) (sesh *mux.Session, existing bool, err error) {
u.sessionsM.Lock()
2019-07-28 11:52:57 +00:00
defer u.sessionsM.Unlock()
if sesh = u.sessions[sessionID]; sesh != nil {
return sesh, true, nil
} else {
2019-08-04 20:10:59 +00:00
if !u.bypass {
err := u.panel.Manager.AuthoriseNewSession(u.arrUID[:], len(u.sessions))
if err != nil {
return nil, false, err
}
}
2019-08-11 23:22:15 +00:00
config.Valve = u.valve
sesh = mux.MakeSession(sessionID, config)
u.sessions[sessionID] = sesh
return sesh, false, nil
}
}
2019-07-24 14:25:09 +00:00
func (u *ActiveUser) Terminate(reason string) {
u.sessionsM.Lock()
for _, sesh := range u.sessions {
2019-07-24 14:25:09 +00:00
if reason != "" {
sesh.SetTerminalMsg(reason)
}
2019-08-04 20:10:59 +00:00
sesh.Close()
}
u.sessionsM.Unlock()
2019-08-04 20:10:59 +00:00
u.panel.DeleteActiveUser(u)
}
func (u *ActiveUser) NumSession() int {
u.sessionsM.RLock()
2019-07-28 11:52:57 +00:00
defer u.sessionsM.RUnlock()
return len(u.sessions)
}