88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"github.com/gliderlabs/ssh"
|
|
"io"
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// global configuration
|
|
|
|
type AgentState struct {
|
|
|
|
// Advance warning time to notify the user of something important happening
|
|
advanceWarningTime time.Duration
|
|
|
|
// session expiry time
|
|
sessionExpiryTime time.Duration
|
|
|
|
// ticker
|
|
tickerInterval time.Duration
|
|
ticker *time.Ticker
|
|
|
|
// map of unique session id to a session
|
|
sessions map[int]*AgentSession
|
|
}
|
|
|
|
type AgentSession struct {
|
|
startTime time.Time
|
|
|
|
// For sending messages to the user
|
|
sshSession ssh.Session
|
|
}
|
|
|
|
var state AgentState
|
|
|
|
func ConfigureAgent(advanceWarningTime, sessionExpiryTime, tickerInterval time.Duration) {
|
|
state = AgentState{
|
|
advanceWarningTime: advanceWarningTime,
|
|
sessionExpiryTime: sessionExpiryTime,
|
|
tickerInterval: tickerInterval,
|
|
ticker: time.NewTicker(tickerInterval),
|
|
sessions: make(map[int]*AgentSession),
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
<-state.ticker.C
|
|
check()
|
|
}
|
|
}()
|
|
}
|
|
|
|
func Login(sessionId int, sshSession ssh.Session) {
|
|
log.Println("New login")
|
|
agentSession := AgentSession{
|
|
startTime: time.Now(),
|
|
sshSession: sshSession,
|
|
}
|
|
state.sessions[sessionId] = &agentSession
|
|
LogStatus()
|
|
}
|
|
|
|
func LogOut(sessionId int) {
|
|
log.Println("User logged out")
|
|
delete(state.sessions, sessionId)
|
|
LogStatus()
|
|
check()
|
|
}
|
|
|
|
func LogStatus() {
|
|
fmt := "%-20s %-20s"
|
|
log.Println()
|
|
log.Printf(fmt, "UID", "START_TIME")
|
|
for uid, session := range state.sessions {
|
|
log.Printf(fmt, strconv.Itoa(uid), session.startTime.Format("2006-01-02 15:04:05"))
|
|
}
|
|
log.Println()
|
|
}
|
|
|
|
func check() {
|
|
log.Println("Timer is firing!")
|
|
for _, session := range state.sessions {
|
|
io.WriteString(session.sshSession.Stderr(), "\n\nThe clock is ticking for you!\n\n")
|
|
}
|
|
}
|