diff --git a/main.go b/main.go index 58b2ac8..25f1af0 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,7 @@ func main() { if err != nil { log.Panic("failed to connect database") } - db.AutoMigrate(&runtime.Node{}, &websocket.Auth{}) + db.AutoMigrate(&runtime.Node{}, &websocket.Session{}) sshmanager := ssh.NewManager(config.SSHPrivateKey, config.SSHTimeout.Duration) nodesYanic := runtimeYanic.NewNodes(&runtimeYanic.NodesConfig{}) diff --git a/websocket/hd_auth.go b/websocket/hd_auth.go index 7fa49af..d6f3085 100644 --- a/websocket/hd_auth.go +++ b/websocket/hd_auth.go @@ -1,29 +1,39 @@ package websocket import ( + "time" + "github.com/google/uuid" log "github.com/sirupsen/logrus" "dev.sum7.eu/genofire/golang-lib/websocket" ) -type Auth struct { - SessionID uuid.UUID +type Session struct { + SessionID uuid.UUID `json:"-" gorm:"primary_key"` + Lastseen *time.Time `json:"-"` + Ping bool `json:"ping"` } -func (ws *WebsocketServer) IsLoggedIn(msg *websocket.Message) bool { - auth := Auth{ +func (ws *WebsocketServer) IsLoggedIn(msg *websocket.Message) (*Session, bool) { + session := Session{ SessionID: msg.Session, } - err := ws.db.First(&auth) - return err.Error == nil + err := ws.db.First(&session) + if err.Error == nil { + now := time.Now() + session.Lastseen = &now + ws.db.Save(&session) + return &session, true + } + return nil, false } func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Message) error { - auth := Auth{ + session := Session{ SessionID: msg.Session, } - err := ws.db.First(&auth) + err := ws.db.First(&session) if err.Error == nil { msg.Answer(msg.Subject, true) logger.Warn("already loggedIn") @@ -35,39 +45,45 @@ func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Messag msg.Answer(msg.Subject, false) return nil } - ok = (ws.secret == secret) - if ok { - err = ws.db.Create(&auth) + if ws.secret == secret { + now := time.Now() + session.Lastseen = &now + err = ws.db.Create(&session) if err.Error != nil { log.Warnf("database error: %s", err.Error.Error()) msg.Answer(msg.Subject, false) return err.Error } logger.Debug("done") + msg.Answer(msg.Subject, session) } else { logger.Warn("wrong secret") + msg.Answer(msg.Subject, false) } - msg.Answer(msg.Subject, ok) return nil } func (ws *WebsocketServer) authStatusHandler(logger *log.Entry, msg *websocket.Message) error { defer logger.Debug("done") - - msg.Answer(msg.Subject, ws.IsLoggedIn(msg)) + sess, ok := ws.IsLoggedIn(msg) + if ok { + msg.Answer(msg.Subject, sess) + } else { + msg.Answer(msg.Subject, false) + } return nil } func (ws *WebsocketServer) logoutHandler(logger *log.Entry, msg *websocket.Message) error { - auth := Auth{ + session := Session{ SessionID: msg.Session, } - err := ws.db.First(&auth) + err := ws.db.First(&session) if err.Error != nil { msg.Answer(msg.Subject, false) logger.Warn("logout without login") return nil } - err = ws.db.Delete(&auth) + err = ws.db.Delete(&session) logger.Debug("done") msg.Answer(msg.Subject, err.Error == nil) return err.Error diff --git a/websocket/hd_node.go b/websocket/hd_node.go index a09b9f1..907a9cc 100644 --- a/websocket/hd_node.go +++ b/websocket/hd_node.go @@ -10,7 +10,7 @@ import ( ) func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) error { - if !ws.IsLoggedIn(msg) { + if _, ok := ws.IsLoggedIn(msg); !ok { msg.Answer(msg.Subject, false) logger.Warn("not logged in") return nil