-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
1144 lines (974 loc) · 27.5 KB
/
client.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2010-2011 The Grumble Authors
// The use of this source code is goverened by a BSD-style
// license that can be found in the LICENSE-file.
package main
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"gorm.io/gorm"
"github.com/wfjsw/hall/cryptstate"
"github.com/wfjsw/hall/mumbleproto"
"github.com/wfjsw/hall/packetdata"
"google.golang.org/protobuf/proto"
)
type waitableMessage struct {
wg *sync.WaitGroup
msg interface{}
}
// Client A client connection
type Client struct {
// Logging
*log.Logger
lf *clientLogForwarder
// Connection-related
laddr net.IP
tcpaddr *net.TCPAddr
udpaddr *net.UDPAddr
realip *net.TCPAddr
conn net.Conn
udpconn net.Conn
reader *bufio.Reader
state int
server *Server
geoipCache *GeoIPResult
hasFullUserList bool
outgoingMessageQueue chan *waitableMessage
udpsend chan []byte
udprecv chan []byte
disconnected bool
disconnectOnce sync.Once
lastResync int64
crypt cryptstate.CryptState
codecs []int32
opus bool
udp bool
unstableUDP bool
voiceTargets map[uint32]*VoiceTarget
untargetedCache map[uint32]*Client
vtMutex sync.RWMutex
// Ping stats
UDPPingAvg float32
UDPPingVar float32
UDPPackets uint32
UDPTotalPackets uint64
UDPVolume uint64
TCPPingAvg float32
TCPPingVar float32
TCPTotalPackets uint64
TCPPackets uint32
TCPVolume uint64
statsMutex sync.Mutex
LoginTime int64
LastActiveTime int64
LastPing int64
// Runtime Options
optBlockGroupShout bool
optPromiscuousMode bool
// If the client is a registered user on the server,
// the user field will point to the registration record.
// user *User
groups []string
// The clientReady channel signals the client's receiever routine that
// the client has been successfully authenticated and that it has been
// sent the necessary information to be a participant on the server.
// When this signal is received, the client has transitioned into the
// 'ready' state.
// clientReady chan bool
// Version
Version uint32
ClientName string
OSName string
OSVersion string
CryptoMode string
// Personal
userID uint32
Username string
Password string // used to reverify
session uint32
certHash string
tokens []string
// Channel *Channel
channelID int
channel *Channel
SelfMute bool
SelfDeaf bool
Mute bool
Deaf bool
Suppress bool
PrioritySpeaker bool
Recording bool
PluginContext []byte
PluginIdentity string
listenMutex sync.RWMutex
listens []*Channel
}
type UserLastChannel struct {
ID uint32 `gorm:"PRIMARY_KEY"`
LastChannel int
}
func (client *Client) GetLastChannel() (channelID int) {
if !client.IsRegistered() {
return 0
}
ulc := new(UserLastChannel)
if err := client.server.db.First(&ulc, client.userID).Error; err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
client.server.db.Create(&UserLastChannel{
ID: client.userID,
LastChannel: 0,
})
return 0
}
return ulc.LastChannel
}
func (client *Client) SetLastChannel(channelID int) {
if !client.IsRegistered() {
return
}
ulc := &UserLastChannel{
ID: client.userID,
LastChannel: channelID,
}
client.server.db.Save(ulc)
}
func (client *Client) assignChannel(channel *Channel) {
client.channelID = channel.ID
client.channel = channel
}
// MoveChannel Move client into channel
func (client *Client) MoveChannel(channel *Channel, actor *Client) {
if client.channelID == channel.ID {
// No-OP
return
}
userstate := &mumbleproto.UserState{}
userstate.Session = proto.Uint32(client.Session())
userstate.ChannelId = proto.Uint32(uint32(channel.ID))
if actor != nil {
userstate.Actor = proto.Uint32(actor.Session())
}
client.server.userEnterChannel(client, channel, userstate) // Note: panicable
client.server.broadcastUserState(userstate)
}
// Debugf implements debug-level printing for Clients.
func (client *Client) Debugf(format string, v ...interface{}) {
if client.server.cfg.Debug {
client.Printf(format, v...)
}
}
// IsRegistered Is the client a registered user?
func (client *Client) IsRegistered() bool {
return client.UserId() > 0
// return true // TODO: what if false?
}
// HasCertificate Does the client have a certificate?
func (client *Client) HasCertificate() bool {
return len(client.certHash) > 0
}
// IsSuperUser Is the client the SuperUser?
func (client *Client) IsSuperUser() bool {
// if client.user == nil {
// return false
// }
// return client.user.Id == 0
// MOCK:
// return true
return client.InGroup("admin")
}
func (client *Client) CertHash() string {
return client.certHash
}
func (client *Client) Session() uint32 {
return client.session
}
func (client *Client) Tokens() []string {
return client.tokens
}
func (client *Client) Groups() []string {
return client.groups
}
func (client *Client) InGroup(name string) bool {
for _, group := range client.Groups() {
if name == group {
return true
}
}
return false
}
func (client *Client) Channel() (channel *Channel) {
if client.channel != nil {
// UNSAFE: primitives may not be latest
return client.channel
}
if channel = client.server.GetChannel(client.channelID); channel == nil {
client.channelID = 0
channel = client.server.GetChannel(0)
}
client.channel = channel
return
}
// UserId gets the User ID of this client.
// Returns -1 if the client is not a registered user.
func (client *Client) UserId() int {
return int(client.userID)
}
// ShownName gets the client's shown name.
func (client *Client) ShownName() string {
// TODO
// if client.IsSuperUser() {
// return "SuperUser"
// }
// if client.IsRegistered() {
// return client.user.Name
// }
return client.Username
}
// IsVerified checks whether the client's certificate is
// verified.
func (client *Client) IsVerified() bool {
tlsconn := client.conn.(*tls.Conn)
state := tlsconn.ConnectionState()
return len(state.VerifiedChains) > 0
}
func (client *Client) GeoIP() (*GeoIPResult, error) {
if client.geoipCache == nil {
geoip, err := lookupIPAddress(client.realip.IP)
if err != nil {
return nil, err
}
client.geoipCache = geoip
return geoip, nil
} else {
return client.geoipCache, nil
}
}
// Log a panic and disconnect the client.
func (client *Client) Panic(v ...interface{}) {
client.Print(v...)
client.Disconnect()
}
// Log a formatted panic and disconnect the client.
func (client *Client) Panicf(format string, v ...interface{}) {
client.Printf(format, v...)
client.Disconnect()
}
// recover client thread from panic
func (client *Client) recover(wg *sync.WaitGroup) {
if err := recover(); err != nil {
if wg != nil {
wg.Done()
}
ignore := false
switch err.(type) {
case error:
if strings.Contains(err.(error).Error(), "Client is dead") || strings.Contains(err.(error).Error(), "io: read/write on closed pipe") {
ignore = true
}
if strings.Contains(err.(error).Error(), "use of closed connection") || strings.Contains(err.(error).Error(), "io: read/write on closed pipe") {
ignore = true
}
}
if !ignore {
client.Printf("panic: %v", err)
}
go client.Disconnect()
}
}
// Internal disconnect function
func (client *Client) disconnect(kicked bool) {
client.disconnected = true
// Close the client's UDP receiever goroutine.
if client.udprecv != nil {
close(client.udprecv)
// drain the channel to prevent deadlock
for range client.udprecv {
continue
}
// client.udprecv = nil
}
if client.udpsend != nil {
close(client.udpsend)
for range client.udpsend {
continue
}
// client.udpsend = nil
}
if client.outgoingMessageQueue != nil {
close(client.outgoingMessageQueue)
// drain the channel to prevent deadlock
for m := range client.outgoingMessageQueue {
if m.wg != nil {
m.wg.Done()
}
}
// client.outgoingMessageQueue = nil
}
_ = client.conn.SetDeadline(time.Now().Add(1 * time.Second))
_ = client.conn.Close()
client.server.RemoveClient(client, kicked)
client.Printf("Disconnected")
client.state = StateClientDead
if client.server.running {
go client.server.updateCodecVersions(nil)
}
}
// Disconnect Disconnect a client (client requested or server shutdown)
func (client *Client) Disconnect() {
client.disconnectOnce.Do(func() { client.disconnect(false) })
}
// ForceDisconnect Disconnect a client (kick/ban)
func (client *Client) ForceDisconnect() {
client.disconnectOnce.Do(func() { client.disconnect(true) })
}
// ClearCaches clears the client's caches
func (client *Client) ClearCaches() {
client.vtMutex.RLock()
client.untargetedCache = nil
for _, vt := range client.voiceTargets {
vt.ClearCache()
}
client.vtMutex.RUnlock()
}
// Reject an authentication attempt
func (client *Client) RejectAuth(rejectType mumbleproto.Reject_RejectType, reason string) {
var reasonString *string = nil
if len(reason) > 0 {
client.Print(reason)
reasonString = proto.String(reason)
}
_ = client.sendMessage(&mumbleproto.Reject{
Type: rejectType.Enum(),
Reason: reasonString,
})
client.ForceDisconnect()
}
// Read a protobuf message from a client
func (client *Client) readProtoMessage() (msg *Message, err error) {
var (
length uint32
kind uint16
)
err = client.conn.SetReadDeadline(time.Now().Add(time.Duration(client.server.cfg.Timeout) * time.Second))
if err != nil {
return nil, err
}
// Read the message type (16-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &kind)
if err != nil {
return
}
// Read the message length (32-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &length)
if err != nil {
return
}
if length > 10000000 {
client.server.tempIPBan.Add(client.realip.IP.String(), time.Now().Unix())
return nil, errors.New("oversized protobuf message detected")
}
buf := make([]byte, length)
_, err = io.ReadFull(client.reader, buf)
if err != nil {
return
}
err = client.conn.SetReadDeadline(time.Time{})
if err != nil {
return nil, err
}
msg = &Message{
buf: buf,
kind: kind,
client: client,
}
return
}
// Send permission denied by type
func (c *Client) sendPermissionDeniedType(denyType mumbleproto.PermissionDenied_DenyType) {
c.sendPermissionDeniedTypeUser(denyType, nil)
}
// Send permission denied by type (and user)
func (c *Client) sendPermissionDeniedTypeUser(denyType mumbleproto.PermissionDenied_DenyType, user *Client) {
pd := &mumbleproto.PermissionDenied{
Type: denyType.Enum(),
}
if user != nil {
pd.Session = proto.Uint32(user.Session())
}
err := c.sendMessage(pd)
if err != nil {
c.Panic(err)
return
}
}
// Send permission denied by who, what, where
func (c *Client) sendPermissionDenied(who *Client, where *Channel, what Permission) {
pd := &mumbleproto.PermissionDenied{
Permission: proto.Uint32(uint32(what)),
ChannelId: proto.Uint32(uint32(where.ID)),
Session: proto.Uint32(who.Session()),
Type: mumbleproto.PermissionDenied_Permission.Enum(),
}
err := c.sendMessage(pd)
if err != nil {
c.Panic(err)
return
}
}
// Send permission denied fallback
func (client *Client) sendPermissionDeniedFallback(denyType mumbleproto.PermissionDenied_DenyType, version uint32, text string) {
pd := &mumbleproto.PermissionDenied{
Type: denyType.Enum(),
}
if client.Version < version {
pd.Reason = proto.String(text)
}
err := client.sendMessage(pd)
if err != nil {
panic(err) // Caught by incoming message hub
}
}
// Send permission denied text
func (client *Client) sendPermissionDeniedText(text string) {
pd := &mumbleproto.PermissionDenied{
Type: mumbleproto.PermissionDenied_Text.Enum(),
Reason: proto.String(text),
}
err := client.sendMessage(pd)
if err != nil {
panic(err) // Caught by incoming message hub
}
}
// UDP send loop
func (client *Client) udpSendLoop() {
defer client.recover(nil)
for packet := range client.udpsend {
client.SendUDP(packet, false)
}
}
// UDP receive loop
func (client *Client) udpRecvLoop() {
// GOROUTINE START
defer client.recover(nil)
for buf := range client.udprecv {
client.handleUDPPacket(buf)
}
}
func (client *Client) handleUDPPacket(buf []byte) {
defer client.recover(nil)
if client.disconnected || client.state == StateClientDead {
return
}
kind := (buf[0] >> 5) & 0x07
if kind == mumbleproto.UDPMessagePing {
client.SendUDP(buf, true)
// client.queueUDP(buf)
// xmitBuf.Put(buf)
return
}
if client.state == StateClientReady {
switch kind {
case mumbleproto.UDPMessageVoiceSpeex:
fallthrough
case mumbleproto.UDPMessageVoiceCELTAlpha:
fallthrough
case mumbleproto.UDPMessageVoiceCELTBeta:
if client.server.Opus {
return
}
fallthrough
case mumbleproto.UDPMessageVoiceOpus:
target := buf[0] & 0x1f
var counter uint8
// outbuf := make([]byte, 1024)
outbuf := (*(xmitBuf.Get().(*[]byte)))[:1024]
incoming := packetdata.New(buf[1 : 1+(len(buf)-1)])
outgoing := packetdata.New(outbuf[1 : 1+(len(outbuf)-1)])
_ = incoming.GetUint32()
if kind != mumbleproto.UDPMessageVoiceOpus {
for {
counter = incoming.Next8()
incoming.Skip(int(counter & 0x7f))
if !((counter&0x80) != 0 && incoming.IsValid()) {
break
}
}
} else {
size := int(incoming.GetUint16())
incoming.Skip(size & 0x1fff)
}
outgoing.PutUint32(client.Session())
outgoing.PutBytes(buf[1 : 1+(len(buf)-1)])
outbuf[0] = buf[0] & 0xe0 // strip target
if target != 0x1f { // VoiceTarget
client.LastActiveTime = time.Now().Unix()
client.server.routeVoiceBroadcast(&VoiceBroadcast{
client: client,
buf: outbuf[0 : 1+outgoing.Size()],
target: target,
})
} else { // Server loopback
client.queueUDP(outbuf[0 : 1+outgoing.Size()])
}
xmitBuf.Put(&buf)
}
}
}
func (client *Client) queueMessage(msg *waitableMessage) {
defer client.recover(msg.wg) // Decrease the counter in case of exception
client.outgoingMessageQueue <- msg
return
}
// Queue UDP packet into server's UDP send queue
func (client *Client) queueUDP(buf []byte) {
defer client.recover(nil)
client.udpsend <- buf
}
func (client *Client) createUDPPacket(buf []byte, dst []byte) {
// length = len(buf) + client.crypt.Overhead()
// // out = make([]byte, length)
client.crypt.Encrypt(dst, buf)
return
}
// Send buf as a UDP message. If the client does not have
// an established UDP connection, the datagram will be tunelled
// through the client's control channel (TCP).
func (client *Client) SendUDP(buf []byte, force bool) {
defer client.recover(nil)
defer xmitBuf.Put(&buf)
if force || (client.udp && !client.unstableUDP) {
// crypted := make([]byte, len(buf)+client.crypt.Overhead())
// client.crypt.Encrypt(crypted, buf)
packetLen := len(buf) + client.crypt.Overhead()
crypted := (*(xmitBuf.Get().(*[]byte)))[:packetLen]
defer xmitBuf.Put(&crypted)
client.createUDPPacket(buf, crypted)
err := client.server.SendUDP(crypted, client.udpaddr, client.laddr)
if err != nil {
client.udp = false
}
} else {
err := client.sendMessage(buf)
if err != nil {
panic(err) // Caught by this function
}
}
}
// Send a Message to the client. The Message in msg to the client's
// buffered writer and flushes it when done.
//
// This method should only be called from within the client's own
// sender goroutine, since it serializes access to the underlying
// buffered writer.
func (client *Client) sendMessage(msg interface{}) error {
if client.disconnected && client.state >= StateClientDead {
return errors.New("client is dead")
}
buf := new(bytes.Buffer)
var (
kind uint16
msgData []byte
err error
)
kind = mumbleproto.MessageType(msg)
if kind == mumbleproto.MessageUDPTunnel {
msgData = msg.([]byte)
} else {
protoMsg, ok := (msg).(proto.Message)
if !ok {
return errors.New("client: expected a proto.Message")
}
msgData, err = proto.Marshal(protoMsg)
if err != nil {
return err
}
}
err = binary.Write(buf, binary.BigEndian, kind)
if err != nil {
return err
}
err = binary.Write(buf, binary.BigEndian, uint32(len(msgData)))
if err != nil {
return err
}
_, err = buf.Write(msgData)
if err != nil {
return err
}
if client.disconnected && client.state >= StateClientDead {
return errors.New("client is dead")
}
_ = client.conn.SetWriteDeadline(time.Now().Add(time.Duration(client.server.cfg.Timeout) * time.Second))
_, err = client.conn.Write(buf.Bytes())
_ = client.conn.SetWriteDeadline(time.Time{})
if err != nil {
return err
}
return nil
}
func strLimit(str string, cap int) string {
if len(str) > cap {
return str[:cap]
} else {
return str
}
}
// TLS receive loop
func (client *Client) tlsRecvLoop() {
defer client.recover(nil)
for {
if client.disconnected {
return
}
// The client has just connected. Before it sends its authentication
// information we must send it our version information so it knows
// what version of the protocol it should speak.
if client.state == StateClientConnected {
version := &mumbleproto.Version{
// CryptoModes: cryptstate.SupportedModes(),
}
if client.server.cfg.SendVersion {
version.Version = proto.Uint32(uint32(verProtover))
}
if client.server.cfg.SendBuildInfo {
version.Release = proto.String(fmt.Sprintf("%s %s", verRelease, VERSION))
version.Os = proto.String(runtime.GOOS)
version.OsVersion = proto.String("Unknown")
}
err := client.sendMessage(version)
if err != nil {
panic(err) // Caught by this function
}
client.state = StateServerSentVersion
continue
}
msg, err := client.readProtoMessage()
if err != nil {
if err == io.EOF || strings.Contains(err.Error(), "use of closed network connection") || strings.Contains(err.Error(), "connection reset by peer") {
client.Disconnect()
return
} else {
panic(err) // Caught by this function
}
}
atomic.AddUint64(&client.TCPTotalPackets, 1)
atomic.AddUint64(&client.TCPVolume, uint64(len(msg.buf)))
// Always deal with pings with highest priority.
if msg.kind == mumbleproto.MessagePing {
go client.server.handlePingMessage(client, msg)
} else if client.state == StateServerSentVersion && msg.kind == mumbleproto.MessageVersion {
version := &mumbleproto.Version{}
err = proto.Unmarshal(msg.buf, version)
if err != nil {
panic(err) // Caught by this function
}
if version.Version != nil {
client.Version = *version.Version
} else {
client.Version = 0x10200
}
// https://github.com/mumble-voip/mumble/pull/4101#issuecomment-619146743
if version.Release != nil {
client.ClientName = strLimit(*version.Release, 100)
}
if version.Os != nil {
client.OSName = strLimit(*version.Os, 40)
}
if version.OsVersion != nil {
client.OSVersion = strLimit(*version.OsVersion, 60)
}
// CHANGE: MinClientVersion
if client.server.cfg.MinClientVersion > 0 {
if uint32(client.server.cfg.MinClientVersion) > client.Version {
client.RejectAuth(mumbleproto.Reject_WrongVersion, trnVersionTooOld)
continue
}
}
// CHANGE: MaxUsers
if client.server.cfg.MaxUsers > 0 {
if client.server.clients.Len() >= client.server.cfg.MaxUsers {
client.RejectAuth(mumbleproto.Reject_ServerFull, trnServerIsFull)
continue
}
}
// CHANGE: RequireClientPlatformInfo
if client.server.cfg.RequireClientPlatformInfo {
if version.Os == nil || version.OsVersion == nil {
client.RejectAuth(mumbleproto.Reject_None, trnPlatformInfoMissing)
continue
}
}
// Extract the client's supported crypto mode.
// If the client does not pick a crypto mode
// itself, use an invalid mode (the empty string)
// as its requested mode. This is effectively
// a flag asking for the default crypto mode.
// requestedMode := ""
// if len(version.CryptoModes) > 0 {
// requestedMode = version.CryptoModes[0]
// }
// // Check if the requested crypto mode is supported
// // by us. If not, fall back to the default crypto
// // mode.
// supportedModes := cryptstate.SupportedModes()
// ok := false
// for _, mode := range supportedModes {
// if requestedMode == mode {
// ok = true
// break
// }
// }
// if !ok {
// requestedMode = "OCB2-AES128"
// }
// client.CryptoMode = requestedMode
client.CryptoMode = "OCB2-AES128"
client.state = StateClientSentVersion
} else if client.state == StateClientSentVersion && msg.kind == mumbleproto.MessageAuthenticate {
go client.server.handleAuthenticate(client, msg)
} else if client.state >= StateClientConnected && client.state < StateClientAuthenticated && msg.kind == mumbleproto.MessageUserState {
go client.server.handlePreConnectUserStateMessage(client, msg)
} else if msg.kind == mumbleproto.MessageUDPTunnel {
// Special case UDPTunnel messages. They're high priority and shouldn't
// go through our synchronous path.
client.udp = false
// Safe to use: guarded by TLS close.
buf := (*(xmitBuf.Get().(*[]byte)))[:len(msg.buf)]
copy(buf, msg.buf)
client.udprecv <- buf
} else if client.state >= StateClientAuthenticated && client.state < StateClientDead {
go client.server.handleIncomingMessage(client, msg)
} else {
client.Printf("Unexpected message type ID %d on state %d", msg.kind, client.state)
}
}
}
func (client *Client) outgoingMQHandler() {
defer client.recover(nil)
for m := range client.outgoingMessageQueue {
func() {
if m.wg != nil {
defer m.wg.Done()
}
err := client.sendMessage(m.msg)
if err != nil {
panic(err)
// if err.Error() != "Client is dead" {
// panic(err) // Caught by this function
// } else if err.Error() == "Client is dead" {
// return
// }
}
}()
}
}
func (client *Client) sendChannelList() {
// client.sendChannelTree(client.server.RootChannel())
channels := client.server.AllChannels()
for _, channel := range channels {
chanstate := &mumbleproto.ChannelState{
ChannelId: proto.Uint32(uint32(channel.ID)),
Name: proto.String(channel.Name),
// IsEnterRestricted: proto.Bool(HasSomehowRestricted(&channel, EnterPermission)),
// CanEnter: proto.Bool(HasPermission(&channel, client, EnterPermission, []string{})),
}
if channel.HasDescription() {
if client.Version >= 0x10202 {
chanstate.DescriptionHash = channel.DescriptionBlobHashBytes()
} else {
buf, err := blobStore.Get(channel.DescriptionBlob)
if err == nil {
// panic("Blobstore error.")
chanstate.Description = proto.String(string(buf))
}
}
}
if channel.ParentID != -1 {
chanstate.Parent = proto.Uint32(0)
}
chanstate.Position = proto.Int32(int32(channel.Position))
if channel.IsTemporary() {
chanstate.Temporary = proto.Bool(true)
}
err := client.sendMessage(chanstate)
if err != nil {
panic(err) // Caught by authentication function
}
}
for _, channel := range channels {
chanstate := &mumbleproto.ChannelState{
ChannelId: proto.Uint32(uint32(channel.ID)),
}
if channel.ParentID != -1 {
chanstate.Parent = proto.Uint32(uint32(channel.ParentID))
}
chanstate.Position = proto.Int32(int32(channel.Position))
if channel.IsTemporary() {
chanstate.Temporary = proto.Bool(true)
}
err := client.sendMessage(chanstate)
if err != nil {
panic(err) // Caught by authentication function
}
}
}
// func (client *Client) sendChannelTree(channel *Channel) {
// chanstate := &mumbleproto.ChannelState{
// ChannelId: proto.Uint32(uint32(channel.ID)),
// Name: proto.String(channel.Name),
// }
// if channel.ParentID != -1 {
// chanstate.Parent = proto.Uint32(uint32(channel.ParentID))
// }
// if channel.HasDescription() {
// if client.Version >= 0x10202 {
// chanstate.DescriptionHash = channel.DescriptionBlobHashBytes()
// } else {
// buf, err := blobStore.Get(channel.DescriptionBlob)
// if err == nil {
// // panic("Blobstore error.")
// chanstate.Description = proto.String(string(buf))
// }
// }
// }
// if channel.IsTemporary() {
// chanstate.Temporary = proto.Bool(true)
// }
// chanstate.Position = proto.Int32(int32(channel.Position))
// err := client.sendMessage(chanstate)
// if err != nil {
// panic(err) // Caught by authentication function
// }
// for _, subchannel := range channel.Childrens() {