8
8
"net/http"
9
9
"os"
10
10
"sync"
11
+ "strconv"
11
12
12
13
"github.com/gin-gonic/gin"
13
14
"github.com/gorilla/websocket"
@@ -140,7 +141,11 @@ func (h *Hub) Run() {
140
141
}
141
142
142
143
// ServeWs handles WebSocket requests
143
- func serveWs (hub * Hub , c * gin.Context , roomMappings * verify.RoomMappings ) {
144
+ func serveWs (
145
+ hub * Hub , c * gin.Context ,
146
+ roomMappings * verify.RoomMappings ,
147
+ persistMappings * verify.PersistMappings ,
148
+ ) {
144
149
roomID := c .Query ("roomID" )
145
150
if roomID == "" {
146
151
http .Error (c .Writer , "roomID required" , http .StatusBadRequest )
@@ -156,19 +161,40 @@ func serveWs(hub *Hub, c *gin.Context, roomMappings *verify.RoomMappings) {
156
161
client := & Client {conn : conn , roomID : roomID }
157
162
hub .register <- client
158
163
159
- go handleMessages (client , hub , roomMappings )
164
+ go handleMessages (client , hub , roomMappings , persistMappings )
160
165
}
161
166
162
- func authenticateClient (token string , client * Client , roomMappings * verify.RoomMappings ) bool {
167
+ func authenticateClient (
168
+ token string , match string , client * Client ,
169
+ roomMappings * verify.RoomMappings ,
170
+ persistMappings * verify.PersistMappings ,
171
+ ) bool {
163
172
ok , userID := verifyToken (token )
164
173
if ! ok {
165
- log .Println ("bruh " )
174
+ log .Println ("bad token in request " )
166
175
return false
167
176
}
168
- return verify .VerifyRoom (roomMappings , client .roomID , userID )
177
+ return verify .VerifyRoomAndMoveToPersist (
178
+ roomMappings , client .roomID , userID , match , persistMappings )
169
179
}
170
180
171
- func handleMessages (client * Client , hub * Hub , roomMappings * verify.RoomMappings ) {
181
+ func authenticateClientNoMatch (
182
+ token string , client * Client ,
183
+ persistMappings * verify.PersistMappings ,
184
+ ) bool {
185
+ ok , userID := verifyToken (token )
186
+ if ! ok {
187
+ log .Println ("bad token in request" )
188
+ return false
189
+ }
190
+ return verify .VerifyPersist (persistMappings , client .roomID , userID )
191
+ }
192
+
193
+ func handleMessages (
194
+ client * Client , hub * Hub ,
195
+ roomMappings * verify.RoomMappings ,
196
+ persistMappings * verify.PersistMappings ,
197
+ ) {
172
198
defer func () {
173
199
hub .unregister <- client
174
200
}()
@@ -187,16 +213,41 @@ func handleMessages(client *Client, hub *Hub, roomMappings *verify.RoomMappings)
187
213
}
188
214
// Handle authentication message
189
215
if msgData ["type" ] == "auth" {
190
- token , ok := msgData ["token" ].(string )
191
- if ! ok || ! authenticateClient (token , client , roomMappings ) {
192
- log .Println ("Authentication failed" )
193
- client .conn .WriteMessage (websocket .TextMessage , []byte ("Authentication failed" ))
194
- client .conn .Close ()
195
- break
196
- }
197
- client .authenticated = true
198
- log .Println ("Client authenticated successfully" )
199
- }
216
+ token , tokenOk := msgData ["token" ].(string )
217
+ if ! tokenOk {
218
+ log .Println ("Authentication failed - no token attached" )
219
+ client .conn .WriteMessage (
220
+ websocket .TextMessage ,
221
+ []byte ("Authentication failed - no JWT token" ),
222
+ )
223
+ client .conn .Close ()
224
+ break
225
+ }
226
+ isSuccess := false
227
+ match , matchOk := msgData ["matchHash" ].(string )
228
+ if matchOk && ! authenticateClient (token , match , client , roomMappings , persistMappings ) {
229
+ log .Println (
230
+ "failed to find a matching room from match hash, proceeding with persistence check" ,
231
+ )
232
+ }
233
+ // I will ping the persistent map even if I've found it in the original map
234
+ if ! authenticateClientNoMatch (token , client , persistMappings ) {
235
+ log .Println ("failed to find a persistent room" )
236
+ isSuccess = false
237
+ } else {
238
+ isSuccess = true
239
+ }
240
+ if ! isSuccess {
241
+ client .conn .WriteMessage (
242
+ websocket .TextMessage ,
243
+ []byte ("Authentication failed - failed to find a matching room" ),
244
+ )
245
+ client .conn .Close ()
246
+ break
247
+ }
248
+ client .authenticated = true
249
+ log .Println ("Client authenticated successfully" )
250
+ }
200
251
201
252
if msgData ["type" ] == "close_session" {
202
253
closeMessage := Message {
@@ -250,11 +301,34 @@ func main() {
250
301
if REDIS_URI == "" {
251
302
REDIS_URI = "localhost:9190"
252
303
}
253
- roomMappings := verify .InitialiseRoomMappings (REDIS_URI , 1 )
304
+
305
+ REDIS_ROOM_MAPPING := 1
306
+ REDIS_ROOM_PERSIST := 2
307
+
308
+ if os .Getenv ("REDIS_ROOM_MAPPING" ) != "" {
309
+ num , err := strconv .Atoi (os .Getenv ("REDIS_ROOM_MAPPING" ))
310
+ if err != nil {
311
+ log .Fatal ("DB no of room map is badly formatted" + err .Error ())
312
+ } else {
313
+ REDIS_ROOM_MAPPING = num
314
+ }
315
+ }
316
+
317
+ if os .Getenv ("REDIS_ROOM_PERSIST" ) != "" {
318
+ num , err := strconv .Atoi (os .Getenv ("REDIS_ROOM_PERSIST" ))
319
+ if err != nil {
320
+ log .Fatal ("DB no of room persistance store is badly formatted" + err .Error ())
321
+ } else {
322
+ REDIS_ROOM_PERSIST = num
323
+ }
324
+ }
325
+
326
+ roomMappings := verify .InitialiseRoomMappings (REDIS_URI , REDIS_ROOM_MAPPING )
327
+ persistMappings := verify .InitialisePersistMappings (REDIS_URI , REDIS_ROOM_PERSIST );
254
328
255
329
// WebSocket connection endpoint
256
330
r .GET ("/ws" , func (c * gin.Context ) {
257
- serveWs (hub , c , roomMappings )
331
+ serveWs (hub , c , roomMappings , persistMappings )
258
332
})
259
333
260
334
// Status endpoint
0 commit comments