WS upgrade no returning error.

This commit is contained in:
Miroslav Šedivý 2021-03-16 10:45:22 +01:00
parent 971e3bff4e
commit 58d029f455
3 changed files with 26 additions and 12 deletions

View File

@ -44,7 +44,6 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
router.Route("/api", ApiManager.Route) router.Route("/api", ApiManager.Route)
router.Get("/api/ws", func(w http.ResponseWriter, r *http.Request) { router.Get("/api/ws", func(w http.ResponseWriter, r *http.Request) {
//nolint
WebSocketManager.Upgrade(w, r, func(r *http.Request) bool { WebSocketManager.Upgrade(w, r, func(r *http.Request) bool {
return conf.AllowOrigin(r.Header.Get("Origin")) return conf.AllowOrigin(r.Header.Get("Origin"))
}) })

View File

@ -15,5 +15,5 @@ type WebSocketManager interface {
Start() Start()
Shutdown() error Shutdown() error
AddHandler(handler HandlerFunction) AddHandler(handler HandlerFunction)
Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin CheckOrigin) error Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin CheckOrigin)
} }

View File

@ -138,7 +138,7 @@ func (manager *WebSocketManagerCtx) AddHandler(handler types.HandlerFunction) {
manager.handlers = append(manager.handlers, handler) manager.handlers = append(manager.handlers, handler)
} }
func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin types.CheckOrigin) error { func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin types.CheckOrigin) {
manager.logger.Debug().Msg("attempting to upgrade connection") manager.logger.Debug().Msg("attempting to upgrade connection")
upgrader := websocket.Upgrader{ upgrader := websocket.Upgrader{
@ -148,12 +148,12 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
connection, err := upgrader.Upgrade(w, r, nil) connection, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
manager.logger.Error().Err(err).Msg("failed to upgrade connection") manager.logger.Error().Err(err).Msg("failed to upgrade connection")
return err return
} }
session, err := manager.sessions.Authenticate(r) session, err := manager.sessions.Authenticate(r)
if err != nil { if err != nil {
manager.logger.Warn().Err(err).Msg("authentication failed") manager.logger.Debug().Err(err).Msg("authentication failed")
// TODO: Refactor, return error code. // TODO: Refactor, return error code.
if err = connection.WriteJSON( if err = connection.WriteJSON(
@ -161,36 +161,52 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
Event: event.SYSTEM_DISCONNECT, Event: event.SYSTEM_DISCONNECT,
Message: err.Error(), Message: err.Error(),
}); err != nil { }); err != nil {
manager.logger.Error().Err(err).Msg("failed to send disconnect") manager.logger.Error().Err(err).Msg("failed to send disconnect event")
} }
return connection.Close() if err := connection.Close(); err != nil {
manager.logger.Warn().Err(err).Msg("connection closed with an error")
}
return
} }
if !session.Profile().CanConnect { if !session.Profile().CanConnect {
manager.logger.Debug().Str("session_id", session.ID()).Msg("connection disabled")
// TODO: Refactor, return error code. // TODO: Refactor, return error code.
if err = connection.WriteJSON( if err = connection.WriteJSON(
message.SystemDisconnect{ message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT, Event: event.SYSTEM_DISCONNECT,
Message: "connection disabled", Message: "connection disabled",
}); err != nil { }); err != nil {
manager.logger.Error().Err(err).Msg("failed to send disconnect") manager.logger.Error().Err(err).Msg("failed to send disconnect event")
} }
return connection.Close() if err := connection.Close(); err != nil {
manager.logger.Warn().Err(err).Msg("connection closed with an error")
}
return
} }
if session.State().IsConnected { if session.State().IsConnected {
manager.logger.Debug().Str("session_id", session.ID()).Msg("already connected")
// TODO: Refactor, return error code. // TODO: Refactor, return error code.
if err = connection.WriteJSON( if err = connection.WriteJSON(
message.SystemDisconnect{ message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT, Event: event.SYSTEM_DISCONNECT,
Message: "already connected", Message: "already connected",
}); err != nil { }); err != nil {
manager.logger.Error().Err(err).Msg("failed to send disconnect") manager.logger.Error().Err(err).Msg("failed to send disconnect event")
} }
return connection.Close() if err := connection.Close(); err != nil {
manager.logger.Warn().Err(err).Msg("connection closed with an error")
}
return
} }
session.SetWebSocketPeer(&WebSocketPeerCtx{ session.SetWebSocketPeer(&WebSocketPeerCtx{
@ -218,7 +234,6 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
}() }()
manager.handle(connection, session) manager.handle(connection, session)
return nil
} }
func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.Session) { func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.Session) {