Archived
2
0

lint fix.

This commit is contained in:
Miroslav Šedivý 2021-10-05 23:10:10 +02:00
parent 1ff743c598
commit 9c014336ff
16 changed files with 76 additions and 46 deletions

View File

@ -47,7 +47,7 @@ func init() {
} }
if _, err := os.Stat(logs); os.IsNotExist(err) { 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") latest := filepath.Join(logs, "neko-latest.log")

View File

@ -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 { func (manager *BroadcastManager) Start() error {
if !manager.enabled || manager.IsActive() { if !manager.enabled || manager.IsActive() {
return nil return nil

View File

@ -63,10 +63,8 @@ func errResponse(input interface{}) *ErrResponse {
case *error: case *error:
e := err.(error) e := err.(error)
res.Details = e.Error() res.Details = e.Error()
break
default: default:
res.Details = fmt.Sprintf("%+v", err) res.Details = fmt.Sprintf("%+v", err)
break
} }
} }

View File

@ -3,7 +3,6 @@ package http
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"os" "os"
@ -32,21 +31,22 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server {
router.Use(middleware.Logger) // Log API request calls router.Use(middleware.Logger) // Log API request calls
router.Get("/ws", func(w http.ResponseWriter, r *http.Request) { 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) { router.Get("/stats", func(w http.ResponseWriter, r *http.Request) {
password := r.URL.Query().Get("pwd") password := r.URL.Query().Get("pwd")
isAdmin, err := webSocketHandler.IsAdmin(password) isAdmin, err := webSocketHandler.IsAdmin(password)
if err != nil { if err != nil {
w.WriteHeader(http.StatusForbidden) http.Error(w, err.Error(), http.StatusForbidden)
fmt.Fprint(w, err)
return return
} }
if !isAdmin { if !isAdmin {
w.WriteHeader(http.StatusUnauthorized) http.Error(w, "bad authorization", http.StatusUnauthorized)
fmt.Fprint(w, "bad authorization")
return return
} }
@ -59,7 +59,7 @@ func New(conf *config.Server, webSocketHandler types.WebSocketHandler) *Server {
}) })
router.Get("/health", func(w http.ResponseWriter, r *http.Request) { 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)) 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) { if _, err := os.Stat(conf.Static + r.URL.Path); !os.IsNotExist(err) {
fs.ServeHTTP(w, r) fs.ServeHTTP(w, r)
} else { } else {
w.WriteHeader(http.StatusNotFound) http.NotFound(w, r)
fmt.Fprint(w, "404 page not found")
} }
}) })

View File

@ -250,7 +250,7 @@ func (manager *RemoteManager) GetScreenSize() *types.ScreenSize {
} }
func (manager *RemoteManager) SetKeyboardLayout(layout string) { 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) { func (manager *RemoteManager) SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) {

View File

@ -141,7 +141,7 @@ func (manager *SessionManager) Members() []*types.Member {
return members return members
} }
func (manager *SessionManager) Destroy(id string) error { func (manager *SessionManager) Destroy(id string) {
manager.mu.Lock() manager.mu.Lock()
session, ok := manager.members[id] session, ok := manager.members[id]
if ok { if ok {
@ -154,11 +154,11 @@ func (manager *SessionManager) Destroy(id string) error {
manager.mu.Unlock() manager.mu.Unlock()
manager.emmiter.Emit("destroyed", id, session) manager.emmiter.Emit("destroyed", id, session)
return err manager.logger.Err(err).Str("session_id", id).Msg("destorying session")
return
} }
manager.mu.Unlock() manager.mu.Unlock()
return nil
} }
func (manager *SessionManager) Clear() error { func (manager *SessionManager) Clear() error {

View File

@ -1,6 +1,7 @@
package types package types
type BroadcastManager interface { type BroadcastManager interface {
Shutdown() error
Start() error Start() error
Stop() Stop()
IsActive() bool IsActive() bool

View File

@ -38,7 +38,7 @@ type SessionManager interface {
Get(id string) (Session, bool) Get(id string) (Session, bool)
Members() []*Member Members() []*Member
Admins() []*Member Admins() []*Member
Destroy(id string) error Destroy(id string)
Clear() error Clear() error
Broadcast(v interface{}, exclude interface{}) error Broadcast(v interface{}, exclude interface{}) error
OnHost(listener func(id string)) OnHost(listener func(id string))

View File

@ -15,7 +15,7 @@ type WebSocket interface {
} }
type WebSocketHandler interface { type WebSocketHandler interface {
Start() error Start()
Shutdown() error Shutdown() error
Upgrade(w http.ResponseWriter, r *http.Request) error Upgrade(w http.ResponseWriter, r *http.Request) error
Stats() Stats Stats() Stats

View File

@ -93,7 +93,7 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
settings.SetLite(true) 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.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
settings.SetICETimeouts(6*time.Second, 6*time.Second, 3*time.Second) settings.SetICETimeouts(6*time.Second, 6*time.Second, 3*time.Second)
settings.SetSRTPReplayProtectionWindow(512) settings.SetSRTPReplayProtectionWindow(512)
@ -101,8 +101,8 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
// Create MediaEngine based off sdp // Create MediaEngine based off sdp
engine := webrtc.MediaEngine{} engine := webrtc.MediaEngine{}
engine.RegisterCodec(manager.audioCodec, webrtc.RTPCodecTypeAudio) _ = engine.RegisterCodec(manager.audioCodec, webrtc.RTPCodecTypeAudio)
engine.RegisterCodec(manager.videoCodec, webrtc.RTPCodecTypeVideo) _ = engine.RegisterCodec(manager.videoCodec, webrtc.RTPCodecTypeVideo)
i := &interceptor.Registry{} i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(&engine, i); err != nil { if err := webrtc.RegisterDefaultInterceptors(&engine, i); err != nil {

View File

@ -66,7 +66,10 @@ func (h *MessageHandler) adminControl(id string, session types.Session) error {
host, ok := h.sessions.GetHost() host, ok := h.sessions.GetHost()
h.sessions.SetHost(id) err := h.sessions.SetHost(id)
if err != nil {
return err
}
if ok { if ok {
if err := h.sessions.Broadcast( if err := h.sessions.Broadcast(
@ -138,7 +141,10 @@ func (h *MessageHandler) adminGive(id string, session types.Session, payload *me
} }
// set host // set host
h.sessions.SetHost(payload.ID) err := h.sessions.SetHost(payload.ID)
if err != nil {
return err
}
// let everyone know // let everyone know
if err := h.sessions.Broadcast( if err := h.sessions.Broadcast(

View File

@ -35,7 +35,10 @@ func (h *MessageHandler) controlRequest(id string, session types.Session) error
// check for host // check for host
if !h.sessions.HasHost() { if !h.sessions.HasHost() {
// set host // set host
h.sessions.SetHost(id) err := h.sessions.SetHost(id)
if err != nil {
return err
}
// let everyone know // let everyone know
if err := h.sessions.Broadcast( if err := h.sessions.Broadcast(
@ -89,7 +92,10 @@ func (h *MessageHandler) controlGive(id string, session types.Session, payload *
} }
// set host // set host
h.sessions.SetHost(payload.ID) err := h.sessions.SetHost(payload.ID)
if err != nil {
return err
}
// let everyone know // let everyone know
if err := h.sessions.Broadcast( if err := h.sessions.Broadcast(

View File

@ -42,8 +42,8 @@ func (h *MessageHandler) Connected(admin bool, socket *WebSocket) (bool, string,
return true, "", nil return true, "", nil
} }
func (h *MessageHandler) Disconnected(id string) error { func (h *MessageHandler) Disconnected(id string) {
return h.sessions.Destroy(id) h.sessions.Destroy(id)
} }
func (h *MessageHandler) Message(id string, raw []byte) error { func (h *MessageHandler) Message(id string, raw []byte) error {

View File

@ -60,7 +60,7 @@ type WebSocketHandler struct {
conns uint32 conns uint32
} }
func (ws *WebSocketHandler) Start() error { func (ws *WebSocketHandler) Start() {
ws.sessions.OnCreated(func(id string, session types.Session) { ws.sessions.OnCreated(func(id string, session types.Session) {
if err := ws.handler.SessionCreated(id, session); err != nil { if err := ws.handler.SessionCreated(id, session); err != nil {
ws.logger.Warn().Str("id", id).Err(err).Msg("session created with and error") 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: case <-ws.shutdown:
return return
default: default:
if ws.sessions.HasHost() { time.Sleep(100 * time.Millisecond)
if !ws.sessions.HasHost() {
continue
}
text := ws.remote.ReadClipboard() text := ws.remote.ReadClipboard()
if text != current { if text == current {
continue
}
session, ok := ws.sessions.GetHost() session, ok := ws.sessions.GetHost()
if ok { if ok {
session.Send(message.Clipboard{ err := session.Send(message.Clipboard{
Event: event.CONTROL_CLIPBOARD, Event: event.CONTROL_CLIPBOARD,
Text: text, Text: text,
}) })
if err != nil {
ws.logger.Err(err).Msg("unable to synchronize clipboard")
} }
}
current = text current = text
} }
} }
time.Sleep(100 * time.Millisecond)
}
}
}() }()
return nil
} }
func (ws *WebSocketHandler) Shutdown() error { func (ws *WebSocketHandler) Shutdown() error {

View File

@ -129,12 +129,12 @@ func WriteClipboard(data string) {
func ResetKeys() { func ResetKeys() {
for code := range debounce_button { for code := range debounce_button {
ButtonUp(code) _ = ButtonUp(code)
delete(debounce_button, code) delete(debounce_button, code)
} }
for code := range debounce_key { for code := range debounce_key {
KeyUp(code) _ = KeyUp(code)
delete(debounce_key, code) delete(debounce_key, code)
} }
@ -146,7 +146,7 @@ func CheckKeys(duration time.Duration) {
if t.Sub(start) < duration { if t.Sub(start) < duration {
continue continue
} }
ButtonUp(code) _ = ButtonUp(code)
delete(debounce_button, code) delete(debounce_button, code)
} }
@ -154,7 +154,7 @@ func CheckKeys(duration time.Duration) {
if t.Sub(start) < duration { if t.Sub(start) < duration {
continue continue
} }
KeyUp(code) _ = KeyUp(code)
delete(debounce_key, code) delete(debounce_key, code)
} }

View File

@ -136,6 +136,7 @@ func (neko *Neko) Start() {
server := http.New(neko.Server, webSocketHandler) server := http.New(neko.Server, webSocketHandler)
server.Start() server.Start()
neko.broadcastManager = broadcastManager
neko.sessionManager = sessionManager neko.sessionManager = sessionManager
neko.remoteManager = remoteManager neko.remoteManager = remoteManager
neko.webRTCManager = webRTCManager neko.webRTCManager = webRTCManager
@ -144,6 +145,12 @@ func (neko *Neko) Start() {
} }
func (neko *Neko) Shutdown() { 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 { if err := neko.remoteManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("remote manager shutdown with an error") neko.logger.Err(err).Msg("remote manager shutdown with an error")
} else { } else {