2018-11-07 21:16:13 +00:00
|
|
|
package usermanager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2018-11-08 19:47:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
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)
|
2018-11-08 19:47:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
func (u *User) setSessionsCap(cap uint32) {
|
2018-11-07 21:16:13 +00:00
|
|
|
atomic.StoreUint32(&u.sessionsCap, cap)
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
func (u *User) GetSession(sessionID uint32) *mux.Session {
|
2018-11-07 21:16:13 +00:00
|
|
|
u.sessionsM.RLock()
|
|
|
|
defer u.sessionsM.RUnlock()
|
2018-11-08 19:47:53 +00:00
|
|
|
return u.sessions[sessionID]
|
2018-11-07 21:16:13 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +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()
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-11-08 19:47:53 +00:00
|
|
|
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 {
|
2018-11-08 19:47:53 +00:00
|
|
|
log.Printf("Creating session %v\n", sessionID)
|
2018-11-07 21:16:13 +00:00
|
|
|
sesh = mux.MakeSession(sessionID, u.valve, obfs, deobfs, obfsedRead)
|
2018-11-08 19:47:53 +00:00
|
|
|
u.sessions[sessionID] = sesh
|
|
|
|
return sesh, false
|
2018-11-07 21:16:13 +00:00
|
|
|
}
|
|
|
|
}
|