2018-06-30 16:20:54 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2018-08-10 17:33:39 +02:00
|
|
|
"time"
|
|
|
|
|
2019-06-10 02:57:36 +02:00
|
|
|
"github.com/bdlm/log"
|
2018-07-26 14:14:23 +02:00
|
|
|
"github.com/google/uuid"
|
2018-08-24 23:34:02 +02:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-06-30 16:20:54 +02:00
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/websocket"
|
2018-06-30 16:20:54 +02:00
|
|
|
)
|
|
|
|
|
2018-08-10 17:33:39 +02:00
|
|
|
type Session struct {
|
2018-08-24 23:34:02 +02:00
|
|
|
SessionID uuid.UUID `json:"-" gorm:"primary_key" mapstructure:"-"`
|
|
|
|
Lastseen *time.Time `json:"-" mapstructure:"-"`
|
|
|
|
Ping bool `json:"ping" mapstructure:"ping"`
|
2018-07-26 14:14:23 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 17:33:39 +02:00
|
|
|
func (ws *WebsocketServer) IsLoggedIn(msg *websocket.Message) (*Session, bool) {
|
2018-08-30 10:26:23 +02:00
|
|
|
if msg == nil || msg.Session == uuid.Nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
session := Session{
|
2018-07-26 14:14:23 +02:00
|
|
|
SessionID: msg.Session,
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
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
|
2018-07-26 14:14:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (ws *WebsocketServer) loginHandler(logger *log.Entry, msg *websocket.Message) error {
|
2018-08-30 10:26:23 +02:00
|
|
|
if msg == nil || msg.Session == uuid.Nil {
|
|
|
|
logger.Warn("no session for this message detected")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
session := Session{
|
2018-07-26 14:14:23 +02:00
|
|
|
SessionID: msg.Session,
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
err := ws.db.First(&session)
|
2018-07-26 14:14:23 +02:00
|
|
|
if err.Error == nil {
|
2018-08-24 23:34:02 +02:00
|
|
|
msg.Answer(msg.Subject, session)
|
|
|
|
now := time.Now()
|
|
|
|
session.Lastseen = &now
|
|
|
|
ws.db.Save(&session)
|
2018-06-30 16:20:54 +02:00
|
|
|
logger.Warn("already loggedIn")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
secret, ok := msg.Body.(string)
|
|
|
|
if !ok {
|
|
|
|
logger.Warn("invalid secret format")
|
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
if ws.secret == secret {
|
|
|
|
now := time.Now()
|
|
|
|
session.Lastseen = &now
|
|
|
|
err = ws.db.Create(&session)
|
2018-07-26 14:14:23 +02:00
|
|
|
if err.Error != nil {
|
|
|
|
log.Warnf("database error: %s", err.Error.Error())
|
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
return err.Error
|
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
logger.Debug("done")
|
2018-08-10 17:33:39 +02:00
|
|
|
msg.Answer(msg.Subject, session)
|
2018-06-30 16:20:54 +02:00
|
|
|
} else {
|
|
|
|
logger.Warn("wrong secret")
|
2018-08-10 17:33:39 +02:00
|
|
|
msg.Answer(msg.Subject, false)
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebsocketServer) authStatusHandler(logger *log.Entry, msg *websocket.Message) error {
|
|
|
|
defer logger.Debug("done")
|
2018-08-10 17:33:39 +02:00
|
|
|
sess, ok := ws.IsLoggedIn(msg)
|
|
|
|
if ok {
|
|
|
|
msg.Answer(msg.Subject, sess)
|
|
|
|
} else {
|
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
}
|
2018-06-30 16:20:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 23:34:02 +02:00
|
|
|
|
|
|
|
func (ws *WebsocketServer) settingsHandler(logger *log.Entry, msg *websocket.Message) error {
|
|
|
|
session, ok := ws.IsLoggedIn(msg)
|
|
|
|
if !ok {
|
|
|
|
msg.Answer(msg.Subject, false)
|
2018-08-30 10:26:23 +02:00
|
|
|
logger.Warn("try set settings without login")
|
2018-08-24 23:34:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var setting Session
|
|
|
|
if err := mapstructure.Decode(msg.Body, &setting); err != nil {
|
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
logger.Warnf("not able to decode data: %s", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
setting.SessionID = session.SessionID
|
|
|
|
setting.Lastseen = session.Lastseen
|
|
|
|
|
|
|
|
err := ws.db.Save(&setting)
|
|
|
|
|
|
|
|
logger.Debug("done")
|
|
|
|
msg.Answer(msg.Subject, err.Error == nil)
|
|
|
|
return err.Error
|
|
|
|
}
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (ws *WebsocketServer) logoutHandler(logger *log.Entry, msg *websocket.Message) error {
|
2018-08-30 10:26:23 +02:00
|
|
|
if msg == nil || msg.Session == uuid.Nil {
|
|
|
|
logger.Warn("no session for this message detected")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
session := Session{
|
2018-07-26 14:14:23 +02:00
|
|
|
SessionID: msg.Session,
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
err := ws.db.First(&session)
|
2018-07-26 14:14:23 +02:00
|
|
|
if err.Error != nil {
|
2018-06-30 16:20:54 +02:00
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
logger.Warn("logout without login")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-10 17:33:39 +02:00
|
|
|
err = ws.db.Delete(&session)
|
2018-06-30 16:20:54 +02:00
|
|
|
logger.Debug("done")
|
2018-07-26 14:14:23 +02:00
|
|
|
msg.Answer(msg.Subject, err.Error == nil)
|
|
|
|
return err.Error
|
2018-06-30 16:20:54 +02:00
|
|
|
}
|