From 9c014336ffe899ead148703b13aadcb0943ade63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Tue, 5 Oct 2021 23:10:10 +0200 Subject: [PATCH] lint fix. --- server/cmd/root.go | 2 +- server/internal/broadcast/manager.go | 5 +++ server/internal/http/endpoint/endpoint.go | 2 -- server/internal/http/http.go | 17 +++++----- server/internal/remote/manager.go | 2 +- server/internal/session/manager.go | 6 ++-- server/internal/types/broadcast.go | 1 + server/internal/types/session.go | 2 +- server/internal/types/websocket.go | 2 +- server/internal/webrtc/webrtc.go | 6 ++-- server/internal/websocket/admin.go | 10 ++++-- server/internal/websocket/control.go | 10 ++++-- server/internal/websocket/handler.go | 4 +-- server/internal/websocket/websocket.go | 38 ++++++++++++++--------- server/internal/xorg/xorg.go | 8 ++--- server/neko.go | 7 +++++ 16 files changed, 76 insertions(+), 46 deletions(-) diff --git a/server/cmd/root.go b/server/cmd/root.go index d939802..d402f3c 100644 --- a/server/cmd/root.go +++ b/server/cmd/root.go @@ -47,7 +47,7 @@ func init() { } if _, err := os.Stat(logs); os.IsNotExist(err) { - os.Mkdir(logs, os.ModePerm) + _ = os.Mkdir(logs, os.ModePerm) } latest := filepath.Join(logs, "neko-latest.log") diff --git a/server/internal/broadcast/manager.go b/server/internal/broadcast/manager.go index 8af58e2..98b5ab1 100644 --- a/server/internal/broadcast/manager.go +++ b/server/internal/broadcast/manager.go @@ -30,6 +30,11 @@ func New(remote *config.Remote, config *config.Broadcast) *BroadcastManager { } } +func (manager *BroadcastManager) Shutdown() error { + manager.Destroy() + return nil +} + func (manager *BroadcastManager) Start() error { if !manager.enabled || manager.IsActive() { return nil diff --git a/server/internal/http/endpoint/endpoint.go b/server/internal/http/endpoint/endpoint.go index 0018523..714ea0d 100644 --- a/server/internal/http/endpoint/endpoint.go +++ b/server/internal/http/endpoint/endpoint.go @@ -63,10 +63,8 @@ func errResponse(input interface{}) *ErrResponse { case *error: e := err.(error) res.Details = e.Error() - break default: res.Details = fmt.Sprintf("%+v", err) - break } } diff --git a/server/internal/http/http.go b/server/internal/http/http.go index b7e508d..355f33c 100644 --- a/server/internal/http/http.go +++ b/server/internal/http/http.go @@ -3,7 +3,6 @@ package http import ( "context" "encoding/json" - "fmt" "net/http" "os" @@ -32,21 +31,22 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server { router.Use(middleware.Logger) // Log API request calls router.Get("/ws", func(w http.ResponseWriter, r *http.Request) { - webSocketHandler.Upgrade(w, r) + err := webSocketHandler.Upgrade(w, r) + if err != nil { + logger.Warn().Err(err).Msg("failed to upgrade websocket conection") + } }) router.Get("/stats", func(w http.ResponseWriter, r *http.Request) { password := r.URL.Query().Get("pwd") isAdmin, err := webSocketHandler.IsAdmin(password) if err != nil { - w.WriteHeader(http.StatusForbidden) - fmt.Fprint(w, err) + http.Error(w, err.Error(), http.StatusForbidden) return } if !isAdmin { - w.WriteHeader(http.StatusUnauthorized) - fmt.Fprint(w, "bad authorization") + http.Error(w, "bad authorization", http.StatusUnauthorized) return } @@ -59,7 +59,7 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server { }) router.Get("/health", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("true")) + _, _ = w.Write([]byte("true")) }) fs := http.FileServer(http.Dir(conf.Static)) @@ -67,8 +67,7 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server { if _, err := os.Stat(conf.Static + r.URL.Path); !os.IsNotExist(err) { fs.ServeHTTP(w, r) } else { - w.WriteHeader(http.StatusNotFound) - fmt.Fprint(w, "404 page not found") + http.NotFound(w, r) } }) diff --git a/server/internal/remote/manager.go b/server/internal/remote/manager.go index 20aba63..af355ee 100644 --- a/server/internal/remote/manager.go +++ b/server/internal/remote/manager.go @@ -250,7 +250,7 @@ func (manager *RemoteManager) GetScreenSize() *types.ScreenSize { } func (manager *RemoteManager) SetKeyboardLayout(layout string) { - exec.Command("setxkbmap", layout).Run() + _ = exec.Command("setxkbmap", layout).Run() } func (manager *RemoteManager) SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) { diff --git a/server/internal/session/manager.go b/server/internal/session/manager.go index 6f6be09..cd17d61 100644 --- a/server/internal/session/manager.go +++ b/server/internal/session/manager.go @@ -141,7 +141,7 @@ func (manager *SessionManager) Members() []*types.Member { return members } -func (manager *SessionManager) Destroy(id string) error { +func (manager *SessionManager) Destroy(id string) { manager.mu.Lock() session, ok := manager.members[id] if ok { @@ -154,11 +154,11 @@ func (manager *SessionManager) Destroy(id string) error { manager.mu.Unlock() manager.emmiter.Emit("destroyed", id, session) - return err + manager.logger.Err(err).Str("session_id", id).Msg("destorying session") + return } manager.mu.Unlock() - return nil } func (manager *SessionManager) Clear() error { diff --git a/server/internal/types/broadcast.go b/server/internal/types/broadcast.go index 896bed1..069c101 100644 --- a/server/internal/types/broadcast.go +++ b/server/internal/types/broadcast.go @@ -1,6 +1,7 @@ package types type BroadcastManager interface { + Shutdown() error Start() error Stop() IsActive() bool diff --git a/server/internal/types/session.go b/server/internal/types/session.go index 7940f81..9820584 100644 --- a/server/internal/types/session.go +++ b/server/internal/types/session.go @@ -38,7 +38,7 @@ type SessionManager interface { Get(id string) (Session, bool) Members() []*Member Admins() []*Member - Destroy(id string) error + Destroy(id string) Clear() error Broadcast(v interface{}, exclude interface{}) error OnHost(listener func(id string)) diff --git a/server/internal/types/websocket.go b/server/internal/types/websocket.go index c884c77..a2b6e6e 100644 --- a/server/internal/types/websocket.go +++ b/server/internal/types/websocket.go @@ -15,7 +15,7 @@ type WebSocket interface { } type WebSocketHandler interface { - Start() error + Start() Shutdown() error Upgrade(w http.ResponseWriter, r *http.Request) error Stats() Stats diff --git a/server/internal/webrtc/webrtc.go b/server/internal/webrtc/webrtc.go index 7c4f0b7..230dd40 100644 --- a/server/internal/webrtc/webrtc.go +++ b/server/internal/webrtc/webrtc.go @@ -93,7 +93,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri settings.SetLite(true) } - settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax) + _ = settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax) settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost) settings.SetICETimeouts(6*time.Second, 6*time.Second, 3*time.Second) settings.SetSRTPReplayProtectionWindow(512) @@ -101,8 +101,8 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri // Create MediaEngine based off sdp engine := webrtc.MediaEngine{} - engine.RegisterCodec(manager.audioCodec, webrtc.RTPCodecTypeAudio) - engine.RegisterCodec(manager.videoCodec, webrtc.RTPCodecTypeVideo) + _ = engine.RegisterCodec(manager.audioCodec, webrtc.RTPCodecTypeAudio) + _ = engine.RegisterCodec(manager.videoCodec, webrtc.RTPCodecTypeVideo) i := &interceptor.Registry{} if err := webrtc.RegisterDefaultInterceptors(&engine, i); err != nil { diff --git a/server/internal/websocket/admin.go b/server/internal/websocket/admin.go index 3153471..e74ba61 100644 --- a/server/internal/websocket/admin.go +++ b/server/internal/websocket/admin.go @@ -66,7 +66,10 @@ func (h *MessageHandler) adminControl(id string, session types.Session) error { host, ok := h.sessions.GetHost() - h.sessions.SetHost(id) + err := h.sessions.SetHost(id) + if err != nil { + return err + } if ok { if err := h.sessions.Broadcast( @@ -138,7 +141,10 @@ func (h *MessageHandler) adminGive(id string, session types.Session, payload *me } // set host - h.sessions.SetHost(payload.ID) + err := h.sessions.SetHost(payload.ID) + if err != nil { + return err + } // let everyone know if err := h.sessions.Broadcast( diff --git a/server/internal/websocket/control.go b/server/internal/websocket/control.go index ff783b1..824d7df 100644 --- a/server/internal/websocket/control.go +++ b/server/internal/websocket/control.go @@ -35,7 +35,10 @@ func (h *MessageHandler) controlRequest(id string, session types.Session) error // check for host if !h.sessions.HasHost() { // set host - h.sessions.SetHost(id) + err := h.sessions.SetHost(id) + if err != nil { + return err + } // let everyone know if err := h.sessions.Broadcast( @@ -89,7 +92,10 @@ func (h *MessageHandler) controlGive(id string, session types.Session, payload * } // set host - h.sessions.SetHost(payload.ID) + err := h.sessions.SetHost(payload.ID) + if err != nil { + return err + } // let everyone know if err := h.sessions.Broadcast( diff --git a/server/internal/websocket/handler.go b/server/internal/websocket/handler.go index aad245d..a52520e 100644 --- a/server/internal/websocket/handler.go +++ b/server/internal/websocket/handler.go @@ -42,8 +42,8 @@ func (h *MessageHandler) Connected(admin bool, socket *WebSocket) (bool, string, return true, "", nil } -func (h *MessageHandler) Disconnected(id string) error { - return h.sessions.Destroy(id) +func (h *MessageHandler) Disconnected(id string) { + h.sessions.Destroy(id) } func (h *MessageHandler) Message(id string, raw []byte) error { diff --git a/server/internal/websocket/websocket.go b/server/internal/websocket/websocket.go index 3e2cd57..7f4dbda 100644 --- a/server/internal/websocket/websocket.go +++ b/server/internal/websocket/websocket.go @@ -60,7 +60,7 @@ type WebSocketHandler struct { conns uint32 } -func (ws *WebSocketHandler) Start() error { +func (ws *WebSocketHandler) Start() { ws.sessions.OnCreated(func(id string, session types.Session) { if err := ws.handler.SessionCreated(id, session); err != nil { ws.logger.Warn().Str("id", id).Err(err).Msg("session created with and error") @@ -99,25 +99,33 @@ func (ws *WebSocketHandler) Start() error { case <-ws.shutdown: return default: - if ws.sessions.HasHost() { - text := ws.remote.ReadClipboard() - if text != current { - session, ok := ws.sessions.GetHost() - if ok { - session.Send(message.Clipboard{ - Event: event.CONTROL_CLIPBOARD, - Text: text, - }) - } - current = text + time.Sleep(100 * time.Millisecond) + + if !ws.sessions.HasHost() { + continue + } + + text := ws.remote.ReadClipboard() + if text == current { + continue + } + + session, ok := ws.sessions.GetHost() + if ok { + err := session.Send(message.Clipboard{ + Event: event.CONTROL_CLIPBOARD, + Text: text, + }) + + if err != nil { + ws.logger.Err(err).Msg("unable to synchronize clipboard") } } - time.Sleep(100 * time.Millisecond) + + current = text } } }() - - return nil } func (ws *WebSocketHandler) Shutdown() error { diff --git a/server/internal/xorg/xorg.go b/server/internal/xorg/xorg.go index e958ce8..874bf4c 100644 --- a/server/internal/xorg/xorg.go +++ b/server/internal/xorg/xorg.go @@ -129,12 +129,12 @@ func WriteClipboard(data string) { func ResetKeys() { for code := range debounce_button { - ButtonUp(code) + _ = ButtonUp(code) delete(debounce_button, code) } for code := range debounce_key { - KeyUp(code) + _ = KeyUp(code) delete(debounce_key, code) } @@ -146,7 +146,7 @@ func CheckKeys(duration time.Duration) { if t.Sub(start) < duration { continue } - ButtonUp(code) + _ = ButtonUp(code) delete(debounce_button, code) } @@ -154,7 +154,7 @@ func CheckKeys(duration time.Duration) { if t.Sub(start) < duration { continue } - KeyUp(code) + _ = KeyUp(code) delete(debounce_key, code) } diff --git a/server/neko.go b/server/neko.go index ed91be8..b44bd42 100644 --- a/server/neko.go +++ b/server/neko.go @@ -136,6 +136,7 @@ func (neko *Neko) Start() { server := http.New(neko.Server, webSocketHandler) server.Start() + neko.broadcastManager = broadcastManager neko.sessionManager = sessionManager neko.remoteManager = remoteManager neko.webRTCManager = webRTCManager @@ -144,6 +145,12 @@ func (neko *Neko) Start() { } func (neko *Neko) Shutdown() { + if err := neko.broadcastManager.Shutdown(); err != nil { + neko.logger.Err(err).Msg("broadcast manager shutdown with an error") + } else { + neko.logger.Debug().Msg("broadcast manager shutdown") + } + if err := neko.remoteManager.Shutdown(); err != nil { neko.logger.Err(err).Msg("remote manager shutdown with an error") } else {