-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.go
714 lines (612 loc) · 14.7 KB
/
connection.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package main
import (
"bytes"
"crypto/md5"
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/gorilla/websocket"
)
// regexp to detect three or more consecutive characters intended to be combined
// with another char (like accents, diacritics), if there are more than 5
// its most likely a zalgo pattern
// we also do not allow unicode non-breaking space/page/paragraph separators
// for an explanation on the unicode char classes used see:
// https://code.google.com/p/re2/wiki/Syntax
// cannot use the Z (separator) or Zs (space separator) because most of those
// are legitimate, we do not want non-breaking space characters tho
// http://www.fileformat.info/info/unicode/char/202f/index.htm
// http://www.fileformat.info/info/unicode/char/00a0/index.htm
var invalidmessage = regexp.MustCompile(`\p{M}{5,}|[\p{Zl}\p{Zp}\x{202f}\x{00a0}]`)
type Connection struct {
socket *websocket.Conn
ip string
send chan *message
sendmarshalled chan *message
blocksend chan *message
banned chan bool
stop chan bool
user *User
ping chan time.Time
sync.RWMutex
}
type SimplifiedUser struct {
Nick string `json:"nick"`
Features *[]string `json:"features"`
}
type EventDataIn struct {
Data string `json:"data"`
Extradata string `json:"extradata"`
Duration int64 `json:"duration"`
}
type EventDataOut struct {
*SimplifiedUser
Targetuserid Userid `json:"-"`
Timestamp int64 `json:"timestamp"`
Data string `json:"data,omitempty"`
Extradata string `json:"extradata,omitempty"`
Entities *Entities `json:"entities,omitempty"`
}
type BanIn struct {
Nick string `json:"nick"`
BanIP bool `json:"banip"`
Duration int64 `json:"duration"`
Ispermanent bool `json:"ispermanent"`
Reason string `json:"reason"`
}
type PingOut struct {
Timestamp int64 `json:"data"`
}
type message struct {
msgtyp int
event string
data interface{}
}
type PrivmsgIn struct {
Nick string `json:"nick"`
Data string `json:"data"`
}
type PrivmsgOut struct {
message
targetuid Userid
Messageid int64 `json:"messageid"`
Timestamp int64 `json:"timestamp"`
Nick string `json:"nick,omitempty"`
TargetNick string `json:"targetNick,omitempty"`
Data string `json:"data,omitempty"`
Entities *Entities `json:"entities,omitempty"`
}
// Create a new connection using the specified socket and router.
func newConnection(s *websocket.Conn, user *User, ip string) {
c := &Connection{
socket: s,
ip: ip,
send: make(chan *message, SENDCHANNELSIZE),
sendmarshalled: make(chan *message, SENDCHANNELSIZE),
blocksend: make(chan *message),
banned: make(chan bool, 8),
stop: make(chan bool),
user: user,
ping: make(chan time.Time, 2),
RWMutex: sync.RWMutex{},
}
go c.writePumpText()
c.readPumpText()
}
func (c *Connection) readPumpText() {
defer func() {
namescache.disconnect(c.user)
c.Quit()
c.socket.Close()
}()
c.socket.SetReadLimit(MAXMESSAGESIZE)
c.socket.SetReadDeadline(time.Now().Add(READTIMEOUT))
c.socket.SetPongHandler(func(string) error {
c.socket.SetReadDeadline(time.Now().Add(PINGTIMEOUT))
return nil
})
c.socket.SetPingHandler(func(string) error {
c.sendmarshalled <- &message{
msgtyp: websocket.PongMessage,
event: "PONG",
data: []byte{},
}
return nil
})
if c.user != nil {
c.rlockUserIfExists()
n := atomic.LoadInt32(&c.user.connections)
if n > 5 {
c.runlockUserIfExists()
c.SendError("toomanyconnections")
c.stop <- true
return
}
c.runlockUserIfExists()
} else {
namescache.addConnection()
}
hub.register <- c
c.Names()
c.Join() // broadcast to the chat that a user has connected
for {
msgtype, message, err := c.socket.ReadMessage()
if err != nil || msgtype == websocket.BinaryMessage {
return
}
name, data, err := Unpack(string(message))
if err != nil {
// invalid protocol message from the client, just ignore it,
// disconnect the user
return
}
// dispatch
switch name {
case "MSG":
c.OnMsg(data)
case "MUTE":
c.OnMute(data)
case "UNMUTE":
c.OnUnmute(data)
case "BAN":
c.OnBan(data)
case "UNBAN":
c.OnUnban(data)
case "SUBONLY":
c.OnSubonly(data)
case "PING":
c.OnPing(data)
case "PONG":
c.OnPong(data)
case "BROADCAST":
c.OnBroadcast(data)
case "PRIVMSG":
c.OnPrivmsg(data)
}
}
}
func (c *Connection) write(mt int, payload []byte) error {
c.socket.SetWriteDeadline(time.Now().Add(WRITETIMEOUT))
return c.socket.WriteMessage(mt, payload)
}
func (c *Connection) writePumpText() {
defer func() {
hub.unregister <- c
c.socket.Close() // Necessary to force reading to stop, will start the cleanup
}()
for {
select {
case _, ok := <-c.ping:
if !ok {
return
}
m, _ := time.Now().MarshalBinary()
if err := c.write(websocket.PingMessage, m); err != nil {
return
}
case <-c.banned:
c.write(websocket.TextMessage, []byte(`ERR "banned"`))
c.write(websocket.CloseMessage, []byte{})
return
case <-c.stop:
return
case m := <-c.blocksend:
c.rlockUserIfExists()
if data, err := Marshal(m.data); err == nil {
c.runlockUserIfExists()
if data, err := Pack(m.event, data); err == nil {
if err := c.write(websocket.TextMessage, data); err != nil {
return
}
}
} else {
c.runlockUserIfExists()
}
case m := <-c.send:
c.rlockUserIfExists()
if data, err := Marshal(m.data); err == nil {
c.runlockUserIfExists()
if data, err := Pack(m.event, data); err == nil {
typ := m.msgtyp
if typ == 0 {
typ = websocket.TextMessage
}
if err := c.write(typ, data); err != nil {
return
}
}
} else {
c.runlockUserIfExists()
}
case message := <-c.sendmarshalled:
data := message.data.([]byte)
if data, err := Pack(message.event, data); err == nil {
typ := message.msgtyp
if typ == 0 {
typ = websocket.TextMessage
}
if err := c.write(typ, data); err != nil {
return
}
}
}
}
}
func (c *Connection) rlockUserIfExists() {
if c.user == nil {
return
}
c.user.RLock()
}
func (c *Connection) runlockUserIfExists() {
if c.user == nil {
return
}
c.user.RUnlock()
}
func (c *Connection) Emit(event string, data interface{}) {
c.send <- &message{
event: event,
data: data,
}
}
func (c *Connection) EmitBlock(event string, data interface{}) {
c.blocksend <- &message{
event: event,
data: data,
}
}
func (c *Connection) Broadcast(event string, data *EventDataOut) {
c.rlockUserIfExists()
marshalled, _ := Marshal(data)
c.runlockUserIfExists()
m := &message{
event: event,
data: marshalled,
}
hub.broadcast <- m
}
func (c *Connection) canModerateUser(nick string) (bool, Userid) {
if c.user == nil || utf8.RuneCountInString(nick) == 0 {
return false, 0
}
uid, protected := usertools.getUseridForNick(nick)
if uid == 0 || c.user.id == uid || protected {
return false, uid
}
return true, uid
}
func (c *Connection) getEventDataOut() *EventDataOut {
out := &EventDataOut{
Timestamp: unixMilliTime(),
}
if c.user != nil {
out.SimplifiedUser = c.user.simplified
}
return out
}
func (c *Connection) Join() {
if c.user != nil {
c.rlockUserIfExists()
defer c.runlockUserIfExists()
n := atomic.LoadInt32(&c.user.connections)
if n == 1 {
c.Broadcast("JOIN", c.getEventDataOut())
}
}
}
func (c *Connection) Quit() {
if c.user != nil {
c.rlockUserIfExists()
defer c.runlockUserIfExists()
n := atomic.LoadInt32(&c.user.connections)
if n <= 0 {
c.Broadcast("QUIT", c.getEventDataOut())
}
}
}
func (c *Connection) OnBroadcast(data []byte) {
m := &EventDataIn{}
if err := Unmarshal(data, m); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil {
c.SendError("needlogin")
return
}
if !c.user.featureGet(ISADMIN) {
c.SendError("nopermission")
return
}
msg := strings.TrimSpace(m.Data)
msglen := utf8.RuneCountInString(msg)
if !utf8.ValidString(msg) || msglen == 0 || msglen > 512 || invalidmessage.MatchString(msg) {
c.SendError("invalidmsg")
return
}
out := c.getEventDataOut()
out.Data = msg
out.Entities = entities.Extract(msg)
c.Broadcast("BROADCAST", out)
}
func (c *Connection) canMsg(msg string, ignoresilence bool) bool {
msglen := utf8.RuneCountInString(msg)
if !utf8.ValidString(msg) || msglen == 0 || msglen > 512 || invalidmessage.MatchString(msg) {
c.SendError("invalidmsg")
return false
}
if !ignoresilence {
if mutes.isUserMuted(c) {
c.SendError("muted")
return false
}
if !hub.canUserSpeak(c) {
c.SendError("submode")
return false
}
}
if c.user != nil && !c.user.isBot() {
// very simple heuristics of "punishing" the flooding user
// if the user keeps spamming, the delay between messages increases
// this delay resets after a fixed amount of time
now := time.Now()
difference := now.Sub(c.user.lastmessagetime)
switch {
case difference <= DELAY:
c.user.delayscale *= 2
case difference > MAXTHROTTLETIME:
c.user.delayscale = 1
}
sendtime := c.user.lastmessagetime.Add(time.Duration(c.user.delayscale) * DELAY)
if sendtime.After(now) {
c.SendError("throttled")
return false
}
c.user.lastmessagetime = now
}
return true
}
func (c *Connection) OnMsg(data []byte) {
m := &EventDataIn{}
if err := Unmarshal(data, m); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil {
c.SendError("needlogin")
return
}
msg := strings.TrimSpace(m.Data)
if !c.canMsg(msg, false) {
return
}
// strip off /me for anti-spam purposes
var bmsg []byte
if len(msg) > 4 && msg[:4] == "/me " {
bmsg = []byte(strings.TrimSpace(msg[4:]))
} else {
bmsg = []byte(msg)
}
tsum := md5.Sum(bmsg)
sum := tsum[:]
if bytes.Equal(sum, c.user.lastmessage) {
c.user.delayscale++
c.SendError("duplicate")
return
}
c.user.lastmessage = sum
out := c.getEventDataOut()
out.Data = msg
out.Entities = entities.Extract(msg)
if err := combos.Transform(out); err == ErrComboDuplicate {
c.SendError("duplicate")
return
}
TransformRares(out)
c.Broadcast("MSG", out)
}
func (c *Connection) OnPrivmsg(data []byte) {
pin := &PrivmsgIn{}
if err := Unmarshal(data, pin); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil {
c.SendError("needlogin")
return
}
msg := pin.Data
if !c.canMsg(msg, true) {
return
}
tuid, _ := usertools.getUseridForNick(pin.Nick)
if tuid == 0 || tuid == c.user.id {
c.SendError("notfound")
return
}
// ephemeral private messages
// in particular, messages sent to users that are offline will never be delivered
// TODO search db instead? -> can tell user that name is right, but just offline.
pout := &PrivmsgOut{
message: message{
event: "PRIVMSG",
},
Nick: c.user.nick,
TargetNick: pin.Nick,
targetuid: Userid(tuid),
Data: msg,
Messageid: 1337, // no saving in db means ids do not matter
Timestamp: unixMilliTime(),
Entities: entities.Extract(msg),
}
pout.message.data, _ = Marshal(pout)
c.Emit("PRIVMSGSENT", pout)
hub.privmsg <- pout
}
func (c *Connection) Names() {
n := namescache.getNames()
if string(n) == "" { // handle empty cache on very first connection. TODO: connectioncount?
n = []byte("{}")
}
c.sendmarshalled <- &message{
event: "NAMES",
data: n,
}
}
func (c *Connection) OnMute(data []byte) {
mute := &EventDataIn{} // Data is the nick
if err := Unmarshal(data, mute); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil || !c.user.isModerator() {
c.SendError("nopermission")
return
}
ok, uid := c.canModerateUser(mute.Data)
if !ok || uid == 0 {
c.SendError("nopermission")
return
}
if mute.Duration == 0 {
mute.Duration = int64(DEFAULTMUTEDURATION)
}
if time.Duration(mute.Duration) > 7*24*time.Hour {
c.SendError("protocolerror") // too long mute
return
}
mutes.muteUserid(uid, mute.Duration)
out := c.getEventDataOut()
out.Data = mute.Data
out.Targetuserid = uid
c.Broadcast("MUTE", out)
}
func (c *Connection) OnUnmute(data []byte) {
user := &EventDataIn{} // Data is the nick
if err := Unmarshal(data, user); err != nil || utf8.RuneCountInString(user.Data) == 0 {
c.SendError("protocolerror")
return
}
if c.user == nil || !c.user.isModerator() {
c.SendError("nopermission")
return
}
uid, _ := usertools.getUseridForNick(user.Data)
if uid == 0 {
c.SendError("notfound")
return
}
mutes.unmuteUserid(uid)
out := c.getEventDataOut()
out.Data = user.Data
out.Targetuserid = uid
c.Broadcast("UNMUTE", out)
}
func (c *Connection) Muted() {
}
func (c *Connection) OnBan(data []byte) {
ban := &BanIn{}
if err := Unmarshal(data, ban); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil {
c.SendError("nopermission")
return
}
if !c.user.isModerator() {
c.SendError("nopermission")
return
}
ok, uid := c.canModerateUser(ban.Nick)
if uid == 0 {
c.SendError("notfound")
return
} else if !ok {
c.SendError("nopermission")
return
}
reason := strings.TrimSpace(ban.Reason)
if utf8.RuneCountInString(reason) == 0 || !utf8.ValidString(reason) {
c.SendError("needbanreason")
return
}
if ban.Duration == 0 {
ban.Duration = int64(DEFAULTBANDURATION)
}
bans.banUser(c.user.id, uid, ban)
out := c.getEventDataOut()
out.Data = ban.Nick
out.Targetuserid = uid
c.Broadcast("BAN", out)
}
func (c *Connection) OnUnban(data []byte) {
user := &EventDataIn{}
if err := Unmarshal(data, user); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil || !c.user.isModerator() {
c.SendError("nopermission")
return
}
uid, _ := usertools.getUseridForNick(user.Data)
if uid == 0 {
c.SendError("notfound")
return
}
bans.unbanUserid(uid)
mutes.unmuteUserid(uid)
out := c.getEventDataOut()
out.Data = user.Data
out.Targetuserid = uid
c.Broadcast("UNBAN", out)
}
func (c *Connection) Banned() {
c.banned <- true
}
func (c *Connection) OnSubonly(data []byte) {
m := &EventDataIn{} // Data is on/off
if err := Unmarshal(data, m); err != nil {
c.SendError("protocolerror")
return
}
if c.user == nil || !c.user.isModerator() {
c.SendError("nopermission")
return
}
switch {
case m.Data == "on":
hub.toggleSubmode(true)
case m.Data == "off":
hub.toggleSubmode(false)
default:
c.SendError("protocolerror")
return
}
out := c.getEventDataOut()
out.Data = m.Data
c.Broadcast("SUBONLY", out)
}
func (c *Connection) Ping() {
d := &PingOut{
time.Now().UnixNano(),
}
c.Emit("PING", d)
}
func (c *Connection) OnPing(data []byte) {
c.Emit("PONG", data)
}
func (c *Connection) OnPong(data []byte) {
}
func (c *Connection) SendError(identifier string) {
c.EmitBlock("ERR", identifier)
}
func (c *Connection) Refresh() {
c.EmitBlock("REFRESH", c.getEventDataOut())
c.stop <- true
}