2024-02-02 18:47:45 +01:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2024-02-03 11:11:10 +01:00
|
|
|
"log"
|
2024-02-02 18:47:45 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
|
|
|
"git.jmbit.de/jmb/podterminal/pods"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sessionData struct {
|
|
|
|
lastAccess *time.Time
|
|
|
|
sessionID string
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidSessions []string
|
|
|
|
var sessionLastAccess map[string]*sessionData
|
|
|
|
|
|
|
|
func initSessionAging() error {
|
|
|
|
sessionLastAccess = make(map[string]*sessionData)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSession(id string, sessionID string) {
|
|
|
|
nowTime := time.Now()
|
|
|
|
sessionLastAccess[id].lastAccess = &nowTime
|
|
|
|
sessionLastAccess[id].sessionID = sessionID
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteIdleSessions() {
|
|
|
|
idleTimout := viper.GetInt("session_timeout")
|
|
|
|
tenMinutesAgo := -time.Duration(idleTimout) * time.Second
|
|
|
|
oldAge := time.Now().Add(tenMinutesAgo)
|
|
|
|
for session, sessionData := range sessionLastAccess {
|
2024-02-03 11:11:10 +01:00
|
|
|
log.Printf("Session %s last connected at %s", session, sessionData.lastAccess.String())
|
2024-02-02 18:47:45 +01:00
|
|
|
if oldAge.After(*sessionData.lastAccess) {
|
|
|
|
pods.DestroyContainer(session)
|
|
|
|
invalidSessions = append(invalidSessions, sessionData.sessionID)
|
2024-02-03 11:11:10 +01:00
|
|
|
// Delete Proxy entry to avoid 502s
|
|
|
|
delete(proxies, session)
|
2024-02-02 18:47:45 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func IdleSessionCleanup() error {
|
|
|
|
err := initSessionAging()
|
|
|
|
if err != nil {
|
|
|
|
println("Could not initialize Session aging")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
deleteIdleSessions()
|
|
|
|
time.Sleep(time.Duration(viper.GetInt("session_timeout")) * time.Second)
|
|
|
|
}
|
|
|
|
}
|