removed user request and added user connection init to ws

main
alex 2023-04-21 11:45:30 +02:00
parent 4374167998
commit 74188bc007
5 changed files with 52 additions and 24 deletions

12
main.go
View File

@ -53,24 +53,20 @@ func main() {
database.DB.First(&userSession, "id = ?", sessionId) database.DB.First(&userSession, "id = ?", sessionId)
if userSession.Id == "" { if userSession.Id != "" {
return c.SendStatus(fiber.StatusUnauthorized)
}
log.Info().Msg("session id: " + userSession.Id + " user: " + userSession.Id) log.Info().Msg("session id: " + userSession.Id + " user: " + userSession.Id)
var user structs.User var user structs.User
database.DB.First(&user, "id = ?", userSession.UserId) database.DB.First(&user, "id = ?", userSession.UserId)
if user.Id == "" { if user.Id != "" {
return c.SendStatus(fiber.StatusInternalServerError)
}
log.Info().Msg("user " + user.Id + user.Username) log.Info().Msg("user " + user.Id + user.Username)
c.Locals("sessionId", sessionId) c.Locals("sessionId", sessionId)
c.Locals("userId", user.Id) c.Locals("userId", user.Id)
}
}
return c.Next() return c.Next()
} }

View File

@ -35,6 +35,10 @@ func (socketClient *SocketClient) SendCloseMessage() error {
return socketClient.writeMessage(websocket.CloseMessage, SendSocketMessage{}, true) return socketClient.writeMessage(websocket.CloseMessage, SendSocketMessage{}, true)
} }
func (socketClient *SocketClient) SendUnauthorizedCloseMessage() error {
return socketClient.writeMessage(websocket.CloseMessage, SendSocketMessage{}, true)
}
func (socketClient *SocketClient) SendMessage(message SendSocketMessage) error { func (socketClient *SocketClient) SendMessage(message SendSocketMessage) error {
return socketClient.writeMessage(websocket.TextMessage, message, false) return socketClient.writeMessage(websocket.TextMessage, message, false)
} }
@ -44,7 +48,8 @@ func (socketClient *SocketClient) writeMessage(messageType int, message SendSock
var err error var err error
if closeMessage { if closeMessage {
//marshaledMessage = websocket.FormatCloseMessage(utils.WsCloseCodeNewConnectionWasMade, "") // Status codes in the range 4000-4999 are reserved for private use
marshaledMessage = websocket.FormatCloseMessage(4001, "")
} else { } else {
marshaledMessage, err = json.Marshal(message) marshaledMessage, err = json.Marshal(message)
@ -71,3 +76,8 @@ func (socketClient *SocketClient) writeMessage(messageType int, message SendSock
return nil return nil
} }
type InitUserSocketConnection struct {
Username string
Email string
}

View File

@ -14,6 +14,12 @@ const (
HeaderXAuthorization = "X-Authorization" HeaderXAuthorization = "X-Authorization"
) )
// commands sent to web clients
const (
SentInitUserSocketConnection = 1
SentCmdUpdateConnectedUsers = 2
)
var ( var (
generalRules = map[string]string{ generalRules = map[string]string{
"Username": "required,min=" + minUsername + ",max=" + maxUsername, "Username": "required,min=" + minUsername + ",max=" + maxUsername,

View File

@ -3,11 +3,7 @@ package socketclients
import ( import (
"janex/admin-dashboard-backend/modules/cache" "janex/admin-dashboard-backend/modules/cache"
"janex/admin-dashboard-backend/modules/structs" "janex/admin-dashboard-backend/modules/structs"
) "janex/admin-dashboard-backend/modules/utils"
// commands sent to web clients
const (
SentCmdUpdateConnectedUsers = 1
) )
func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) { func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) {
@ -18,7 +14,7 @@ func BroadcastMessage(sendSocketMessage structs.SendSocketMessage) {
func UpdateConnectedUsers() { func UpdateConnectedUsers() {
BroadcastMessage(structs.SendSocketMessage{ BroadcastMessage(structs.SendSocketMessage{
Cmd: SentCmdUpdateConnectedUsers, Cmd: utils.SentCmdUpdateConnectedUsers,
Body: len(cache.GetSocketClients()), Body: len(cache.GetSocketClients()),
}) })
} }

View File

@ -4,7 +4,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"janex/admin-dashboard-backend/modules/cache" "janex/admin-dashboard-backend/modules/cache"
"janex/admin-dashboard-backend/modules/database"
"janex/admin-dashboard-backend/modules/structs" "janex/admin-dashboard-backend/modules/structs"
"janex/admin-dashboard-backend/modules/utils"
"janex/admin-dashboard-backend/socketclients" "janex/admin-dashboard-backend/socketclients"
"github.com/gofiber/websocket/v2" "github.com/gofiber/websocket/v2"
@ -22,6 +24,12 @@ func RunHub() {
userId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("userId")) userId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("userId"))
sessionId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("sessionId")) sessionId := fmt.Sprintf("%v", newSocketClient.Conn.Locals("sessionId"))
// close connection instantly if sessionId is empty
if sessionId == "<nil>" {
newSocketClient.SendUnauthorizedCloseMessage()
continue
}
newSocketClient.SessionId = sessionId newSocketClient.SessionId = sessionId
newSocketClient.UserId = userId newSocketClient.UserId = userId
@ -30,6 +38,18 @@ func RunHub() {
log.Debug().Msgf("clients: %d", len(cache.GetSocketClients())) log.Debug().Msgf("clients: %d", len(cache.GetSocketClients()))
log.Debug().Msgf("REGISTER CLIENT: %s", sessionId) log.Debug().Msgf("REGISTER CLIENT: %s", sessionId)
var user structs.User
database.DB.First(&user, "id = ?", userId)
newSocketClient.SendMessage(structs.SendSocketMessage{
Cmd: utils.SentInitUserSocketConnection,
Body: structs.InitUserSocketConnection{
Username: user.Username,
Email: user.Email,
},
})
socketclients.UpdateConnectedUsers() socketclients.UpdateConnectedUsers()
case data := <-broadcast: case data := <-broadcast: