/
session.py
708 lines (553 loc) · 22.2 KB
/
session.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
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
"""
session stuff for jabber connections
"""
from twisted.internet import defer, reactor
from twisted.python import log
from twisted.web import server
from twisted.names.srvconnect import SRVConnector
try:
from twisted.words.xish import domish, xmlstream
from twisted.words.protocols import jabber as jabber_protocol
except ImportError:
from twisted.xish import domish, xmlstream
import traceback
import os
from punjab import jabber
from punjab.xmpp import ns
import time
import error
try:
from twisted.internet import ssl
except ImportError:
ssl = None
if ssl and not ssl.supported:
ssl = None
if not ssl:
log.msg("SSL ERROR: You do not have ssl support this may cause problems with tls client connections.")
class XMPPClientConnector(SRVConnector):
"""
A jabber connection to find srv records for xmpp client connections.
"""
def __init__(self, client_reactor, domain, factory):
""" Init """
SRVConnector.__init__(self, client_reactor, 'xmpp-client', domain, factory)
self.timeout = [1,3]
def pickServer(self):
"""
Pick a server and port to make the connection.
"""
host, port = SRVConnector.pickServer(self)
if port == 5223 and ssl:
context = ssl.ClientContextFactory()
context.method = ssl.SSL.SSLv23_METHOD
self.connectFuncName = 'connectSSL'
self.connectFuncArgs = (context,)
return host, port
def make_session(pint, attrs, session_type='BOSH'):
"""
pint - punjab session interface class
attrs - attributes sent from the body tag
"""
s = Session(pint, attrs)
if pint.v:
log.msg('================================== %s connect to %s:%s ==================================' % (str(time.time()),s.hostname,s.port))
connect_srv = True
if attrs.has_key('route'):
connect_srv = False
if s.hostname in ['localhost', '127.0.0.1']:
connect_srv = False
if not connect_srv:
reactor.connectTCP(s.hostname, s.port, s, bindAddress=pint.bindAddress)
else:
connector = XMPPClientConnector(reactor, s.hostname, s)
connector.connect()
# timeout
reactor.callLater(s.inactivity, s.checkExpired)
pint.sessions[s.sid] = s
return s, s.waiting_requests[0].deferred
class WaitingRequest(object):
"""A helper object for managing waiting requests."""
def __init__(self, deferred, delayedcall, timeout = 30, startup = False, rid = None):
""" """
self.deferred = deferred
self.delayedcall = delayedcall
self.startup = startup
self.timeout = timeout
self.wait_start = time.time()
self.rid = rid
def doCallback(self, data):
""" """
self.deferred.callback(data)
def doErrback(self, data):
""" """
self.deferred.errback(data)
class Session(jabber.JabberClientFactory, server.Session):
""" Jabber Client Session class for client XMPP connections. """
def __init__(self, pint, attrs):
"""
Initialize the session
"""
if attrs.has_key('charset'):
self.charset = str(attrs['charset'])
else:
self.charset = 'utf-8'
self.to = attrs['to']
self.port = 5222
self.inactivity = 900
if self.to != '' and self.to.find(":") != -1:
# Check if port is in the 'to' string
to, port = self.to.split(':')
if port:
self.to = to
self.port = int(port)
else:
self.port = 5222
self.sid = "".join("%02x" % ord(i) for i in os.urandom(20))
jabber.JabberClientFactory.__init__(self, self.to, pint.v)
server.Session.__init__(self, pint, self.sid)
self.pint = pint
self.attrs = attrs
self.s = None
self.elems = []
rid = int(attrs['rid'])
self.waiting_requests = []
self.use_raw = attrs.get('raw', False)
self.raw_buffer = u""
self.xmpp_node = ''
self.success = 0
self.mechanisms = []
self.xmlstream = None
self.features = None
self.session = None
self.cache_data = {}
self.verbose = self.pint.v
self.noisy = self.verbose
self.version = attrs.get('version', 0.0)
self.key = attrs.get('newkey')
self.wait = int(attrs.get('wait', 0))
self.hold = int(attrs.get('hold', 0))
self.inactivity = int(attrs.get('inactivity', 900)) # default inactivity 15 mins
if attrs.has_key('window'):
self.window = int(attrs['window'])
else:
self.window = self.hold + 2
if attrs.has_key('polling'):
self.polling = int(attrs['polling'])
else:
self.polling = 0
if attrs.has_key('port'):
self.port = int(attrs['port'])
if attrs.has_key('hostname'):
self.hostname = attrs['hostname']
else:
self.hostname = self.to
self.use_raw = getattr(pint, 'use_raw', False) # use raw buffers
self.secure = attrs.has_key('secure') and attrs['secure'] == 'true'
self.authenticator.useTls = self.secure
if attrs.has_key('route'):
if attrs['route'].startswith("xmpp:"):
self.route = attrs['route'][5:]
if self.route.startswith("//"):
self.route = self.route[2:]
# route format change, see http://www.xmpp.org/extensions/xep-0124.html#session-request
rhostname, rport = self.route.split(":")
self.port = int(rport)
self.hostname = rhostname
self.resource = ''
else:
raise error.Error('internal-server-error')
self.authid = 0
self.rid = rid + 1
self.connected = 0 # number of clients connected on this session
self.notifyOnExpire(self.onExpire)
self.stream_error = None
if pint.v:
log.msg('Session Created : %s %s' % (str(self.sid),str(time.time()), ))
self.addBootstrap(xmlstream.STREAM_START_EVENT, self.streamStart)
self.addBootstrap(xmlstream.STREAM_CONNECTED_EVENT, self.connectEvent)
self.addBootstrap(xmlstream.STREAM_ERROR_EVENT, self.streamError)
self.addBootstrap(xmlstream.STREAM_END_EVENT, self.connectError)
# create the first waiting request
d = defer.Deferred()
timeout = 30
rid = self.rid - 1
self.appendWaitingRequest(d, rid,
timeout=timeout,
poll=self._startup_timeout,
startup=True,
)
def rawDataIn(self, buf):
""" Log incoming data on the xmlstream """
if self.pint.v:
try:
log.msg("SID: %s => RECV: %r" % (self.sid, buf,))
except:
log.err()
if self.use_raw and self.authid:
if type(buf) == type(''):
buf = unicode(buf, 'utf-8')
# add some raw data
self.raw_buffer = self.raw_buffer + buf
def rawDataOut(self, buf):
""" Log outgoing data on the xmlstream """
try:
log.msg("SID: %s => SEND: %r" % (self.sid, buf,))
except:
log.err()
def _wrPop(self, data, i=0):
"""Pop off a waiting requst, do callback, and cache request
"""
wr = self.waiting_requests.pop(i)
wr.doCallback(data)
self._cacheData(wr.rid, data)
def clearWaitingRequests(self, hold = 0):
"""clear number of requests given
hold - number of requests to clear, default is all
"""
while len(self.waiting_requests) > hold:
self._wrPop([])
def _wrError(self, err, i = 0):
wr = self.waiting_requests.pop(i)
wr.doErrback(err)
def appendWaitingRequest(self, d, rid, timeout=None, poll=None, startup=False):
"""append waiting request
"""
if timeout is None:
timeout = self.wait
if poll is None:
poll = self._pollTimeout
self.waiting_requests.append(
WaitingRequest(d,
poll,
timeout = timeout,
rid = rid,
startup=startup))
def returnWaitingRequests(self):
"""return a waiting request
"""
while len(self.elems) > 0 and len(self.waiting_requests) > 0:
data = self.elems
self.elems = []
self._wrPop(data)
def onExpire(self):
""" When the session expires call this. """
if 'onExpire' in dir(self.pint):
self.pint.onExpire(self.sid)
if self.verbose and not getattr(self, 'terminated', False):
log.msg('SESSION -> We have expired', self.sid, self.rid, self.waiting_requests)
self.disconnect()
def terminate(self):
"""Terminates the session."""
self.wait = 0
self.terminated = True
if self.verbose:
log.msg('SESSION -> Terminate')
# if there are any elements hanging around and waiting
# requests, send those off
self.returnWaitingRequests()
self.clearWaitingRequests()
try:
self.expire()
except:
self.onExpire()
return defer.succeed(self.elems)
def poll(self, d = None, rid = None):
"""Handles the responses to requests.
This function is called for every request except session setup
and session termination. It handles the reply portion of the
request by returning a deferred which will get called back
when there is data or when the wait timeout expires.
"""
# queue this request
if d is None:
d = defer.Deferred()
if self.pint.error:
d.addErrback(self.pint.error)
if not rid:
rid = self.rid - 1
self.appendWaitingRequest(d, rid)
# check if there is any data to send back to a request
self.returnWaitingRequests()
# make sure we aren't queueing too many requests
self.clearWaitingRequests(self.hold)
return d
def _pollTimeout(self, d):
"""Handle request timeouts.
Since the timeout function is called, we must return an empty
reply as there is no data to send back.
"""
# find the request that timed out and reply
pop_eye = []
for i in range(len(self.waiting_requests)):
if self.waiting_requests[i].deferred == d:
pop_eye.append(i)
self.touch()
for i in pop_eye:
self._wrPop([],i)
def _pollForId(self, d):
if self.xmlstream.sid:
self.authid = self.xmlstream.sid
self._pollTimeout(d)
def connectEvent(self, xs):
self.version = self.authenticator.version
self.xmlstream = xs
if self.pint.v:
# add logging for verbose output
self.xmlstream.rawDataOutFn = self.rawDataOut
self.xmlstream.rawDataInFn = self.rawDataIn
if self.version == '1.0':
self.xmlstream.addObserver("/features", self.featuresHandler)
def streamStart(self, xs):
"""
A xmpp stream has started
"""
# This is done to fix the stream id problem, I should submit a bug to twisted bugs
try:
self.authid = self.xmlstream.sid
if not self.attrs.has_key('no_events'):
self.xmlstream.addOnetimeObserver("/auth", self.stanzaHandler)
self.xmlstream.addOnetimeObserver("/response", self.stanzaHandler)
self.xmlstream.addOnetimeObserver("/success", self._saslSuccess)
self.xmlstream.addOnetimeObserver("/failure", self._saslError)
self.xmlstream.addObserver("/iq/bind", self.bindHandler)
self.xmlstream.addObserver("/bind", self.stanzaHandler)
self.xmlstream.addObserver("/challenge", self.stanzaHandler)
self.xmlstream.addObserver("/message", self.stanzaHandler)
self.xmlstream.addObserver("/iq", self.stanzaHandler)
self.xmlstream.addObserver("/presence", self.stanzaHandler)
# TODO - we should do something like this
# self.xmlstream.addObserver("/*", self.stanzaHandler)
except:
log.err(traceback.print_exc())
self._wrError(error.Error("remote-connection-failed"))
self.disconnect()
def featuresHandler(self, f):
"""
handle stream:features
"""
f.prefixes = ns.XMPP_PREFIXES.copy()
#check for tls
self.f = {}
for feature in f.elements():
self.f[(feature.uri, feature.name)] = feature
starttls = (ns.TLS_XMLNS, 'starttls') in self.f
initializers = getattr(self.xmlstream, 'initializers', [])
self.features = f
self.xmlstream.features = f
# There is a tls initializer added by us, if it is available we need to try it
if len(initializers)>0 and starttls:
self.secure = True
if self.authid is None:
self.authid = self.xmlstream.sid
# If we get tls, then we should start tls, wait and then return
# Here we wait, the tls initializer will start it
if starttls and self.secure:
if self.verbose:
log.msg("Wait until starttls is completed.")
log.msg(initializers)
return
self.elems.append(f)
if len(self.waiting_requests) > 0:
self.returnWaitingRequests()
self.elems = [] # reset elems
self.raw_buffer = u"" # reset raw buffer, features should not be in it
def bindHandler(self, stz):
"""bind debugger for punjab, this is temporary! """
if self.verbose:
try:
log.msg('BIND: %s %s' % (str(self.sid), str(stz.bind.jid)))
except:
log.err()
if self.use_raw:
self.raw_buffer = stz.toXml()
def stanzaHandler(self, stz):
"""generic stanza handler for httpbind and httppoll"""
stz.prefixes = ns.XMPP_PREFIXES
if self.use_raw and self.authid:
stz = domish.SerializedXML(self.raw_buffer)
self.raw_buffer = u""
self.elems.append(stz)
if self.waiting_requests and len(self.waiting_requests) > 0:
# if there are any waiting requests, give them all the
# data so far, plus this new data
self.returnWaitingRequests()
def _startup_timeout(self, d):
# this can be called if connection failed, or if we connected
# but never got a stream features before the timeout
if self.pint.v:
log.msg('================================== %s %s startup timeout ==================================' % (str(self.sid), str(time.time()),))
for i in range(len(self.waiting_requests)):
if self.waiting_requests[i].deferred == d:
# check if we really failed or not
if self.authid:
self._wrPop(self.elems, i=i)
else:
self._wrError(error.Error("remote-connection-failed"), i=i)
def buildRemoteError(self, err_elem=None):
e = error.Error('remote-stream-error')
e.error_stanza = 'remote-stream-error'
e.children = []
if err_elem:
e.children.append(err_elem)
return e
def streamError(self, streamerror):
"""called when we get a stream:error stanza"""
if isinstance(streamerror.value, jabber_protocol.error.StreamError):
# This is an actual stream:error. Create a remote-stream-error to encapsulate it.
err_elem = getattr(streamerror.value, "element")
e = self.buildRemoteError(err_elem)
else:
# This is another error, such as an XML parsing error. This isn't a stream:error,
# so expose it as remote-connection-failed.
e = error.Error('remote-connection-failed')
do_expire = True
if len(self.waiting_requests) > 0:
wr = self.waiting_requests.pop(0)
wr.doErrback(e)
else: # need to wait for a new request and then expire
do_expire = False
if self.pint and self.pint.sessions.has_key(self.sid):
if do_expire:
try:
self.expire()
except:
self.onExpire()
else:
s = self.pint.sessions.get(self.sid)
s.stream_error = e
def connectError(self, xs):
"""called when we get disconnected"""
# If the connection was established and lost, then we need to report the error
# back to the client, since he needs to reauthenticate. FIXME: If the connection was
# lost before anything happened, we could silently retry instead.
if self.verbose:
log.msg('connect ERROR')
try:
log.msg(xs)
except:
pass
self.stopTrying()
e = error.Error('remote-connection-failed')
do_expire = True
if self.waiting_requests:
wr = self.waiting_requests.pop(0)
wr.doErrback(e)
else: # need to wait for a new request and then expire
do_expire = False
if self.pint and self.pint.sessions.has_key(self.sid):
if do_expire:
try:
self.expire()
except:
self.onExpire()
else:
s = self.pint.sessions.get(self.sid)
s.stream_error = e
def sendRawXml(self, obj):
"""
Send a raw xml string, not a domish.Element
"""
self.touch()
self._send(obj)
def _send(self, xml):
"""
Send valid data over the xmlstream
"""
if self.xmlstream: # FIXME this happens on an expired session and the post has something to send
if isinstance(xml, domish.Element):
xml.localPrefixes = {}
self.xmlstream.send(xml)
def _removeObservers(self, typ = ''):
if typ == 'event':
observers = self.xmlstream._eventObservers
else:
observers = self.xmlstream._xpathObservers
emptyLists = []
for priority, priorityObservers in observers.iteritems():
for query, callbacklist in priorityObservers.iteritems():
callbacklist.callbacks = []
emptyLists.append((priority, query))
for priority, query in emptyLists:
del observers[priority][query]
def disconnect(self):
"""
Disconnect from the xmpp server.
"""
if not getattr(self, 'xmlstream',None):
return
if self.xmlstream:
#sh = "<presence type='unavailable' xmlns='jabber:client'/>"
sh = "</stream:stream>"
self.xmlstream.send(sh)
self.stopTrying()
if self.xmlstream:
self.xmlstream.transport.loseConnection()
del self.xmlstream
self.connected = 0
self.pint = None
self.elems = []
if self.waiting_requests:
self.clearWaitingRequests()
del self.waiting_requests
self.mechanisms = None
self.features = None
def checkExpired(self):
"""
Check if the session or xmpp connection has expired
"""
# send this so we do not timeout from servers
if getattr(self, 'xmlstream', None):
self.xmlstream.send(' ')
if self.inactivity is None:
wait = 900
elif self.inactivity == 0:
wait = time.time()
else:
wait = self.inactivity
if self.waiting_requests and len(self.waiting_requests)>0:
wait += self.wait # if we have pending requests we need to add the wait time
if time.time() - self.lastModified > wait+(0.1):
if self.site.sessions.has_key(self.uid):
self.terminate()
else:
pass
else:
reactor.callLater(wait, self.checkExpired)
def _cacheData(self, rid, data):
if len(self.cache_data.keys())>=3:
# remove the first one in
keys = self.cache_data.keys()
keys.sort()
del self.cache_data[keys[0]]
self.cache_data[int(rid)] = data
# This stuff will leave when SASL and TLS are implemented correctly
# session stuff
def _sessionResultEvent(self, iq):
""" """
if len(self.waiting_requests)>0:
wr = self.waiting_requests.pop(0)
d = wr.deferred
else:
d = None
if iq["type"] == "result":
if d:
d.callback(self)
else:
if d:
d.errback(self)
def _saslSuccess(self, s):
""" """
self.success = 1
self.s = s
# return success to the client
if len(self.waiting_requests)>0:
self._wrPop([s])
self.authenticator._reset()
if self.use_raw:
self.raw_buffer = u""
def _saslError(self, sasl_error, d = None):
""" SASL error """
if d:
d.errback(self)
if len(self.waiting_requests)>0:
self._wrPop([sasl_error])