/
MumbleConnection.py
298 lines (275 loc) · 9.44 KB
/
MumbleConnection.py
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
import logging
import sys
import struct
import socket
import select
import os
import platform
import thread
from MessageTypes import MessageType
from PingThread import PingThread
from CryptState import CryptState
from IMumbleConnectionObserver import ConnectionState
log = logging.getLogger("MumbleConnection")
try:
import ssl
except:
print "WARNING: This python program requires the python ssl module (available in python 2.6; standalone version may be at found http://pypi.python.org/pypi/ssl/)\n"
exit
try:
from Mumble_pb2 import Authenticate, ChannelRemove, ChannelState, ServerSync, TextMessage, UserRemove, UserState, Version, UDPTunnel, CryptSetup, CodecVersion, ServerConfig
except:
print "WARNING: Module Mumble_pb2 not found\n"
exit
UDPMESSAGETYPE_UDPVOICECELTALPHA = 0
UDPMESSAGETYPE_UDPPING = 1
UDPMESSAGETYPE_UDPVOICESPEEX = 2
UDPMESSAGETYPE_UDPVOICECELTBETA = 3
#headerFormat=">HI"
headerFormat = b"!hi"
protocolVersion = (1 << 16) | (2 << 8) | (3 & 0xFF)
#supportedCodec = 0x8000000b
supportedCodec = -2147483637
CODEC_NOCODEC = -1
CODEC_ALPHA = UDPMESSAGETYPE_UDPVOICECELTALPHA
CODEC_BETA = UDPMESSAGETYPE_UDPVOICECELTBETA
class MumbleConnection:
'''
This class is responsible for implementing the underlying protocol
of Mumble. It communicates with the server using protobuf messages.
'''
def __init__(self, connectionObserver, host, port, username, password):
log.debug("Starting MumbleClient. Protocol Version: " + str(protocolVersion))
self.host = host
self.port = port
self.username = username
self.password = password
self.isConnected = False
self.pingThread=None
self.chatList=[]
self.session=None
self.authenticated = False
self.canSpeak = True
self.currentChannel = -1
self.channelList = []
self.userList = []
self.sockLock=thread.allocate_lock()
self.CryptState = CryptState()
self.send_queue = []
self.stateLock = thread.allocate_lock()
self.connectionObserver = connectionObserver
self.disconnecting = False
self.connectionObserver.setConnectionState(ConnectionState.Disconnected)
self.codec = CODEC_NOCODEC
def isConnected(self):
return self.socket != None and self.isConnected
def joinChannel(self, channelId):
log.debug("Joining channel " + str(channelId))
us = UserState()
us.session=self.session
us.channel_id=channelId
try:
self.sendMessage(MessageType.UserState, us)
self.currentChannel=channelId
except:
log.error("Could not join channel")
def sendMessage(self, messageType, message):
packet=struct.pack(headerFormat,messageType,message.ByteSize())+message.SerializeToString()
self.__sendRawPacket(packet)
def __sendRawPacket(self, packet):
self.sockLock.acquire()
while len(packet)>0:
sent=self.socket.send(packet)
if sent < 0:
log.error("Server socket error")
self.sockLock.release()
return
packet=packet[sent:]
self.sockLock.release()
def sendUdpTunnelMessage(self, data):
msgType = MessageType.UDPTunnel
packet=struct.pack(headerFormat,msgType,len(data))+data
self.__sendRawPacket(packet)
def findChannel(self, channel_id):
for c in self.channelList:
if c['channel_id'] == channel_id:
return c
return None
def __findUser(self, session_id):
for u in self.userList:
if u['session_id'] == session_id:
return u
return None
def __readFully(self,size):
msg=""
while len(msg) < size:
try:
rcvd=self.socket.recv(size-len(msg))
except Exception as inst:
log.error("Recv error: ")
log.error(inst)
return None
#log.error("Error: " + type(inst) + " " + inst)
msg += rcvd
if len(rcvd)==0:
log.warning("Got 0 bytes from socket. Strange.")
return None
return msg
def __readThread(self):
log.debug("Read thread started")
msg = ""
try:
while self.isConnected:
header=self.__readFully(6)
if not header:
log.debug("Didn't get header")
msgType,length=struct.unpack(headerFormat, header)
msg=self.__readFully(length)
self.stateLock.acquire()
try:
self.__processMessage(msgType, msg)
finally:
self.stateLock.release()
except Exception as e:
log.error("Got exception: ")
log.error(type(e))
log.error(e.args)
log.error(e)
def __handleProtocol(self):
self.stateLock.acquire()
v = Version()
v.version=protocolVersion
v.release="mmb 0.0.1-dev"
a = Authenticate()
a.username=self.username
if self.password != None:
a.password=self.password
a.celt_versions.append(-2147483637)
log.debug("Sending Version message")
self.sendMessage(MessageType.Version, v)
log.debug("Sending Authenticate message")
self.sendMessage(MessageType.Authenticate, a)
self.stateLock.release()
thread.start_new_thread(self.__readThread, ())
def __processMessage(self, msgType, message):
'''A message was received from the server, so process it.
The msgType is the type of message that was received.
The message is a ProtoBuf message that was received.
'''
log.debug("Processing Message...")
log.debug(MessageType.StringLookupTable[msgType])
if msgType == MessageType.UDPTunnel or msgType == MessageType.Ping:
log.debug("Got Ping or UDPTunnel. Ignoring")
return
if msgType == MessageType.CodecVersion:
oldCanSpeak = self.canSpeak
cv = CodecVersion()
cv.ParseFromString(message)
log.debug("alpha = " + str(cv.alpha))
log.debug("beta = " + str(cv.beta))
if cv.alpha != None and cv.alpha == supportedCodec:
self.codec = CODEC_ALPHA
elif cv.beta != None and cv.beta == supportedCodec:
self.codec = CODEC_BETA
self.canSpeak = self.canSpeak and (self.codec != CODEC_NOCODEC)
if self.canSpeak != oldCanSpeak:
self.connectionObserver.currentUserUpdated()
elif msgType == MessageType.ServerSync:
ss = ServerSync()
ss.ParseFromString(message)
self.session=ss.session
self.authenticated = True
u = self.__findUser(self.session)
self.currentChannel = u['channel_id']
self.pingThread = PingThread(self)
self.pingThread.start()
us = UserState()
us.session=self.session
self.connectionObserver.setConnectionState(ConnectionState.Connected)
self.connectionObserver.serverSyncCompleted()
elif msgType == MessageType.ChannelState:
cs = ChannelState()
cs.ParseFromString(message)
c = self.findChannel(cs.channel_id)
if c != None:
c['name'] = cs.name
return
self.channelList.append({'channel_id':cs.channel_id,'name':cs.name})
elif msgType == MessageType.ChannelRemove:
cr = ChannelRemove()
cr.ParseFromString(message)
# to do
elif msgType == MessageType.UserState:
us = UserState()
us.ParseFromString(message)
u = self.__findUser(us.session)
if u != None:
if us.channel_id != None:
u['channel_id'] = us.channel_id
if us.session == self.session:
self.currentChannel = u['channel_id']
if us.session == self.session:
if us.mute != None:
self.canSpeak = not us.mute
if us.suppress != None:
self.canSpeak = not us.suppress
return
self.userList.append({'session_id':us.session,'name':us.name,'channel_id':us.channel_id})
elif msgType == MessageType.UserRemove:
pass
elif msgType == MessageType.TextMessage:
pass
elif msgType == MessageType.Version:
pass
elif msgType == MessageType.ServerConfig:
sc = ServerConfig()
sc.ParseFromString(message)
msgLength = sc.message_length
if msgLength > 0:
welcome_msg = self.__readFully(msgLength)
log.debug(welcome_msg)
imageMsgLength = sc.image_message_length
if imageMsgLength > 0:
welcome_image = self.__readFully(imageMsgLength)
elif msgType == MessageType.CryptSetup:
cs = CryptSetup()
cs.ParseFromString(message)
self.key = cs.key
self.encrypt_iv = cs.server_nonce
self.decrypt_iv = cs.client_nonce
if self.key != None and self.encrypt_iv != None and self.decrypt_iv != None:
self.CryptState.setKey(self.key, self.encrypt_iv, self.decrypt_iv)
else:
log.debug("unhandled message type: " + str(msgType))
def connect(self):
log.debug("Attempting to connect...")
tcpSock=socket.socket(type=socket.SOCK_STREAM)
self.socket=ssl.wrap_socket(tcpSock,ssl_version=ssl.PROTOCOL_TLSv1)
self.socket.setsockopt(socket.SOL_TCP,socket.TCP_NODELAY,1)
try:
self.socket.connect((self.host,self.port))
except Exception as e:
log.error(e)
return
log.debug("Connected.")
self.isConnected = True
self.connectionObserver.setConnectionState(ConnectionState.Connected)
self.sockFD = self.socket.fileno()
self.__handleProtocol()
def disconnect(self):
self.disconnecting = True
self.stateLock.acquire()
try:
# if self.readingThread != None:
self.isConnected = False
# self.readingThread.join()
# self.readingThread = None
except Exception as e:
log.error(e)
self.stateLock.release()
def setComment(self, comment):
log.debug("Setting comment to: " + comment)
us = UserState()
us.session = self.session
us.comment = comment
self.sendMessage(MessageType.UserState, us)