mirror of https://github.com/cbeuw/Cloak
Implement SessionsCap and ExpiryTime limitations
This commit is contained in:
parent
e31aef3030
commit
652261af81
|
|
@ -144,7 +144,14 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if sesh, existing := user.GetSession(sessionID, mux.MakeObfs(UID), mux.MakeDeobfs(UID), util.ReadTLS); existing {
|
sesh, existing, err := user.GetSession(sessionID, mux.MakeObfs(UID), mux.MakeDeobfs(UID), util.ReadTLS)
|
||||||
|
if err != nil {
|
||||||
|
user.DelSession(sessionID)
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if existing {
|
||||||
sesh.AddConnection(conn)
|
sesh.AddConnection(conn)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
package usermanager
|
package usermanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
mux "github.com/cbeuw/Cloak/internal/multiplex"
|
||||||
)
|
)
|
||||||
|
|
@ -82,17 +84,23 @@ func (u *User) DelSession(sessionID uint32) {
|
||||||
u.sessionsM.Unlock()
|
u.sessionsM.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) GetSession(sessionID uint32, obfs mux.Obfser, deobfs mux.Deobfser, obfsedRead func(net.Conn, []byte) (int, error)) (sesh *mux.Session, existing bool) {
|
func (u *User) GetSession(sessionID uint32, obfs mux.Obfser, deobfs mux.Deobfser, obfsedRead func(net.Conn, []byte) (int, error)) (sesh *mux.Session, existing bool, err error) {
|
||||||
// TODO: session cap
|
if time.Now().Unix() > u.ExpiryTime {
|
||||||
|
return nil, false, errors.New("Expiry time passed")
|
||||||
|
}
|
||||||
u.sessionsM.Lock()
|
u.sessionsM.Lock()
|
||||||
if sesh = u.sessions[sessionID]; sesh != nil {
|
if sesh = u.sessions[sessionID]; sesh != nil {
|
||||||
u.sessionsM.Unlock()
|
u.sessionsM.Unlock()
|
||||||
return sesh, true
|
return sesh, true, nil
|
||||||
} else {
|
} else {
|
||||||
|
if len(u.sessions) >= int(u.SessionsCap) {
|
||||||
|
u.sessionsM.Unlock()
|
||||||
|
return nil, false, errors.New("SessionsCap reached")
|
||||||
|
}
|
||||||
log.Printf("Creating session %v\n", sessionID)
|
log.Printf("Creating session %v\n", sessionID)
|
||||||
sesh = mux.MakeSession(sessionID, u.valve, obfs, deobfs, obfsedRead)
|
sesh = mux.MakeSession(sessionID, u.valve, obfs, deobfs, obfsedRead)
|
||||||
u.sessions[sessionID] = sesh
|
u.sessions[sessionID] = sesh
|
||||||
u.sessionsM.Unlock()
|
u.sessionsM.Unlock()
|
||||||
return sesh, false
|
return sesh, false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -117,8 +117,6 @@ func (up *Userpanel) GetAndActivateAdminUser(AdminUID []byte) (*User, error) {
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: expiry check
|
|
||||||
|
|
||||||
// GetUser is used to retrieve a user if s/he is active, or to retrieve the user's infor
|
// GetUser is used to retrieve a user if s/he is active, or to retrieve the user's infor
|
||||||
// from the db and mark it as an active user
|
// from the db and mark it as an active user
|
||||||
func (up *Userpanel) GetAndActivateUser(UID []byte) (*User, error) {
|
func (up *Userpanel) GetAndActivateUser(UID []byte) (*User, error) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue