Cloak/internal/server/usermanager/user.go

86 lines
1.8 KiB
Go
Raw Normal View History

2018-11-07 21:16:13 +00:00
package usermanager
import (
"log"
"net"
"sync"
"sync/atomic"
mux "github.com/cbeuw/Cloak/internal/multiplex"
2018-11-07 21:16:13 +00:00
)
/*
type userParams struct {
sessionsCap uint32
upRate int64
downRate int64
upCredit int64
downCredit int64
}
*/
type User struct {
2018-11-07 21:16:13 +00:00
up *Userpanel
uid [32]byte
sessionsCap uint32 //userParams
valve *mux.Valve
sessionsM sync.RWMutex
sessions map[uint32]*mux.Session
}
func MakeUser(up *Userpanel, uid [32]byte, sessionsCap uint32, upRate, downRate, upCredit, downCredit int64) *User {
2018-11-07 21:16:13 +00:00
valve := mux.MakeValve(upRate, downRate, upCredit, downCredit)
u := &User{
2018-11-07 21:16:13 +00:00
up: up,
uid: uid,
valve: valve,
sessionsCap: sessionsCap,
sessions: make(map[uint32]*mux.Session),
}
return u
}
func (u *User) setSessionsCap(cap uint32) {
2018-11-07 21:16:13 +00:00
atomic.StoreUint32(&u.sessionsCap, cap)
}
func (u *User) GetSession(sessionID uint32) *mux.Session {
2018-11-07 21:16:13 +00:00
u.sessionsM.RLock()
defer u.sessionsM.RUnlock()
return u.sessions[sessionID]
2018-11-07 21:16:13 +00:00
}
func (u *User) PutSession(sessionID uint32, sesh *mux.Session) {
2018-11-07 21:16:13 +00:00
u.sessionsM.Lock()
u.sessions[sessionID] = sesh
u.sessionsM.Unlock()
}
func (u *User) DelSession(sessionID uint32) {
2018-11-07 21:16:13 +00:00
u.sessionsM.Lock()
delete(u.sessions, sessionID)
if len(u.sessions) == 0 {
u.sessionsM.Unlock()
u.up.delActiveUser(u.uid)
return
}
u.sessionsM.Unlock()
}
func (u *User) GetOrCreateSession(sessionID uint32, obfs func(*mux.Frame) []byte, deobfs func([]byte) *mux.Frame, obfsedRead func(net.Conn, []byte) (int, error)) (sesh *mux.Session, existing bool) {
u.sessionsM.Lock()
defer u.sessionsM.Unlock()
if sesh = u.sessions[sessionID]; sesh != nil {
return sesh, true
2018-11-07 21:16:13 +00:00
} else {
log.Printf("Creating session %v\n", sessionID)
2018-11-07 21:16:13 +00:00
sesh = mux.MakeSession(sessionID, u.valve, obfs, deobfs, obfsedRead)
u.sessions[sessionID] = sesh
return sesh, false
2018-11-07 21:16:13 +00:00
}
}