def assemble(file_id, skype_name_from): global _InboxFilesNames global _InboxFilesSize dhnio.Dprint(14, 'transport_skype.assemble file_id:%s from %s' % (str(file_id), skype_name_from)) files_list = _InboxFilesNames[file_id] files_list.sort() fd, filename = tempfile.mkstemp(".dhn-skype-in") for part_filename in files_list: src = dhnio.ReadBinaryFile(part_filename) os.write(fd, src) if os.access(part_filename, os.W_OK): try: os.remove(part_filename) except: dhnio.Dprint(1, 'transport_skype.assemble ERROR removing ' + part_filename) try: fd.flush() os.close(fd) except: pass _InboxFilesNames.pop(file_id, None) _InboxFilesSize.pop(file_id, None) transport_control.receiveStatusReport(filename, "finished", 'skype', skype_name_from) transport_control.log('skype', 'finish receiving from ' + str(skype_name_from))
def success(src, idurl, host, port, conn): global _LastPingTimeDict global _ConnectionsDict if len(src) == 0: increase_receiving_delay(idurl) else: parts = src.splitlines() lg.out(14, 'http_node.receive.success %d bytes in %d parts from %s (%s:%s)' % (len(src), len(parts), idurl, host, port)) for part64 in parts: try: part = base64.b64decode(part64.strip()) except: lg.out(14, 'http_node.receive.success ERROR in base64.b64decode()') # lg.out(1, 'len(part64)=%d' % len(part64)) # lg.out(1, 'len(part64.strip())=%d' % len(part64.strip())) # lg.out(1, 'part64=[%s]' % part64) decrease_receiving_delay(idurl) continue # fd, filename = tempfile.mkstemp(".dhn-http-in") fd, filename = tmpfile.make("http-in", extension='.http') os.write(fd, part) os.close(fd) decrease_receiving_delay(idurl) transport_control.receiveStatusReport( filename, 'finished', 'http', host+':'+port,) transport_control.log('http', 'finish connection with %s:%s ' % (host, port)) conn.disconnect() _ConnectionsDict.pop(idurl, None)
def receive(email_info, timeout=15, receive_event=None): transport_control.log('email', 'start receiving from '+str(email_info[0])) dhnio.Dprint(8, 'transport_email.receive. start looking at [%s:%s] user: %s' % (email_info[1], email_info[2], email_info[3])) ## print email_info server = PopCheckerServer(email_info[1], email_info[2], email_info[3], email_info[4], email_info[5], timeout, receive_event) server.loopcall = reactor.callLater(0, server.run) return server
def add_key(name, public_key): global pub_keys_db if pub_keys_db.has_key(name): return pub_keys_db[name] = keys.Key.fromString( public_key ) transport_control.log('ssh', 'add public key to db for ' + name) dhnio.Dprint(10, "transport_ssh.add_key for " + name)
def connectionMade(self): transport_control.log('ssh', 'new income connection') dhnio.Dprint(14, 'transport_ssh.MyReceiveFilesSubsistem.connectionMade') # self.fout, self.filename = tempfile.mkstemp(".dhn-ssh-in") self.fout, self.filename = tmpfile.make("ssh-in") self.total_size = 0 self.stop_defer = self.transport.session.avatar.stop_defer
def process_msg(self, msg): dhnio.Dprint(12, 'transport_email.process_msg') take = False if msg['Subject'] == 'datahaven_transport': take = True # fout, self.current_filename = tempfile.mkstemp(".dhn-email-in") fout, self.current_filename = tmpfile.make("email-in") dhnio.Dprint(8, 'transport_email.message from: ' + str(msg['From'])) transport_control.log('email', 'receiving file from ' + str(msg['From'])) for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue src = part.get_payload(decode=True) src = src[:-3] if src != None: os.write(fout,src) os.close(fout) ## transport_control.log('email', 'download... ') poll().receive() if take: self.delete(self.cur_message).addCallbacks( self.delete_msg, self.err_delete_msg) else: self.cur_message += 1 if self.cur_message < self.num_messages: self.retrieve(self.cur_message).addCallbacks( self.do_retrieve_msg, self.err_retrieve_msg) else: self.no_messages()
def receive(port): transport_control.log('ssh', 'start listening on port %s' % str(port)) dhnio.Dprint(8,'transport_ssh.receive going to listen on port %s' % port) def _try_receiving(port, count): dhnio.Dprint(10, "transport_ssh.receive count=%d" % count) components.registerAdapter(None, MyAvatar, session.ISession) d = Deferred() myrealm = MyRealm(d) portal_ = portal.Portal(myrealm) portal_.registerChecker(MyPublicKeyChecker()) MyFactory.portal = portal_ try: mylistener = reactor.listenTCP(port, MyFactory()) except: mylistener = None return mylistener def _loop(port, result, count): l = _try_receiving(port, count) if l is not None: dhnio.Dprint(8, "transport_ssh.receive started on port "+ str(port)) result.callback(l) return if count > 10: dhnio.Dprint(1, "transport_ssh.receive WARNING port %s is busy!" % str(port)) result.errback(None) return reactor.callLater(10, _loop, port, result, count+1) res = Deferred() _loop(port, res, 0) return res
def success(src, idurl, host, port, conn): global _LastPingTimeDict global _ConnectionsDict if len(src) == 0: increase_receiving_delay(idurl) else: parts = src.splitlines() dhnio.Dprint(14, 'transport_http.receive.success %d bytes in %d parts from %s (%s:%s)' % (len(src), len(parts), idurl, host, port)) for part64 in parts: try: part = base64.b64decode(part64.strip()) except: dhnio.Dprint(14, 'transport_http.receive.success ERROR in base64.b64decode()') # dhnio.Dprint(1, 'len(part64)=%d' % len(part64)) # dhnio.Dprint(1, 'len(part64.strip())=%d' % len(part64.strip())) # dhnio.Dprint(1, 'part64=[%s]' % part64) decrease_receiving_delay(idurl) continue # fd, filename = tempfile.mkstemp(".dhn-http-in") fd, filename = tmpfile.make("http-in") os.write(fd, part) os.close(fd) decrease_receiving_delay(idurl) transport_control.receiveStatusReport( filename, 'finished', 'http', host+':'+port,) transport_control.log('http', 'finish connection with %s:%s ' % (host, port)) conn.disconnect() _ConnectionsDict.pop(idurl, None)
def ok(x): dhnio.Dprint(14, 'transport_email.send_public.ok: sending done') ## if misc.transport_control_using(): transport_control.sendStatusReport(email, filename, 'finished', 'email') # only one outstanding send per host transport_control.log('email', 'message successful sent') poll().send() if reactor_stop: reactor.stop()
def _OnApplicationReceiving(app, streams): dhnio.Dprint(14, 'transport_skype._OnApplicationReceiving') transport_control.log('skype', 'incomming connection') if streams.Count == 0: return if streams[0].DataLength == 0: return for stream in streams: _stream_read(stream)
def no_messages(self): global ready_receiving dhnio.Dprint(16, 'transport_email.no_messages') if not ready_receiving: dhnio.Dprint(8, 'transport_email.no_messages: mailbox is empty') transport_control.log('email', 'MAILBOX IS EMPTY. ready to receive new messages') ready_receiving = True ## if self.factory.no_messages_event is not None: ## self.factory.no_messages_event() self.finish()
def set_stats(self, stats): dhnio.Dprint(16, 'transport_email.set_stats') self.num_messages = stats[0] self.cur_message = 0 transport_control.log('email', 'checking mailbox') if self.num_messages == 0: self.no_messages() else: self.retrieve(0).addCallbacks( self.do_retrieve_msg, self.err_retrieve_msg)
def fail(x): dhnio.Dprint(1, 'transport_email.send_public.fail NETERROR sending email \n' + str(x.getErrorMessage())) ## transport_control.msg('e', 'error sending email: ' + str(x.getErrorMessage())) ## transport_control.connectionStatusCallback('email', smtp_info[0], x, 'error sending email') transport_control.sendStatusReport(email, filename, 'failed', 'email', x, 'error sending email') transport_control.log('email', 'NETERROR sending email') if reactor_stop: reactor.stop()
def _stream_read(stream): global _ReceiveNotifier dhnio.Dprint(14, 'transport_skype._stream_read') try: skype_name = stream.Handle.split(':')[0] except: dhnio.Dprint(1, 'transport_skype._stream_read NETERROR wrong stream handle') return total_length = 0 try: parts = stream.Read() except: transport_control.log('skype', 'receiving from %s failed' % str(skype_name)) dhnio.Dprint(4, 'transport_skype._stream_read FAILED receiving from ' + str(skype_name)) return for part in parts.split(' '): if len(part.strip()) == 0: continue try: (lengthStr, data) = part.split(':') except: dhnio.Dprint(1, 'transport_skype._stream_read NETERROR wrong data ' + str(part)) continue try: lengthI = int(lengthStr) except: dhnio.Dprint(1, 'transport_skype._stream_read NETERROR wrong data ' + str(lengthStr)) continue if lengthI != len(data): dhnio.Dprint(1, 'transport_skype._stream_read NETERROR wrong data size') continue if lengthI == 0: continue try: data_decoded = base64.b64decode(data) except: dhnio.Dprint(1, 'transport_skype._stream_read NETERROR decoding data') continue total_length += lengthI packet = unserialize(data_decoded) if packet is None: dhnio.Dprint(1, 'transport_skype._stream_read ERROR unserialize incomming data') continue inbox(packet) if _ReceiveNotifier is not None: _ReceiveNotifier(skype_name, total_length)
def connectionLost(self, reason): dhnio.Dprint(14, 'transport_ssh.MyReceiveFilesSubsistem.connectionLost') try: os.close(self.fout) transport_control.receiveStatusReport( self.filename, "finished", 'ssh', self.transport.getPeer(), reason) except: pass if not self.stop_defer.called: self.stop_defer.callback('done') transport_control.log('ssh', 'income connection finished')
def _stream_write(stream, data, skype_name): global _SentNotifier total_length = 0 for part in data.split(' '): try: stream.Write(str(len(part))+':'+str(part)) except: dhnio.Dprint(4, 'transport_skype._stream_write FAIL sending to ' + str(skype_name)) transport_control.log('skype', 'sending to %s failed' % str(skype_name)) continue total_length += len(part) transport_control.log('skype', 'sending to %s done' % str(skype_name)) dhnio.Dprint(12, 'transport_skype._stream_write %s bytes sent to %s' % (str(total_length), str(skype_name))) if _SentNotifier is not None: _SentNotifier(skype_name, total_length) return total_length
def delete_msg(self, ignore): global ready_receiving dhnio.Dprint(14, 'transport_email.delete_msg') self.cur_message += 1 if self.cur_message < self.num_messages: self.retrieve(self.cur_message).addCallbacks( self.do_retrieve_msg, self.err_retrieve_msg) else: self.no_messages() dhnio.Dprint(14, 'transport_email: received file ' + self.current_filename) if self.factory.receive_event is not None: self.factory.receive_event(self.current_filename) transport_control.log('email', 'receiving done. remove message from server') ## if misc.transport_control_using() and ready_receiving: if ready_receiving: transport_control.receiveStatusReport(self.current_filename, "finished", 'email', self.transport.getPeer())
def send(host, port, username, public_key, private_key, filename): def connection_done(x, filename, c): dhnio.Dprint(14, 'transport_ssh.send.connection_done ') ## c.transport.loseConnection() def connection_fail(x, filename, c): dhnio.Dprint(14, 'transport_ssh.send.connection_fail ') ## if misc.transport_control_using(): transport_control.sendStatusReport(host+':'+port, filename, 'failed', 'ssh', x, 'connection failed') # not so good ## c.transport.loseConnection() def done(x, filename, c): dhnio.Dprint(14, 'transport_ssh.send.done') ## if misc.transport_control_using(): transport_control.sendStatusReport(host+':'+port, filename, 'finished', 'ssh') c.transport.loseConnection() transport_control.log('ssh', 'start sending to '+str(host)+':'+str(port)) dhnio.Dprint(12, 'transport_ssh.send: %s -> %s:%s' % (filename,host,port)) port_ = port try: port_ = int(port) except: dhnio.Dprint(1,'transport_ssh.send ERROR wrong port number: ' + str(port)) return opts = options.ConchOptions() opts.conns = ['direct'] opts['nocache'] = True opts['subsystem'] = 'sftp' result_defer = Deferred() conn = MySSHConnection(filename, result_defer) conn.options = opts vhk = my_verifyHostKey uao = MyUserAuth(username, conn, public_key, private_key) d = Deferred() factory = SendingFactory(d, opts, vhk, uao) c = reactor.connectTCP(host, port_, factory) ## client = c.transport d.addCallback(connection_done, filename, c) d.addErrback(connection_fail, filename, c) result_defer.addCallback(done, filename, c)
def send(filename, email, subject = 'datahaven_transport', reactor_stop=False, use_smtplib=False): transport_control.log('email', 'start sending to ' + email) smtp_info = [ settings.getEmailAddress(), #0 settings.getSMTPHost(), #1 settings.getSMTPPort(), #2 settings.getSMTPUser(), #3 settings.getSMTPPass(), #4 settings.getSMTPSSL(), #5 settings.getSMTPNeedLogin()] #6 if smtp_info[0].strip() == '' or smtp_info[1].strip() == '': dhnio.Dprint(1, 'transport_email.send empty mailbox info. check your email options') ## transport_control.msg('e', 'empty mailbox info. check your email options') ## transport_control.connectionStatusCallback('email', email, None, 'empty mailbox info. check your email options') transport_control.sendStatusReport(email, filename, 'failed', 'email', None, 'empty mailbox info. check your email options') return ## print smtp_info ## smtp_info = smtp3_info if not os.path.isfile(filename): dhnio.Dprint(1, 'transport_email.send wrong file name: %s' % filename) ## transport_control.msg('e', 'wrong file name %s' % filename) return from_address = smtp_info[0] dhnio.Dprint(8, 'transport_email.send from:%s to:%s' % (from_address, email)) if use_smtplib: #send via smtplib try: send_smtplib( smtp_info[1], smtp_info[3], smtp_info[4], smtp_info[5], from_address, email, subject, filename) except: dhnio.Dprint(1, 'transport_email.send NETERROR sending email ' + dhnio.formatExceptionInfo()) ## transport_control.msg('e', 'Error sending email') transport_control.log('email', 'error sending email') ## transport_control.connectionStatusCallback('email', email, None, 'error sending email') transport_control.sendStatusReport(email, filename, 'failed', 'email', None, 'error sending email') return ## if misc.transport_control_using(): transport_control.sendStatusReport(email, filename, 'finished', 'email') # only one outstanding send per host transport_control.log('email', 'done') poll().send() else: #Send via Twisted def ok(x): dhnio.Dprint(14, 'transport_email.send.ok: sending done') ## if misc.transport_control_using(): # only one outstanding send per host transport_control.sendStatusReport(email, filename, 'finished', 'email') transport_control.log('email', 'message successful sent') poll().send() if reactor_stop: reactor.stop() def fail(x): ## if misc.transport_control_using(): dhnio.Dprint(1, 'transport_email.send.fail NETERROR sending email \n' + str(x.getErrorMessage())) ## transport_control.msg('e', 'error sending email: ' + str(x.getErrorMessage()) ) ## transport_control.connectionStatusCallback('email', smtp_info[0], x, 'error sending email') transport_control.sendStatusReport(email, filename, 'failed', 'email', x, 'error sending email') transport_control.log('email', 'error sending email' + str(x.getErrorMessage())) if reactor_stop: reactor.stop() if smtp_info[6]: #1th method res = sendTwistedMailAuth( smtp_info[1], smtp_info[2], smtp_info[3], smtp_info[4], smtp_info[5], smtp_info[6], from_address, email, subject, filename) else: #2th method: res = sendTwistedMail( smtp_info[1], smtp_info[0], email, subject, filename, smtp_info[2]) res.addCallback(ok) res.addErrback(fail) return res
def remove_key(name): global pub_keys_db transport_control.log('ssh', 'remove public key of ' + name) if (pub_keys_db.has_key(name)): pub_keys_db.pop(name)