def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ctimer = QtCore.QTimer() Qt.QObject.connect(self.ui.ConnectButton, Qt.SIGNAL("clicked()"), self.connect) Qt.QObject.connect(self.ui.SendFileChoose, Qt.SIGNAL("clicked()"), self.select_file) Qt.QObject.connect(self.ui.SendFileButton, Qt.SIGNAL("clicked()"), self.sendFile) Qt.QObject.connect(self.ui.GetFileButton, Qt.SIGNAL("clicked()"), self.getFile) QtCore.QObject.connect(self.ctimer, QtCore.SIGNAL("timeout()"), self.constantUpdate) self.initLogger() self.node = kenosis.Node(bootstrapNetAddress=None) self.ui.NodePortLabel.setText(str(self.node.port())) self.node.registerNamedHandler(name="darknet", handler=Handler()) self.table = Table('files.tbl') for f in self.table.table: item = Qt.QListWidgetItem(f['filename']) self.ui.listWidget.addItem(item) self.ctimer.start(1000)
class App(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ctimer = QtCore.QTimer() Qt.QObject.connect(self.ui.ConnectButton, Qt.SIGNAL("clicked()"), self.connect) Qt.QObject.connect(self.ui.SendFileChoose, Qt.SIGNAL("clicked()"), self.select_file) Qt.QObject.connect(self.ui.SendFileButton, Qt.SIGNAL("clicked()"), self.sendFile) Qt.QObject.connect(self.ui.GetFileButton, Qt.SIGNAL("clicked()"), self.getFile) QtCore.QObject.connect(self.ctimer, QtCore.SIGNAL("timeout()"), self.constantUpdate) self.initLogger() self.node = kenosis.Node(bootstrapNetAddress=None) self.ui.NodePortLabel.setText(str(self.node.port())) self.node.registerNamedHandler(name="darknet", handler=Handler()) self.table = Table('files.tbl') for f in self.table.table: item = Qt.QListWidgetItem(f['filename']) self.ui.listWidget.addItem(item) self.ctimer.start(1000) def initLogger(self): self.logText = self.ui.Log self.logText.append("Log started...") self.logger = Logger(self.logText) self.errors = Logger(self.logText) sys.stdout = self.logger sys.stderr = self.errors def constantUpdate(self): count = len(self.get_node_list()) self.ui.NodesCountLabel.setText(str(count)) if count > 1: self.ui.StatusLabel.setText('Connected') else: self.ui.StatusLabel.setText('Disconnected') def get_node_list(self): nodes = ['0x%x'%(x[0].nodeAddressObject().address_) for x in self.node.nodeKernel_._bucketsFor('kenosis') if len(x)>0] nodes.append(self.node.nodeAddress()) return nodes def select_file(self): self.ui.SendFileEdit.setText(QtGui.QFileDialog.getOpenFileName()) def connect(self): hostname = self.ui.hostnameEdit.text() port = self.ui.portEdit.text() res = self.node.bootstrap( "%s:%s" % (hostname, port) ) print "Connecting return: "+str(res) def sendFile(self): nodes = self.get_node_list() filepath = self.ui.SendFileEdit.text().toUtf8().data() name = os.path.split(filepath)[-1] file_size = os.path.getsize(filepath) chunk_size = file_size/len(nodes) key = os.urandom(32) temp_file = NamedTemporaryFile(delete=False) crypter.encrypt_file( key, filepath, temp_file.name ) salt = crypter.generate_salt() i = 0 print temp_file while True: chunk = temp_file.read( chunk_size ) if len(chunk) == 0: break data = base64.b64encode( chunk ) address = nodes[i%len(nodes)] backup_node_address = nodes[(i+1)%len(nodes)] temp_filename = name+"."+salt+".part"+str(i) hashed_filename = hashlib.sha1(temp_filename).hexdigest() print "Sending" res = self.process_sending(address, hashed_filename, data) res_b = self.process_sending(backup_node_address, hashed_filename, data) if not (res and res_b): print "Failed to send file" return i += 1 temp_file.close() os.unlink(temp_file.name) print "Adding file to table..." f = { 'filename':name, 'salt':salt, 'key': key, 'pieces': i } self.table.add(f) item = Qt.QListWidgetItem(f['filename']) self.ui.listWidget.addItem(item) def process_sending(self, address, name, data): attempts = 0 res = '' while res != 'OK': res = self.node.rpc(nodeAddress=address).darknet.put(name,data) attempts += 1 if attempts == 3: return False return True def getFile(self): n = self.ui.listWidget.selectedIndexes()[0].row() fname = self.ui.listWidget.selectedItems()[0].text().toUtf8().data() file_info = self.table.table[n] nodes = self.get_node_list() tempfile = NamedTemporaryFile(delete=False) for i in range(file_info['pieces']): temp_filename = fname+"."+file_info['salt']+".part"+str(i) # TODO: add salt hashed_filename = hashlib.sha1(temp_filename).hexdigest() for node_address in nodes: if self.node.rpc(nodeAddress=node_address).darknet.has_file( hashed_filename ): data = self.node.rpc(nodeAddress=node_address).darknet.get( hashed_filename ) tempfile.write( base64.b64decode(data) ) print "Got part No:"+str(i) break else: print "Can't find part No:"+str(i) return tempfile.close() out_filename = os.path.join(DOWNDIR, fname) crypter.decrypt_file( file_info['key'], tempfile.name, out_filename ) os.unlink(tempfile.name)