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"
@@ -142,8 +143,11 @@ func (h *Hub) Run() {
142
143
}
143
144
144
145
// ServeWs handles WebSocket requests
145
- func serveWs (hub * Hub , c * gin.Context , roomMappings * verify.RoomMappings ) {
146
-
146
+ func serveWs (
147
+ hub * Hub , c * gin.Context ,
148
+ roomMappings * verify.RoomMappings ,
149
+ persistMappings * verify.PersistMappings ,
150
+ ) {
147
151
log .Println ("handler called!" )
148
152
roomID := c .Query ("roomID" )
149
153
if roomID == "" {
@@ -160,20 +164,40 @@ func serveWs(hub *Hub, c *gin.Context, roomMappings *verify.RoomMappings) {
160
164
client := & Client {conn : conn , roomID : roomID }
161
165
hub .register <- client
162
166
163
- go handleMessages (client , hub , roomMappings )
167
+ go handleMessages (client , hub , roomMappings , persistMappings )
168
+ }
169
+
170
+ func authenticateClient (
171
+ token string , match string , client * Client ,
172
+ roomMappings * verify.RoomMappings ,
173
+ persistMappings * verify.PersistMappings ,
174
+ ) bool {
175
+ ok , userID := verifyToken (token )
176
+ if ! ok {
177
+ log .Println ("bad token in request" )
178
+ return false
179
+ }
180
+ return verify .VerifyRoomAndMoveToPersist (
181
+ roomMappings , client .roomID , userID , match , persistMappings )
164
182
}
165
183
166
- func authenticateClient (token string , client * Client , roomMappings * verify.RoomMappings ) bool {
184
+ func authenticateClientNoMatch (
185
+ token string , client * Client ,
186
+ persistMappings * verify.PersistMappings ,
187
+ ) bool {
167
188
ok , userID := verifyToken (token )
168
189
if ! ok {
169
- log .Println ("bruh " )
190
+ log .Println ("bad token in request " )
170
191
return false
171
192
}
172
- return verify .VerifyRoom ( roomMappings , client .roomID , userID )
193
+ return verify .VerifyPersist ( persistMappings , client .roomID , userID )
173
194
}
174
195
175
- func handleMessages (client * Client , hub * Hub , roomMappings * verify.RoomMappings ) {
176
- defer func () {
196
+ func handleMessages (
197
+ client * Client , hub * Hub ,
198
+ roomMappings * verify.RoomMappings ,
199
+ persistMappings * verify.PersistMappings ,
200
+ ) { defer func () {
177
201
hub .unregister <- client
178
202
}()
179
203
@@ -189,12 +213,36 @@ func handleMessages(client *Client, hub *Hub, roomMappings *verify.RoomMappings)
189
213
log .Printf ("Failed to parse message: %v" , err )
190
214
continue
191
215
}
192
- // Handle authentication message
193
216
if msgData ["type" ] == "auth" {
194
- token , ok := msgData ["token" ].(string )
195
- if ! ok || ! authenticateClient (token , client , roomMappings ) {
196
- log .Println ("Authentication failed" )
197
- client .conn .WriteMessage (websocket .TextMessage , []byte ("Authentication failed" ))
217
+ token , tokenOk := msgData ["token" ].(string )
218
+ if ! tokenOk {
219
+ log .Println ("Authentication failed - no token attached" )
220
+ client .conn .WriteMessage (
221
+ websocket .TextMessage ,
222
+ []byte ("Authentication failed - no JWT token" ),
223
+ )
224
+ client .conn .Close ()
225
+ break
226
+ }
227
+ isSuccess := false
228
+ match , matchOk := msgData ["matchHash" ].(string )
229
+ if matchOk && ! authenticateClient (token , match , client , roomMappings , persistMappings ) {
230
+ log .Println (
231
+ "failed to find a matching room from match hash, proceeding with persistence check" ,
232
+ )
233
+ }
234
+ // I will ping the persistent map even if I've found it in the original map
235
+ if ! authenticateClientNoMatch (token , client , persistMappings ) {
236
+ log .Println ("failed to find a persistent room" )
237
+ isSuccess = false
238
+ } else {
239
+ isSuccess = true
240
+ }
241
+ if ! isSuccess {
242
+ client .conn .WriteMessage (
243
+ websocket .TextMessage ,
244
+ []byte ("Authentication failed - failed to find a matching room" ),
245
+ )
198
246
client .conn .Close ()
199
247
break
200
248
}
@@ -226,14 +274,14 @@ func statusHandler(hub *Hub) gin.HandlerFunc {
226
274
for client := range hub .clients {
227
275
roomID := client .roomID
228
276
currentStatus , ok := status [roomID ]
229
- if ! ok {
230
- // Initialize status for a new RoomID
277
+ if ( ! ok ) {
278
+ // Initialize status for a new roomID
231
279
status [roomID ] = map [string ]interface {}{
232
- "clients" : 1 ,
233
- "workspace" : hub .workspaces [roomID ],
280
+ "clients" : 1 ,
281
+ "workspace" : hub .workspaces [roomID ],
234
282
}
235
283
} else {
236
- // Update the client count for an existing RoomID
284
+ // Update the client count for an existing roomID
237
285
status [roomID ] = map [string ]interface {}{
238
286
"clients" : currentStatus .(map [string ]interface {})["clients" ].(int ) + 1 ,
239
287
"workspace" : hub .workspaces [roomID ],
@@ -254,10 +302,34 @@ func main() {
254
302
if REDIS_URI == "" {
255
303
REDIS_URI = "localhost:9190"
256
304
}
257
- roomMappings := verify .InitialiseRoomMappings (REDIS_URI , 1 )
305
+
306
+ REDIS_ROOM_MAPPING := 1
307
+ REDIS_ROOM_PERSIST := 2
308
+
309
+ if os .Getenv ("REDIS_ROOM_MAPPING" ) != "" {
310
+ num , err := strconv .Atoi (os .Getenv ("REDIS_ROOM_MAPPING" ))
311
+ if err != nil {
312
+ log .Fatal ("DB no of room map is badly formatted" + err .Error ())
313
+ } else {
314
+ REDIS_ROOM_MAPPING = num
315
+ }
316
+ }
317
+
318
+ if os .Getenv ("REDIS_ROOM_PERSIST" ) != "" {
319
+ num , err := strconv .Atoi (os .Getenv ("REDIS_ROOM_PERSIST" ))
320
+ if err != nil {
321
+ log .Fatal ("DB no of room persistance store is badly formatted" + err .Error ())
322
+ } else {
323
+ REDIS_ROOM_PERSIST = num
324
+ }
325
+ }
326
+
327
+ roomMappings := verify .InitialiseRoomMappings (REDIS_URI , REDIS_ROOM_MAPPING )
328
+ persistMappings := verify .InitialisePersistMappings (REDIS_URI , REDIS_ROOM_PERSIST );
329
+
258
330
// WebSocket connection endpoint
259
331
r .GET ("/ws" , func (c * gin.Context ) {
260
- serveWs (hub , c , roomMappings )
332
+ serveWs (hub , c , roomMappings , persistMappings )
261
333
})
262
334
263
335
// Status endpoint
0 commit comments