def timercheck(self): for ses in self.sessionlist: if ses.TmPing+60<int(time.time()): ses.stream.ping() a={'from':self.manager.servname, 'id':utils.randstr(8), 'type':'get'} if ses.ident()!='': a['to']=ses.ident() nx = xm(ses.SentHeader,tag='iq',attrib=a, sub=[xm(ses.SentHeader, tag='ping', attrib={'xmlns':'urn:xmpp:ping'})]) ses.send(nx.tostring(),force=True) ses.TmPing = int(time.time()) pass if ses.TmRmsg+(180*3)<int(time.time()): if ses.Type==ns.TYPE_S: if ses.activeopen==False: ses.stream.close() pass else: ses.stream.close() pass pass pass pass
def timercheck(self): for ses in self.sessionlist: if ses.TmPing + 60 < int(time.time()): ses.stream.ping() a = { 'from': self.manager.servname, 'id': utils.randstr(8), 'type': 'get' } if ses.ident() != '': a['to'] = ses.ident() nx = xm(ses.SentHeader, tag='iq', attrib=a, sub=[ xm(ses.SentHeader, tag='ping', attrib={'xmlns': 'urn:xmpp:ping'}) ]) ses.send(nx.tostring(), force=True) ses.TmPing = int(time.time()) pass if ses.TmRmsg + (180 * 3) < int(time.time()): if ses.Type == ns.TYPE_S: if ses.activeopen == False: ses.stream.close() pass else: ses.stream.close() pass pass pass pass
def __init__(self, man): self.manager = man self.realm = self.manager.servname self.nonce = utils.randstr(14) self.STATE_INIT = 0 self.STATE_CHALLENGE = 1 self.STATE_CHALLENGEOK = 2 self.state = 0 pass
def Rand(self,len): return utils.randstr(len)
def recv_internal(self, stream, m): self.CntRMsg += 1 self.TmRmsg = int(time.time()) x = xm(self.RcvdHeader) x.fromstring(m) # ============================================================ # receive stream header # ============================================================ if x.e.tag=='{http://etherx.jabber.org/streams}stream': self.RcvdHeader = m nx = xm(self.SentHeader) a = {'xmlns:stream':'http://etherx.jabber.org/streams', 'xmlns:xml':"http://www.w3.org/XML/1998/namespace"} if 'version' in x.e.attrib: self.streamver = float(x.e.attrib['version']) if self.streamver == 1.0: a['version'] = '1.0' elif self.streamver == 0.9: pass else: pass # ERROR: unknown version pass else: self.streamver = 0.9 pass #utils.dprint("stream version: "+ str(self.streamver)) if m.find('jabber:client')>0: self.Type=ns.TYPE_C a['xmlns'] = 'jabber:client' a['from'] = self.manager.servname nx.create(tag='stream:stream', attrib=a) self.SentHeader = '' if self.SentHeader=='': self.SentHeader = nx.tostring() self.send(self.SentHeader) pass mec = xm(self.SentHeader) mec.create(tag='mechanisms', attrib={'xmlns':'urn:ietf:params:xml:ns:xmpp-sasl'}, sub=[xm(self.SentHeader,tag='mechanism',text='PLAIN'), xm(self.SentHeader,tag='mechanism',text='DIGEST-MD5'), xm(self.SentHeader,tag='required')]) bid = xm(self.SentHeader) bid.create(tag='bind', attrib={'xmlns':'urn:ietf:params:xml:ns:xmpp-bind'}, sub=[xm(self.SentHeader,tag='required')]) stg = xm(self.SentHeader) stg.create(tag='session', attrib={'xmlns':'urn:ietf:params:xml:ns:xmpp-session'}, sub=[xm(self.SentHeader,tag='optional')]) nx = xm(self.SentHeader) subtag=[] if self.username == '': subtag=[mec] else: subtag=[bid,stg] nx.create(tag='stream:features', sub=subtag) self.send(nx.tostring()) pass elif m.find('jabber:component:accept')>0: self.Type=ns.TYPE_M a['xmlns'] = 'jabber:component:accept' a['id'] = utils.randstr(8) a['from'] = self.manager.servname nx.create(tag='stream:stream', attrib=a) if self.SentHeader=='': self.SentHeader = nx.tostring() self.send(self.SentHeader) pass self.peername = x.e.attrib['from'] pass elif m.find('jabber:server')>0: self.Type=ns.TYPE_S if 'from' in x.e.attrib: self.peername = x.e.attrib['from'] a['xmlns'] = 'jabber:server' a['xmlns:db']='jabber:server:dialback' if self.peername!='': a['to'] = self.peername self.streamid = utils.randstr(16) a['id'] = self.streamid nx.create(tag='stream:stream', attrib=a) msg = '' if self.SentHeader=='': self.SentHeader = nx.tostring() msg = msg+nx.tostring() if self.streamver == 1.0: msg += '<stream:features>'+\ '<dialback xmlns="urn:xmpp:features:dialback">'+\ '<optional/></dialback></stream:features>' pass self.send(msg) if self.activeopen: key=create_key(utils.randstr(16), self.manager.servname,self.peername, self.streamid) nx = xm(self.SentHeader,tag='db:result', attrib={'from':self.manager.servname, 'to':self.peername}, text=key) self.send(nx.tostring(),force=True) pass pass return # ============================================================ # msg forward check # ============================================================ #utils.dprint(x.e.tag) if 'to' in x.e.attrib: #utils.dprint(x.e.attrib['to']) (uname, sname, rname) = splitjid(x.e.attrib['to']) #utils.dprint((uname, sname, rname)) for sess in self.manager.sessmanager.sessionlist: fw = False if sess==self: continue if sess.Type==ns.TYPE_S and sname==sess.ident(): if sess.activeopen and sess.authorized: fw = True pass elif x.e.attrib['to']==sess.ident(): fw = True elif sess.Type==ns.TYPE_C and x.e.attrib['to']==sess.barejid(): fw = True elif sess.Type==ns.TYPE_C and (uname+'@'+sname)==sess.barejid(): fw = True elif sname==sess.ident(): fw = True if fw==True: if sess.Type == ns.TYPE_S: if sess.ident()==sname and \ sess.authorized and \ sess.activeopen==True: self.forward(sess,m) pass pass else: self.forward(sess,m) return pass if sname!=self.manager.servname: item = (self,m,int(time.time()),'init') self.manager.sessmanager.pendingmsg.append(item) return pass # ============================================================ # in case of client connection # ============================================================ if self.Type==ns.TYPE_C: if x.e.tag=='{urn:ietf:params:xml:ns:xmpp-sasl}auth' or \ x.e.tag=='{urn:ietf:params:xml:ns:xmpp-sasl}response': if self.authman == None: self.authman = xmpp.auth.manager(self.manager.servname, self.send, self.pwfunc, self.authenticated) (self.authman.sendsthdr, self.authman.recvsthdr) = (self.SentHeader,self.RcvdHeader) pass if self.authman.authenticated == False: self.authman.proc(m) return return if x.e.tag=='{jabber:client}iq': ResourceTag='{NS}bind/{NS}resource' ResourceTag=ResourceTag.format(NS='{urn:ietf:params:xml:ns:xmpp-bind}') ResourceStz = x.e.find(ResourceTag) if ResourceStz!=None: self.resource = ResourceStz.text #utils.dprint("# Client Resource is " + self.resource) nx = xm(self.SentHeader) nx.create(tag='iq', attrib={'id':x.e.attrib['id'], 'type':'result'}, sub=[xm(self.SentHeader, tag='bind', attrib={'xmlns':'urn:ietf:params:xml:ns:xmpp-bind'}, sub=[xm(self.SentHeader,tag='jid', text=self.fulljid())])]) self.send(nx.tostring()) return SessionTag='{NS}session' SessionTag=SessionTag.format(NS='{urn:ietf:params:xml:ns:xmpp-session}') SessionStz = x.e.find(SessionTag) if SessionStz!=None: nx = xm(self.SentHeader,tag='iq', attrib={'id':x.e.attrib['id'],'type':'result'}) self.send(nx.tostring()) return RosterQueryTag='{jabber:iq:roster}query' RosterQueryStz=x.e.find(RosterQueryTag) if RosterQueryStz!=None: nx = xm(self.SentHeader,tag='iq', attrib={'id':x.e.attrib['id'],'type':'result'}, sub=[xm(self.SentHeader,tag='query', attrib={'xmlns':'jabber:iq:roster'})]) self.send(nx.tostring()) return if 'type' in x.e.attrib: if x.e.attrib['type']=='get' or x.e.attrib['type']=='set': nx = xm(self.SentHeader,tag='iq', attrib={'id':x.e.attrib['id'],'type':'result'}) self.send(nx.tostring()) return return pass pass # ============================================================ # in case of component connection # ============================================================ elif self.Type==ns.TYPE_M: if x.e.tag=='{jabber:component:accept}handshake': self.send('<handshake />') #utils.dprint(m) self.send('<handshake />') return pass # ============================================================ # in case of server connection # ============================================================ elif self.Type==ns.TYPE_S: if x.e.tag=='{jabber:server:dialback}verify': nx = xm(self.SentHeader,tag='db:verify', attrib={'xmlns:db':'jabber:server:dialback', 'from':x.e.attrib['to'], 'to':x.e.attrib['from'], 'id':x.e.attrib['id'], 'type':'valid'}, text=x.e.text) self.send(nx.tostring()) return if x.e.tag=='{jabber:server:dialback}result': #self.NeedStFeat = False if 'type' in x.e.attrib: if x.e.attrib['type']=='valid': self.authorized = True else: self.stream.close() return else: nx = xm(self.SentHeader,tag='db:result', attrib={'from':x.e.attrib['to'], 'to':x.e.attrib['from'], 'type':'valid'}) time.sleep(1) self.send(nx.tostring()) return return pass # ============================================================ # error # ============================================================ else: self.stream.close() pass
def recv_internal(self, stream, m): self.CntRMsg += 1 self.TmRmsg = int(time.time()) x = xm(self.RcvdHeader) x.fromstring(m) # ============================================================ # receive stream header # ============================================================ if x.e.tag == '{http://etherx.jabber.org/streams}stream': self.RcvdHeader = m nx = xm(self.SentHeader) a = { 'xmlns:stream': 'http://etherx.jabber.org/streams', 'xmlns:xml': "http://www.w3.org/XML/1998/namespace" } if 'version' in x.e.attrib: self.streamver = float(x.e.attrib['version']) if self.streamver == 1.0: a['version'] = '1.0' elif self.streamver == 0.9: pass else: pass # ERROR: unknown version pass else: self.streamver = 0.9 pass #utils.dprint("stream version: "+ str(self.streamver)) if m.find('jabber:client') > 0: self.Type = ns.TYPE_C a['xmlns'] = 'jabber:client' a['from'] = self.manager.servname nx.create(tag='stream:stream', attrib=a) self.SentHeader = '' if self.SentHeader == '': self.SentHeader = nx.tostring() self.send(self.SentHeader) pass mec = xm(self.SentHeader) mec.create( tag='mechanisms', attrib={'xmlns': 'urn:ietf:params:xml:ns:xmpp-sasl'}, sub=[ xm(self.SentHeader, tag='mechanism', text='PLAIN'), xm(self.SentHeader, tag='mechanism', text='DIGEST-MD5'), xm(self.SentHeader, tag='required') ]) bid = xm(self.SentHeader) bid.create( tag='bind', attrib={'xmlns': 'urn:ietf:params:xml:ns:xmpp-bind'}, sub=[xm(self.SentHeader, tag='required')]) stg = xm(self.SentHeader) stg.create( tag='session', attrib={'xmlns': 'urn:ietf:params:xml:ns:xmpp-session'}, sub=[xm(self.SentHeader, tag='optional')]) nx = xm(self.SentHeader) subtag = [] if self.username == '': subtag = [mec] else: subtag = [bid, stg] nx.create(tag='stream:features', sub=subtag) self.send(nx.tostring()) pass elif m.find('jabber:component:accept') > 0: self.Type = ns.TYPE_M a['xmlns'] = 'jabber:component:accept' a['id'] = utils.randstr(8) a['from'] = self.manager.servname nx.create(tag='stream:stream', attrib=a) if self.SentHeader == '': self.SentHeader = nx.tostring() self.send(self.SentHeader) pass self.peername = x.e.attrib['from'] pass elif m.find('jabber:server') > 0: self.Type = ns.TYPE_S if 'from' in x.e.attrib: self.peername = x.e.attrib['from'] a['xmlns'] = 'jabber:server' a['xmlns:db'] = 'jabber:server:dialback' if self.peername != '': a['to'] = self.peername self.streamid = utils.randstr(16) a['id'] = self.streamid nx.create(tag='stream:stream', attrib=a) msg = '' if self.SentHeader == '': self.SentHeader = nx.tostring() msg = msg + nx.tostring() if self.streamver == 1.0: msg += '<stream:features>'+\ '<dialback xmlns="urn:xmpp:features:dialback">'+\ '<optional/></dialback></stream:features>' pass self.send(msg) if self.activeopen: key = create_key(utils.randstr(16), self.manager.servname, self.peername, self.streamid) nx = xm(self.SentHeader, tag='db:result', attrib={ 'from': self.manager.servname, 'to': self.peername }, text=key) self.send(nx.tostring(), force=True) pass pass return # ============================================================ # msg forward check # ============================================================ #utils.dprint(x.e.tag) if 'to' in x.e.attrib: #utils.dprint(x.e.attrib['to']) (uname, sname, rname) = splitjid(x.e.attrib['to']) #utils.dprint((uname, sname, rname)) for sess in self.manager.sessmanager.sessionlist: fw = False if sess == self: continue if sess.Type == ns.TYPE_S and sname == sess.ident(): if sess.activeopen and sess.authorized: fw = True pass elif x.e.attrib['to'] == sess.ident(): fw = True elif sess.Type == ns.TYPE_C and x.e.attrib[ 'to'] == sess.barejid(): fw = True elif sess.Type == ns.TYPE_C and (uname + '@' + sname) == sess.barejid(): fw = True elif sname == sess.ident(): fw = True if fw == True: if sess.Type == ns.TYPE_S: if sess.ident()==sname and \ sess.authorized and \ sess.activeopen==True: self.forward(sess, m) pass pass else: self.forward(sess, m) return pass if sname != self.manager.servname: item = (self, m, int(time.time()), 'init') self.manager.sessmanager.pendingmsg.append(item) return pass # ============================================================ # in case of client connection # ============================================================ if self.Type == ns.TYPE_C: if x.e.tag=='{urn:ietf:params:xml:ns:xmpp-sasl}auth' or \ x.e.tag=='{urn:ietf:params:xml:ns:xmpp-sasl}response': if self.authman == None: self.authman = xmpp.auth.manager(self.manager.servname, self.send, self.pwfunc, self.authenticated) (self.authman.sendsthdr, self.authman.recvsthdr) = (self.SentHeader, self.RcvdHeader) pass if self.authman.authenticated == False: self.authman.proc(m) return return if x.e.tag == '{jabber:client}iq': ResourceTag = '{NS}bind/{NS}resource' ResourceTag = ResourceTag.format( NS='{urn:ietf:params:xml:ns:xmpp-bind}') ResourceStz = x.e.find(ResourceTag) if ResourceStz != None: self.resource = ResourceStz.text #utils.dprint("# Client Resource is " + self.resource) nx = xm(self.SentHeader) nx.create(tag='iq', attrib={ 'id': x.e.attrib['id'], 'type': 'result' }, sub=[ xm(self.SentHeader, tag='bind', attrib={ 'xmlns': 'urn:ietf:params:xml:ns:xmpp-bind' }, sub=[ xm(self.SentHeader, tag='jid', text=self.fulljid()) ]) ]) self.send(nx.tostring()) return SessionTag = '{NS}session' SessionTag = SessionTag.format( NS='{urn:ietf:params:xml:ns:xmpp-session}') SessionStz = x.e.find(SessionTag) if SessionStz != None: nx = xm(self.SentHeader, tag='iq', attrib={ 'id': x.e.attrib['id'], 'type': 'result' }) self.send(nx.tostring()) return RosterQueryTag = '{jabber:iq:roster}query' RosterQueryStz = x.e.find(RosterQueryTag) if RosterQueryStz != None: nx = xm(self.SentHeader, tag='iq', attrib={ 'id': x.e.attrib['id'], 'type': 'result' }, sub=[ xm(self.SentHeader, tag='query', attrib={'xmlns': 'jabber:iq:roster'}) ]) self.send(nx.tostring()) return if 'type' in x.e.attrib: if x.e.attrib['type'] == 'get' or x.e.attrib[ 'type'] == 'set': nx = xm(self.SentHeader, tag='iq', attrib={ 'id': x.e.attrib['id'], 'type': 'result' }) self.send(nx.tostring()) return return pass pass # ============================================================ # in case of component connection # ============================================================ elif self.Type == ns.TYPE_M: if x.e.tag == '{jabber:component:accept}handshake': self.send('<handshake />') #utils.dprint(m) self.send('<handshake />') return pass # ============================================================ # in case of server connection # ============================================================ elif self.Type == ns.TYPE_S: if x.e.tag == '{jabber:server:dialback}verify': nx = xm(self.SentHeader, tag='db:verify', attrib={ 'xmlns:db': 'jabber:server:dialback', 'from': x.e.attrib['to'], 'to': x.e.attrib['from'], 'id': x.e.attrib['id'], 'type': 'valid' }, text=x.e.text) self.send(nx.tostring()) return if x.e.tag == '{jabber:server:dialback}result': #self.NeedStFeat = False if 'type' in x.e.attrib: if x.e.attrib['type'] == 'valid': self.authorized = True else: self.stream.close() return else: nx = xm(self.SentHeader, tag='db:result', attrib={ 'from': x.e.attrib['to'], 'to': x.e.attrib['from'], 'type': 'valid' }) time.sleep(1) self.send(nx.tostring()) return return pass # ============================================================ # error # ============================================================ else: self.stream.close() pass