websocket handler simplified logging.

This commit is contained in:
Miroslav Šedivý 2021-09-01 23:10:18 +02:00
parent c2fe224562
commit 5130d94bbc
8 changed files with 29 additions and 52 deletions

View File

@ -1,21 +1,19 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message.ClipboardData) error { func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message.ClipboardData) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.Profile().CanAccessClipboard { if !session.Profile().CanAccessClipboard {
logger.Debug().Msg("cannot access clipboard") return errors.New("cannot access clipboard")
return nil
} }
if !session.IsHost() { if !session.IsHost() {
logger.Debug().Msg("is not the host") return errors.New("is not the host")
return nil
} }
return h.desktop.ClipboardSetText(types.ClipboardText{ return h.desktop.ClipboardSetText(types.ClipboardText{

View File

@ -1,6 +1,8 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
@ -26,16 +28,12 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
} }
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.Profile().CanHost { if !session.Profile().CanHost {
logger.Debug().Msg("is not allowed to host") return errors.New("is not allowed to host")
return nil
} }
if session.IsHost() { if session.IsHost() {
logger.Debug().Msg("is already the host") return errors.New("is already the host")
return nil
} }
if !h.sessions.ImplicitHosting() { if !h.sessions.ImplicitHosting() {
@ -53,6 +51,5 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
} }
h.sessions.SetHost(session) h.sessions.SetHost(session)
return nil return nil
} }

View File

@ -113,7 +113,7 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM
} }
if err != nil { if err != nil {
logger.Error().Err(err).Msg("message handler has failed") logger.Warn().Err(err).Msg("message handler has failed")
} }
return true return true

View File

@ -1,16 +1,15 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) keyboardMap(session types.Session, payload *message.KeyboardMap) error { func (h *MessageHandlerCtx) keyboardMap(session types.Session, payload *message.KeyboardMap) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.IsHost() { if !session.IsHost() {
logger.Debug().Msg("is not the host") return errors.New("is not the host")
return nil
} }
return h.desktop.SetKeyboardMap(types.KeyboardMap{ return h.desktop.SetKeyboardMap(types.KeyboardMap{
@ -20,11 +19,8 @@ func (h *MessageHandlerCtx) keyboardMap(session types.Session, payload *message.
} }
func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error { func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.IsHost() { if !session.IsHost() {
logger.Debug().Msg("is not the host") return errors.New("is not the host")
return nil
} }
h.desktop.SetKeyboardModifiers(types.KeyboardModifiers{ h.desktop.SetKeyboardModifiers(types.KeyboardModifiers{

View File

@ -1,17 +1,16 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) screenSet(session types.Session, payload *message.ScreenSize) error { func (h *MessageHandlerCtx) screenSet(session types.Session, payload *message.ScreenSize) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.Profile().IsAdmin { if !session.Profile().IsAdmin {
logger.Debug().Msg("is not the admin") return errors.New("is not the admin")
return nil
} }
if err := h.desktop.SetScreenSize(types.ScreenSize{ if err := h.desktop.SetScreenSize(types.ScreenSize{

View File

@ -1,18 +1,17 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) sendUnicast(session types.Session, payload *message.SendUnicast) error { func (h *MessageHandlerCtx) sendUnicast(session types.Session, payload *message.SendUnicast) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
receiver, ok := h.sessions.Get(payload.Receiver) receiver, ok := h.sessions.Get(payload.Receiver)
if !ok { if !ok {
logger.Debug().Msg("receiver session ID not found") return errors.New("receiver session ID not found")
return nil
} }
receiver.Send( receiver.Send(

View File

@ -1,17 +1,16 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *message.SignalVideo) error { func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *message.SignalVideo) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
if !session.Profile().CanWatch { if !session.Profile().CanWatch {
logger.Debug().Msg("not allowed to watch") return errors.New("not allowed to watch")
return nil
} }
// use default first video, if not provided // use default first video, if not provided
@ -37,12 +36,9 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *messag
} }
func (h *MessageHandlerCtx) signalRestart(session types.Session) error { func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
peer := session.GetWebRTCPeer() peer := session.GetWebRTCPeer()
if peer == nil { if peer == nil {
logger.Debug().Msg("webRTC peer does not exist") return errors.New("webRTC peer does not exist")
return nil
} }
offer, err := peer.CreateOffer(true) offer, err := peer.CreateOffer(true)
@ -60,36 +56,27 @@ func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
} }
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error { func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
peer := session.GetWebRTCPeer() peer := session.GetWebRTCPeer()
if peer == nil { if peer == nil {
logger.Debug().Msg("webRTC peer does not exist") return errors.New("webRTC peer does not exist")
return nil
} }
return peer.SignalAnswer(payload.SDP) return peer.SignalAnswer(payload.SDP)
} }
func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *message.SignalCandidate) error { func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *message.SignalCandidate) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
peer := session.GetWebRTCPeer() peer := session.GetWebRTCPeer()
if peer == nil { if peer == nil {
logger.Debug().Msg("webRTC peer does not exist") return errors.New("webRTC peer does not exist")
return nil
} }
return peer.SignalCandidate(payload.ICECandidateInit) return peer.SignalCandidate(payload.ICECandidateInit)
} }
func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.SignalVideo) error { func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.SignalVideo) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger()
peer := session.GetWebRTCPeer() peer := session.GetWebRTCPeer()
if peer == nil { if peer == nil {
logger.Debug().Msg("webRTC peer does not exist") return errors.New("webRTC peer does not exist")
return nil
} }
err := peer.SetVideoID(payload.Video) err := peer.SetVideoID(payload.Video)

View File

@ -1,6 +1,8 @@
package handler package handler
import ( import (
"errors"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/types/event" "demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
@ -19,8 +21,7 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
size := h.desktop.GetScreenSize() size := h.desktop.GetScreenSize()
if size == nil { if size == nil {
h.logger.Warn().Msg("could not get screen size") return errors.New("could not get screen size")
return nil
} }
sessions := map[string]message.SessionData{} sessions := map[string]message.SessionData{}