forked from Syleron/sockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.go
451 lines (368 loc) · 11.3 KB
/
sockets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// MIT License
//
// Copyright (c) 2018-2024 Andrew Zak <andrew@linux.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package sockets
import (
"crypto/x509"
"errors"
"fmt"
"github.com/gorilla/websocket"
"github.com/syleron/sockets/common"
"log"
"net/http"
"os"
"os/signal"
"sync"
"time"
)
type DataHandler interface {
// NewConnection Client connected handler
NewConnection(ctx *Context)
// ConnectionClosed Client disconnect handler
ConnectionClosed(ctx *Context)
}
type Sockets struct {
Connections map[string]*Connection
Sessions map[string]*Session
broadcastChan chan Broadcast
interrupt chan os.Signal
handler DataHandler
config *Config
sync.RWMutex
}
type Context struct {
*Connection
UUID string
PeerCerts []*x509.Certificate
}
type Broadcast struct {
message *common.Message
context *Context
}
type Room struct {
Name string `json:"name"`
Channel string `json:"channel"`
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// TODO: Add a check for the origin of the request to prevent CSRF attacks.
return true
},
}
func New(handler DataHandler, c *Config) *Sockets {
c.MergeDefaults()
sockets := &Sockets{
Connections: make(map[string]*Connection),
Sessions: make(map[string]*Session),
broadcastChan: make(chan Broadcast),
interrupt: make(chan os.Signal, 1),
handler: handler,
config: c,
}
signal.Notify(sockets.interrupt, os.Interrupt)
go sockets.manageInterrupts()
return sockets
}
func (s *Sockets) Close() {
s.RLock()
defer s.RUnlock()
for _, c := range s.Connections {
if c.Conn != nil {
c.Conn.Close()
}
}
log.Println("All connections closed.")
os.Exit(0)
}
func (s *Sockets) HandleEvent(pattern string, handler EventFunc, protected bool) {
if events == nil {
events = make(map[string]*Event)
}
events[pattern] = &Event{
EventFunc: handler,
Protected: protected,
}
}
func (s *Sockets) manageInterrupts() {
<-s.interrupt
log.Println("Received interrupt signal, shutting down...")
s.Close()
}
func (s *Sockets) HandleConnection(w http.ResponseWriter, r *http.Request, realIP string) error {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Failed to upgrade WebSocket: %v", err)
return fmt.Errorf("websocket upgrade error: %w", err)
}
newConnection := NewConnection()
newConnection.Conn = ws
newConnection.RealIP = determineRealIP(ws, realIP)
newConnection.Status = true
peerCerts := getPeerCertificates(r)
s.addConnection(newConnection.UUID, newConnection)
context := &Context{
Connection: newConnection,
UUID: newConnection.UUID,
PeerCerts: peerCerts,
}
s.handler.NewConnection(context)
ws.SetReadLimit(s.config.ReadLimitSize)
ws.SetReadDeadline(time.Now().Add(s.config.PongWait))
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(s.config.PongWait))
return nil
})
return s.handleMessages(ws, context)
}
func (s *Sockets) handleMessages(ws *websocket.Conn, context *Context) error {
for {
var msg common.Message
if err := ws.ReadJSON(&msg); err != nil {
s.closeWS(context.Connection)
return fmt.Errorf("error reading JSON: %w", err)
}
EventHandler(&msg, context)
}
}
func (s *Sockets) Broadcast(event string, data interface{}) {
s.broadcastHelper(func(c *Connection) bool {
return true // Always true, since we're broadcasting to all
}, event, data)
}
func (s *Sockets) BroadcastToRoom(roomName, event string, data interface{}, ctx *Context) {
s.broadcastHelper(func(c *Connection) bool {
return c.Room != nil && c.Room.Name == roomName && c.UUID != ctx.UUID
}, event, data)
}
func (s *Sockets) BroadcastToRoomChannel(roomName, channelName, event string, data interface{}, ctx *Context) {
s.broadcastHelper(func(c *Connection) bool {
return c.Room != nil && c.Room.Name == roomName && c.Room.Channel == channelName && c.UUID != ctx.UUID
}, event, data)
}
func (s *Sockets) broadcastHelper(filter func(*Connection) bool, event string, data interface{}) {
s.RLock()
defer s.RUnlock()
for _, c := range s.Connections {
if c.Conn == nil {
continue
}
if filter(c) {
message := common.Response{
EventName: event,
Data: data,
}
if err := c.Emit(message); err != nil {
log.Printf("Failed to emit message to UUID %s: %v", c.UUID, err)
continue
}
}
}
}
func (s *Sockets) GetUserRoom(username, uuid string) (string, error) {
if username == "" || uuid == "" {
return "", errors.New("invalid input: username or UUID is empty")
}
s.RLock()
defer s.RUnlock()
if session, ok := s.Sessions[username]; ok {
if conn, ok := session.connections[uuid]; ok && conn.Room != nil {
return conn.Room.Name, nil
}
}
return "", fmt.Errorf("unable to find room for user %s with UUID %s", username, uuid)
}
func (s *Sockets) JoinRoom(room, uuid string) error {
if room == "" || uuid == "" {
return errors.New("invalid input: room or UUID is empty")
}
s.Lock()
defer s.Unlock()
if conn, ok := s.Connections[uuid]; ok {
conn.Room = &Room{Name: room}
log.Printf("User with UUID %s joined room %s", uuid, room)
return nil
}
return fmt.Errorf("unable to find client connection for UUID %s", uuid)
}
func (s *Sockets) LeaveRoom(uuid string) {
if uuid == "" {
log.Println("Attempted to leave room with empty UUID")
return
}
s.Lock()
defer s.Unlock()
if conn, ok := s.Connections[uuid]; ok && conn.Room != nil {
conn.Room = &Room{} // Effectively "leaving" the room by resetting it
log.Printf("User with UUID %s left the room", uuid)
}
}
func (s *Sockets) JoinRoomChannel(channel, uuid string) error {
if channel == "" || uuid == "" {
return errors.New("invalid input: channel or UUID is empty")
}
s.Lock()
defer s.Unlock()
if conn, ok := s.Connections[uuid]; ok && conn.Room != nil {
conn.Room.Channel = channel
log.Printf("User with UUID %s joined channel %s", uuid, channel)
return nil
}
return fmt.Errorf("unable to find client connection for UUID %s", uuid)
}
func (s *Sockets) manageSessionAndConnection(conn *Connection) {
s.Lock()
defer s.Unlock()
username := conn.Username
uuid := conn.UUID
// Check if the session exists and manage the session if it does
if session, exists := s.Sessions[username]; exists {
delete(session.connections, uuid) // Remove connection from session
// If no more connections are left in the session, delete the session
if len(session.connections) == 0 {
if err := s.deleteSession(username); err != nil {
log.Printf("Error deleting session for user %s: %v", username, err)
}
}
}
// Remove the connection from the global list
delete(s.Connections, uuid)
}
func (s *Sockets) deleteSession(username string) error {
if _, exists := s.Sessions[username]; !exists {
return fmt.Errorf("no session exists for username: %s", username)
}
delete(s.Sessions, username)
return nil
}
func (s *Sockets) addConnection(uuid string, conn *Connection) {
if uuid == "" || conn == nil {
log.Println("Invalid parameters: UUID is empty or Connection is nil")
return
}
s.Lock()
defer s.Unlock()
// Check if there's already an existing connection with the same UUID
if existing, exists := s.Connections[uuid]; exists {
log.Printf("Warning: Connection with UUID %s already exists, closing existing connection.", uuid)
existing.Conn.Close() // Ensure the existing connection is properly closed
}
// Start our pong handler
go conn.pongHandler(s.config.PingPeriod)
// Append our connection
s.Connections[uuid] = conn
log.Printf("Connection added with UUID %s", uuid)
}
func (s *Sockets) removeConnection(uuid string) {
if uuid == "" {
log.Println("Attempted to remove connection with empty UUID")
return
}
s.Lock()
if _, exists := s.Connections[uuid]; !exists {
s.Unlock()
log.Printf("No connection exists with UUID %s", uuid)
return
}
delete(s.Connections, uuid)
s.Unlock()
log.Printf("Connection removed with UUID %s", uuid)
}
func (s *Sockets) AddSession(username string, conn *Connection) error {
if username == "" || conn == nil {
return errors.New("invalid username or connection")
}
s.Lock()
defer s.Unlock()
// Do we already have a session?
if _, exists := s.Sessions[username]; exists {
return errors.New("session already exists for this user")
}
// Define our new session
newSession := &Session{
Username: username,
connections: make(map[string]*Connection),
}
// Add our connection to our session
newSession.connections[conn.UUID] = conn
// Add our session reference to our connection
conn.addSession(newSession)
// Add our session to our sockets store
s.Sessions[username] = newSession
// success
return nil
}
func (s *Sockets) CheckIfSessionExists(username string) bool {
if s.Sessions[username] != nil {
return true
}
return false
}
func (s *Sockets) UpdateSession(username string, conn *Connection) error {
if username == "" || conn == nil {
return errors.New("invalid username or connection")
}
s.Lock()
defer s.Unlock()
session, exists := s.Sessions[username]
if !exists {
log.Printf("Failed to update session: No session exists for %s", username)
return errors.New("no session exists for this user")
}
// Add our connection to our session
session.addConnection(conn)
// Add our session to our connection
conn.addSession(session)
log.Printf("Session updated for user: %s", username)
// success
return nil
}
func (s *Sockets) DeleteSession(username string) error {
if username == "" {
return errors.New("invalid username")
}
s.Lock()
defer s.Unlock()
if _, exists := s.Sessions[username]; !exists {
log.Printf("Failed to delete session: No session exists for %s", username)
return errors.New("no session exists for this user")
}
delete(s.Sessions, username)
log.Printf("Session deleted for user: %s", username)
return nil
}
func (s *Sockets) closeWS(conn *Connection) {
// Check if the connection is non-nil
if conn == nil {
log.Println("Attempted to close a nil connection")
return
}
// Notify handler that the connection is closing
s.handler.ConnectionClosed(&Context{
Connection: conn,
UUID: conn.UUID,
})
// Attempt to close the WebSocket connection
if err := conn.Conn.Close(); err != nil {
log.Printf("Error closing WebSocket connection for UUID %s: %v", conn.UUID, err)
}
// Safely manage the session and connection removal
s.manageSessionAndConnection(conn)
}