forked from mumble-voip/grumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
1057 lines (914 loc) · 26.6 KB
/
server.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 (
"log"
"crypto/tls"
"crypto/sha1"
"os"
"net"
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"encoding/hex"
"sync"
"goprotobuf.googlecode.com/hg/proto"
"mumbleproto"
"cryptstate"
"gob"
"hash"
"io"
"rand"
"strings"
)
// The default port a Murmur server listens on
const DefaultPort = 64738
const UDPPacketSize = 1024
const CeltCompatBitstream = -2147483637
const (
StateClientConnected = iota
StateServerSentVersion
StateClientSentVersion
StateClientAuthenticated
StateClientReady
StateClientDead
)
// A Murmur server instance
type Server struct {
Id int64
listener tls.Listener
address string
port int
udpconn *net.UDPConn
running bool
incoming chan *Message
udpsend chan *Message
voicebroadcast chan *VoiceBroadcast
freezeRequest chan *freezeRequest
// Signals to the server that a client has been successfully
// authenticated.
clientAuthenticated chan *Client
// Config-related
MaxUsers int
MaxBandwidth uint32
// Clients
clients map[uint32]*Client
// Host, host/port -> client mapping
hmutex sync.Mutex
hclients map[string][]*Client
hpclients map[string]*Client
// Codec information
AlphaCodec int32
BetaCodec int32
PreferAlphaCodec bool
// Channels
chanid int
root *Channel
Channels map[int]*Channel
// Users
Users map[uint32]*User
UserCertMap map[string]*User
UserNameMap map[string]*User
// ACL cache
aclcache ACLCache
}
type freezeRequest struct {
done chan bool
readCloser io.ReadCloser
}
// Allocate a new Murmur instance
func NewServer(id int64, addr string, port int) (s *Server, err os.Error) {
s = new(Server)
s.Id = id
s.address = addr
s.port = port
s.running = false
s.clients = make(map[uint32]*Client)
s.Users = make(map[uint32]*User)
s.UserCertMap = make(map[string]*User)
s.UserNameMap = make(map[string]*User)
s.hclients = make(map[string][]*Client)
s.hpclients = make(map[string]*Client)
s.incoming = make(chan *Message)
s.udpsend = make(chan *Message)
s.voicebroadcast = make(chan *VoiceBroadcast)
s.freezeRequest = make(chan *freezeRequest)
s.clientAuthenticated = make(chan *Client)
s.MaxBandwidth = 300000
s.MaxUsers = 10
s.Channels = make(map[int]*Channel)
s.root = s.NewChannel(0, "Root")
s.aclcache = NewACLCache()
return
}
// Check whether password matches the set SuperUser password.
func (server *Server) CheckSuperUserPassword(password string) bool {
superUser, exists := server.Users[0]
if !exists {
log.Panicf("Fatal error: No SuperUser for server %v", server.Id)
}
parts := strings.Split(superUser.Password, "$", -1)
if len(parts) != 3 {
return false
}
if len(parts[2]) == 0 {
return false
}
var h hash.Hash
switch parts[0] {
case "sha1":
h = sha1.New()
default:
// no such hash
return false
}
// salt
if len(parts[1]) > 0 {
h.Write([]byte(parts[1]))
}
// password
h.Write([]byte(password))
sum := hex.EncodeToString(h.Sum())
if parts[2] == sum {
return true
}
return false
}
// Called by the server to initiate a new client connection.
func (server *Server) NewClient(conn net.Conn) (err os.Error) {
client := new(Client)
addr := conn.RemoteAddr()
if addr == nil {
err = os.NewError("Unable to extract address for client.")
return
}
client.tcpaddr = addr.(*net.TCPAddr)
client.server = server
client.conn = conn
client.reader = bufio.NewReader(client.conn)
client.writer = bufio.NewWriter(client.conn)
client.state = StateClientConnected
client.msgchan = make(chan *Message)
client.udprecv = make(chan []byte)
client.user = nil
go client.receiver()
go client.udpreceiver()
client.doneSending = make(chan bool)
go client.sender()
return
}
// Remove a disconnected client from the server's
// internal representation.
func (server *Server) RemoveClient(client *Client, kicked bool) {
server.hmutex.Lock()
if client.udpaddr != nil {
host := client.udpaddr.IP.String()
oldclients := server.hclients[host]
newclients := []*Client{}
for _, hostclient := range oldclients {
if hostclient != client {
newclients = append(newclients, hostclient)
}
}
server.hclients[host] = newclients
server.hpclients[client.udpaddr.String()] = nil, false
}
server.hmutex.Unlock()
server.clients[client.Session] = nil, false
// Remove client from channel
channel := client.Channel
if channel != nil {
channel.RemoveClient(client)
}
// If the user was not kicked, broadcast a UserRemove message.
// If the user is disconnect via a kick, the UserRemove message has already been sent
// at this point.
if !kicked && client.state > StateClientAuthenticated {
err := server.broadcastProtoMessage(MessageUserRemove, &mumbleproto.UserRemove{
Session: proto.Uint32(client.Session),
})
if err != nil {
log.Panic("Unable to broadcast UserRemove message for disconnected client.")
}
}
}
// Add an existing channel to the Server. (Do not arbitrarily pick an ID)
func (server *Server) NewChannel(id int, name string) (channel *Channel) {
_, exists := server.Channels[id]
if exists {
// fime(mkrautz): Handle duplicates
return nil
}
channel = NewChannel(id, name)
server.Channels[id] = channel
if id > server.chanid {
server.chanid = id + 1
}
return
}
// Add a new channel to the server. Automatically assign it a channel ID.
func (server *Server) AddChannel(name string) (channel *Channel) {
channel = NewChannel(server.chanid, name)
server.Channels[channel.Id] = channel
return
}
// Remove a channel from the server.
func (server *Server) RemoveChanel(channel *Channel) {
if channel.Id == 0 {
log.Printf("Attempted to remove root channel.")
return
}
server.Channels[channel.Id] = nil, false
}
// Link two channels
func (server *Server) LinkChannels(channel *Channel, other *Channel) {
channel.Links[other.Id] = other
other.Links[channel.Id] = channel
}
// Unlink two channels
func (server *Server) UnlinkChannels(channel *Channel, other *Channel) {
channel.Links[other.Id] = nil, false
other.Links[channel.Id] = nil, false
}
// Generate a random, valid session ID.
// The returned session ID is guaranteed not to currently be in use
// on the server, and not to be the zero-value for integers (0).
func (server *Server) GenSessionId() (session uint32) {
for {
session = rand.Uint32()
// The 0 session is disallowed in Grumble. 0 is the zero-value for
// integer types, and as such, an uninitialized session id could
// point to a valid client if we allowed the 0 session id.
if session == 0 {
continue
}
// Is there already a client on the sever with the generated id?
// Give us a new one, please.
if _, exists := server.clients[session]; exists {
continue
}
break
}
return
}
// This is the synchronous handler goroutine.
// Important control channel messages are routed through this Goroutine
// to keep server state synchronized.
func (server *Server) handler() {
for {
select {
// Control channel messages
case msg := <-server.incoming:
client := msg.client
server.handleIncomingMessage(client, msg)
// Voice broadcast
case vb := <-server.voicebroadcast:
log.Printf("VoiceBroadcast!")
if vb.target == 0 {
channel := vb.client.Channel
for _, client := range channel.clients {
if client != vb.client {
client.sendUdp(&Message{
buf: vb.buf,
client: client,
})
}
}
}
// Finish client authentication. Send post-authentication
// server info.
case client := <-server.clientAuthenticated:
server.finishAuthenticate(client)
// Synchonized freeze requests
case req := <-server.freezeRequest:
fs, err := server.Freeze()
if err != nil {
log.Panicf("Unable to freeze the server")
}
go server.handleFreezeRequest(req, &fs)
}
}
}
func (server *Server) handleFreezeRequest(freq *freezeRequest, fs *frozenServer) {
pr, pw := io.Pipe()
freq.readCloser = pr
freq.done <- true
zw, err := gzip.NewWriterLevel(pw, gzip.BestCompression)
if err != nil {
if err = pw.CloseWithError(err); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
return
}
enc := gob.NewEncoder(zw)
err = enc.Encode(fs)
if err != nil {
if err = pw.CloseWithError(err); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
}
if err = pw.CloseWithError(zw.Close()); err != nil {
log.Panicf("Unable to close PipeWriter: %v", err.String())
}
}
// Handle an Authenticate protobuf message. This is handled in a separate
// goroutine to allow for remote authenticators that are slow to respond.
//
// Once a user has been authenticated, it will ping the server's handler
// routine, which will call the finishAuthenticate method on Server which
// will send the channel tree, user list, etc. to the client.
func (server *Server) handleAuthenticate(client *Client, msg *Message) {
// Is this message not an authenticate message? If not, discard it...
if msg.kind != MessageAuthenticate {
client.Panic("Unexpected message. Expected Authenticate.")
return
}
auth := &mumbleproto.Authenticate{}
err := proto.Unmarshal(msg.buf, auth)
if err != nil {
client.Panic("Unable to unmarshal Authenticate message.")
return
}
// Did we get a username?
if auth.Username == nil || len(*auth.Username) == 0 {
client.RejectAuth("InvalidUsername", "Please specify a username to log in")
return
}
client.Username = *auth.Username
// Extract certhash
tlsconn, ok := client.conn.(*tls.Conn)
if !ok {
client.Panic("Invalid connection")
return
}
state := tlsconn.ConnectionState()
if len(state.PeerCertificates) > 0 {
hash := sha1.New()
hash.Write(state.PeerCertificates[0].Raw)
sum := hash.Sum()
client.CertHash = hex.EncodeToString(sum)
}
if client.Username == "SuperUser" {
if auth.Password == nil {
client.RejectAuth("WrongUserPW", "")
return
} else {
if server.CheckSuperUserPassword(*auth.Password) {
client.user, ok = server.UserNameMap[client.Username]
if !ok {
client.RejectAuth("InvalidUsername", "")
return
}
} else {
client.RejectAuth("WrongUserPW", "")
return
}
}
} else {
// First look up registration by name.
user, exists := server.UserNameMap[client.Username]
if exists {
if len(client.CertHash) > 0 && user.CertHash == client.CertHash {
client.user = user
} else {
client.RejectAuth("WrongUserPW", "Wrong certificate hash")
return
}
}
// Name matching didn't do. Try matching by certificate.
if client.user == nil && len(client.CertHash) > 0 {
user, exists := server.UserCertMap[client.CertHash]
if exists {
client.user = user
}
}
}
// Setup the cryptstate for the client.
client.crypt, err = cryptstate.New()
if err != nil {
client.Panic(err.String())
return
}
err = client.crypt.GenerateKey()
if err != nil {
client.Panic(err.String())
return
}
// Send CryptState information to the client so it can establish an UDP connection,
// if it wishes.
err = client.sendProtoMessage(MessageCryptSetup, &mumbleproto.CryptSetup{
Key: client.crypt.RawKey[0:],
ClientNonce: client.crypt.DecryptIV[0:],
ServerNonce: client.crypt.EncryptIV[0:],
})
if err != nil {
client.Panic(err.String())
}
// Add codecs
client.codecs = auth.CeltVersions
if len(client.codecs) == 0 {
log.Printf("Client %i connected without CELT codecs.", client.Session)
}
client.state = StateClientAuthenticated
server.clientAuthenticated <- client
}
// The last part of authentication runs in the server's synchronous handler.
func (server *Server) finishAuthenticate(client *Client) {
// If the client succeeded in proving to the server that it should be granted
// the credentials of a registered user, do some sanity checking to make sure
// that user isn't already connected.
//
// If the user is already connected, try to check whether this new client is
// connecting from the same IP address. If that's the case, disconnect the
// previous client and let the new guy in.
if client.user != nil {
found := false
for _, connectedClient := range server.clients {
if connectedClient.UserId() == client.UserId() {
found = true
break
}
}
// The user is already present on the server.
if found {
// todo(mkrautz): Do the address checking.
client.RejectAuth("UsernameInUse", "A client is already connected using those credentials.")
return
}
// No, that user isn't already connected. Move along.
}
// Add the client to the connected list
client.Session = server.GenSessionId()
server.clients[client.Session] = client
// First, check whether we need to tell the other connected
// clients to switch to a codec so the new guy can actually speak.
server.updateCodecVersions()
client.sendChannelList()
// Add the client to the host slice for its host address.
host := client.tcpaddr.IP.String()
server.hmutex.Lock()
server.hclients[host] = append(server.hclients[host], client)
server.hmutex.Unlock()
userstate := &mumbleproto.UserState{
Session: proto.Uint32(client.Session),
Name: proto.String(client.ShownName()),
ChannelId: proto.Uint32(0),
}
if client.IsRegistered() {
userstate.UserId = proto.Uint32(uint32(client.UserId()))
if client.user.HasTexture() {
// Does the client support blobs?
if client.Version >= 0x10203 {
userstate.TextureHash = client.user.TextureBlobHashBytes()
} else {
buf, err := globalBlobstore.Get(client.user.TextureBlob)
if err != nil {
log.Panicf("Blobstore error: %v", err.String())
}
userstate.Texture = buf
}
}
if client.user.HasComment() {
// Does the client support blobs?
if client.Version >= 0x10203 {
userstate.CommentHash = client.user.CommentBlobHashBytes()
} else {
buf, err := globalBlobstore.Get(client.user.CommentBlob)
if err != nil {
log.Panicf("Blobstore error: %v", err.String())
}
userstate.Comment = proto.String(string(buf))
}
}
}
server.userEnterChannel(client, server.root, userstate)
if err := server.broadcastProtoMessage(MessageUserState, userstate); err != nil {
// Server panic?
}
server.sendUserList(client)
sync := &mumbleproto.ServerSync{}
sync.Session = proto.Uint32(client.Session)
sync.MaxBandwidth = proto.Uint32(server.MaxBandwidth)
if client.IsSuperUser() {
sync.Permissions = proto.Uint64(uint64(AllPermissions))
} else {
server.HasPermission(client, server.root, EnterPermission)
perm := server.aclcache.GetPermission(client, server.root)
if !perm.IsCached() {
client.Panic("Corrupt ACL cache")
return
}
perm.ClearCacheBit()
sync.Permissions = proto.Uint64(uint64(perm))
}
if err := client.sendProtoMessage(MessageServerSync, sync); err != nil {
client.Panic(err.String())
return
}
err := client.sendProtoMessage(MessageServerConfig, &mumbleproto.ServerConfig{
AllowHtml: proto.Bool(true),
MessageLength: proto.Uint32(1000),
ImageMessageLength: proto.Uint32(1000),
})
if err != nil {
client.Panic(err.String())
return
}
client.state = StateClientReady
client.clientReady <- true
}
func (server *Server) updateCodecVersions() {
codecusers := map[int32]int{}
var winner int32
var count int
for _, client := range server.clients {
for _, codec := range client.codecs {
codecusers[codec] += 1
}
}
for codec, users := range codecusers {
if users > count {
count = users
winner = codec
}
if users == count && codec > winner {
winner = codec
}
}
var current int32
if server.PreferAlphaCodec {
current = server.AlphaCodec
} else {
current = server.BetaCodec
}
if winner == current {
return
}
if winner == CeltCompatBitstream {
server.PreferAlphaCodec = true
} else {
server.PreferAlphaCodec = !server.PreferAlphaCodec
}
if server.PreferAlphaCodec {
server.AlphaCodec = winner
} else {
server.BetaCodec = winner
}
err := server.broadcastProtoMessage(MessageCodecVersion, &mumbleproto.CodecVersion{
Alpha: proto.Int32(server.AlphaCodec),
Beta: proto.Int32(server.BetaCodec),
PreferAlpha: proto.Bool(server.PreferAlphaCodec),
})
if err != nil {
log.Printf("Unable to broadcast.")
return
}
log.Printf("CELT codec switch %#x %#x (PreferAlpha %v)", uint32(server.AlphaCodec), uint32(server.BetaCodec), server.PreferAlphaCodec)
return
}
func (server *Server) sendUserList(client *Client) {
for _, connectedClient := range server.clients {
if connectedClient.state != StateClientReady {
continue
}
if connectedClient == client {
continue
}
userstate := &mumbleproto.UserState{
Session: proto.Uint32(connectedClient.Session),
Name: proto.String(connectedClient.ShownName()),
ChannelId: proto.Uint32(uint32(connectedClient.Channel.Id)),
}
if connectedClient.IsRegistered() {
userstate.UserId = proto.Uint32(uint32(connectedClient.UserId()))
if connectedClient.user.HasTexture() {
// Does the client support blobs?
if client.Version >= 0x10203 {
userstate.TextureHash = connectedClient.user.TextureBlobHashBytes()
} else {
buf, err := globalBlobstore.Get(connectedClient.user.TextureBlob)
if err != nil {
log.Panicf("Blobstore error: %v", err.String())
}
userstate.Texture = buf
}
}
if connectedClient.user.HasComment() {
// Does the client support blobs?
if client.Version >= 0x10203 {
userstate.CommentHash = connectedClient.user.CommentBlobHashBytes()
} else {
buf, err := globalBlobstore.Get(connectedClient.user.CommentBlob)
if err != nil {
log.Panicf("Blobstore error: %v", err.String())
}
userstate.Comment = proto.String(string(buf))
}
}
if len(connectedClient.user.CertHash) > 0 {
userstate.Hash = proto.String(connectedClient.user.CertHash)
}
}
if connectedClient.Mute {
userstate.Mute = proto.Bool(true)
}
if connectedClient.Suppress {
userstate.Suppress = proto.Bool(true)
}
if connectedClient.SelfMute {
userstate.SelfMute = proto.Bool(true)
}
if connectedClient.SelfDeaf {
userstate.SelfDeaf = proto.Bool(true)
}
if connectedClient.PrioritySpeaker {
userstate.PrioritySpeaker = proto.Bool(true)
}
if connectedClient.Recording {
userstate.Recording = proto.Bool(true)
}
if connectedClient.PluginContext != nil || len(connectedClient.PluginContext) > 0 {
userstate.PluginContext = connectedClient.PluginContext
}
if len(connectedClient.PluginIdentity) > 0 {
userstate.PluginIdentity = proto.String(connectedClient.PluginIdentity)
}
err := client.sendProtoMessage(MessageUserState, userstate)
if err != nil {
// Server panic?
continue
}
}
}
// Send a client its permissions for channel.
func (server *Server) sendClientPermissions(client *Client, channel *Channel) {
// No caching for SuperUser
if client.IsSuperUser() {
return
}
// Update cache
server.HasPermission(client, channel, EnterPermission)
perm := server.aclcache.GetPermission(client, channel)
log.Printf("Permissions = 0x%x", perm)
// fixme(mkrautz): Cache which permissions we've already sent.
client.sendProtoMessage(MessagePermissionQuery, &mumbleproto.PermissionQuery{
ChannelId: proto.Uint32(uint32(channel.Id)),
Permissions: proto.Uint32(uint32(perm)),
})
}
type ClientPredicate func(client *Client) bool
func (server *Server) broadcastProtoMessageWithPredicate(kind uint16, msg interface{}, clientcheck ClientPredicate) (err os.Error) {
for _, client := range server.clients {
if !clientcheck(client) {
continue
}
if client.state < StateClientAuthenticated {
continue
}
err := client.sendProtoMessage(kind, msg)
if err != nil {
return
}
}
return
}
func (server *Server) broadcastProtoMessage(kind uint16, msg interface{}) (err os.Error) {
err = server.broadcastProtoMessageWithPredicate(kind, msg, func(client *Client) bool { return true })
return
}
func (server *Server) handleIncomingMessage(client *Client, msg *Message) {
log.Printf("Handle Incoming Message")
switch msg.kind {
case MessagePing:
server.handlePingMessage(msg.client, msg)
case MessageChannelRemove:
server.handlePingMessage(msg.client, msg)
case MessageChannelState:
server.handleChannelStateMessage(msg.client, msg)
case MessageUserState:
server.handleUserStateMessage(msg.client, msg)
case MessageUserRemove:
server.handleUserRemoveMessage(msg.client, msg)
case MessageBanList:
server.handleBanListMessage(msg.client, msg)
case MessageTextMessage:
server.handleTextMessage(msg.client, msg)
case MessageACL:
server.handleAclMessage(msg.client, msg)
case MessageQueryUsers:
server.handleQueryUsers(msg.client, msg)
case MessageCryptSetup:
server.handleCryptSetup(msg.client, msg)
case MessageContextActionAdd:
log.Printf("MessageContextActionAdd from client")
case MessageContextAction:
log.Printf("MessageContextAction from client")
case MessageUserList:
log.Printf("MessageUserList from client")
case MessageVoiceTarget:
log.Printf("MessageVoiceTarget from client")
case MessagePermissionQuery:
server.handlePermissionQuery(msg.client, msg)
case MessageCodecVersion:
log.Printf("MessageCodecVersion from client")
case MessageUserStats:
server.handleUserStatsMessage(msg.client, msg)
case MessageRequestBlob:
server.handleRequestBlob(msg.client, msg)
case MessageServerConfig:
log.Printf("MessageServerConfig from client")
}
}
func (s *Server) SetupUDP() (err os.Error) {
addr := &net.UDPAddr{
Port: s.port,
}
s.udpconn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
return
}
func (s *Server) SendUDP() {
for {
msg := <-s.udpsend
// Encrypted
if msg.client != nil {
crypted := make([]byte, len(msg.buf)+4)
msg.client.crypt.Encrypt(crypted, msg.buf)
s.udpconn.WriteTo(crypted, msg.client.udpaddr)
// Non-encrypted
} else if msg.address != nil {
s.udpconn.WriteTo(msg.buf, msg.address)
} else {
// Skipping
}
}
}
// Listen for and handle UDP packets.
func (server *Server) ListenUDP() {
buf := make([]byte, UDPPacketSize)
for {
nread, remote, err := server.udpconn.ReadFrom(buf)
if err != nil {
// Not much to do here. This is bad, of course. Should we panic this server instance?
continue
}
udpaddr, ok := remote.(*net.UDPAddr)
if !ok {
log.Printf("No UDPAddr in read packet. Disabling UDP. (Windows?)")
return
}
// Length 12 is for ping datagrams from the ConnectDialog.
if nread == 12 {
readbuf := bytes.NewBuffer(buf)
var (
tmp32 uint32
rand uint64
)
_ = binary.Read(readbuf, binary.BigEndian, &tmp32)
_ = binary.Read(readbuf, binary.BigEndian, &rand)
buffer := bytes.NewBuffer(make([]byte, 0, 24))
_ = binary.Write(buffer, binary.BigEndian, uint32((1<<16)|(2<<8)|2))
_ = binary.Write(buffer, binary.BigEndian, rand)
_ = binary.Write(buffer, binary.BigEndian, uint32(len(server.clients)))
_ = binary.Write(buffer, binary.BigEndian, uint32(server.MaxUsers))
_ = binary.Write(buffer, binary.BigEndian, uint32(server.MaxBandwidth))
server.udpsend <- &Message{
buf: buffer.Bytes(),
address: udpaddr,
}
} else {
var match *Client
plain := make([]byte, nread-4)
// Determine which client sent the the packet. First, we
// check the map 'hpclients' in the server struct. It maps
// a hort-post combination to a client.
//
// If we don't find any matches, we look in the 'hclients',
// which maps a host address to a slice of clients.
server.hmutex.Lock()
client, ok := server.hpclients[udpaddr.String()]
if ok {
err = client.crypt.Decrypt(plain[0:], buf[0:nread])
if err != nil {
log.Panicf("Unable to decrypt incoming packet for client %v (host-port matched)", client)
}
match = client
} else {
host := udpaddr.IP.String()
hostclients := server.hclients[host]
for _, client := range hostclients {
err = client.crypt.Decrypt(plain[0:], buf[0:nread])
if err != nil {
continue
} else {
match = client
}
}
if match != nil {
match.udpaddr = udpaddr
server.hpclients[udpaddr.String()] = match
}
}
server.hmutex.Unlock()
// No client found.
if match == nil {
log.Printf("Sender of UDP packet could not be determined. Packet dropped.")
continue
}
match.udp = true
match.udprecv <- plain
}
}
}
// Clear the ACL cache
func (s *Server) ClearACLCache() {
s.aclcache = NewACLCache()
}
// Helper method for users entering new channels
func (server *Server) userEnterChannel(client *Client, channel *Channel, userstate *mumbleproto.UserState) {
if client.Channel == channel {
return
}
oldchan := client.Channel
if oldchan != nil {
oldchan.RemoveClient(client)
}
channel.AddClient(client)
server.ClearACLCache()
// fixme(mkrautz): Set LastChannel for user in datastore
// fixme(mkrautz): Remove channel if temporary
canspeak := server.HasPermission(client, channel, SpeakPermission)
if canspeak == client.Suppress {
client.Suppress = !canspeak
userstate.Suppress = proto.Bool(client.Suppress)
}
server.sendClientPermissions(client, channel)
if channel.parent != nil {
server.sendClientPermissions(client, channel.parent)
}
}
// Create a point-in-time snapshot of Server and make it
// accessible through the returned io.ReadCloser.
func (s *Server) FreezeServer() io.ReadCloser {
if !s.running {
fs, err := s.Freeze()
if err != nil {
log.Panicf("Unable to freeze the server")
}
fr := &freezeRequest{done:make(chan bool)}
go s.handleFreezeRequest(fr, &fs)
<-fr.done
return fr.readCloser
}
fr := &freezeRequest{done:make(chan bool)}