def receiveData(self): while True: try: data = streamData(self.client) data = data.decode("utf-8") data = Message.from_json(data) # it's a dataclass object except AttributeError: print("\r[*] Connection closed by the server") break if data.typ == "export": timestamp = str(datetime.now()) timestamp = timestamp.replace(":", ".") # windowz is stoopid chat_file = f"./exported/chat{timestamp}.txt" try: with open(chat_file, "wb+") as chat: chat.write(data.cont.encode("utf-8")) print("\r[*] Writing to file...") print( f"[*] Finished! You can find the file at {chat_file}") except: print('\r' + "[*] Something went wrong") elif data.typ == "client_list_update_add" or data.typ == "disconnection": updateClientList(data.cont) else: eel.writeMsg(data.cont, data.username) self.client.close()
def handler(self, client_socketObj): client_socket = client_socketObj.socketObj # renaming address = client_socketObj.getIP() # renaming while True: try: ''' HANDLING DATA FLOW ''' data = streamData(client_socket) # stream it data = decryptMsg(data, client_socketObj.encKey) # decrypting it data = Message.from_json(data) # converting to obj except ConnectionResetError: print( f"*** [{address}] unexpectedly closed the connection, received only an RST packet." ) self.closeConnection(client_socketObj) break except AttributeError: print(f"*** [{address}] disconnected") self.closeConnection(client_socketObj) break except UnicodeDecodeError: print(f"*** [{address}] disconnected due to an encoding error") self.closeConnection(client_socketObj) break except TypeError: print(f"*** [{address}] disconnected") self.closeConnection(client_socketObj) break if data.typ == 'setuser': # clientConnection obj updated in the self.checkUsername function self.checkUsername(client_socketObj, data) if self.temp_f: continue elif data.typ == 'key_exc': finalKey = serverDH.update(int( data.cont)) # generating the shared private secret client_socketObj.encKey = finalKey else: if data.cont != '': if data.typ == 'default': self.logCurrentChat(data.username, data.cont) if data.typ == 'export': print("*** Sending chat...") self.sendLoggedChat(client_socketObj) else: # no need to pack the messages here because its done in the 'self.sendMessageToClients' function for connection in self.clientConnections: if connection.socketObj != client_socket: self.sendMessageToClient(connection, data) # broadcasting
def setUsername(self): while True: self.USERNAME = input("Enter username> ") if self.USERNAME: # encrypted_username = self.cipher.encrypt(self.USERNAME.encode("utf-8")) packet = Message(self.CLIENT_IP, self.SERVER_IP, "temp", str(datetime.now()), self.USERNAME, 'setuser') self.client.send(packet.pack().encode("utf-8")) check = streamData(self.client) print(check["cont"]) if check["cont"] != "[*] Username already in use!": break else: print("Username can't be empty!")
def receiveData(self): iThread = threading.Thread(target=self.sendMsg) iThread.daemon = True iThread.start() while True: data = streamData(self.client).decode("utf-8") if not data: print("[*] Connection closed by the server") sys.exit() if self.export == True: data = Message.from_json(data) # it's a dataclass object timestamp = datetime.now() chat_file = f"./exported/chat{str(timestamp)}.txt" try: with open(chat_file, "wb+") as chat: chat.write(data.cont.encode("utf-8")) print("[*] Writing to file...") print( f"[*] Finished! You can find the file at {chat_file}") self.export = False print('\n' + "You> ", end="") except: self.export = False print('\r' + "[*] Something went wrong" + '\n' + "You> ", end="") else: if self.help == True: data = json.loads(data) for command in data: print('\r' + command + " : " + data[command]) print('\r' + "You> ", end="") self.help = False else: data = Message.from_json(data) # it's a dataclass object print('\r' + data.username + "> " + data.cont + '\n' + "You> ", end="")
def handler(self, client_socket, address): while True: try: data = streamData(client_socket) except ConnectionResetError: print( f"*** [{address[0]}] unexpectedly closed the connetion, received only an RST packet." ) self.closeConnection(client_socket, address) break if not data: print(f"*** [{address[0]}] disconnected") self.closeConnection(client_socket, address) break if data["typ"] == 'setuser': self.checkUsername(client_socket, address, data) if self.temp_f == True: continue else: if data["cont"] != '': if data["typ"] == 'default': self.logChat(data["cont"]) self.current(data["cont"]) else: self.logChat(data["cont"]) if data["typ"] == 'export': print("*** Sending chat...") self.exportChat(client_socket, address) elif data["typ"] == 'help': print("*** Sending command list...") self.commandList(client_socket) else: for connection in self.connections: if connection != client_socket: connection.send( createMsg( json.dumps(data)).encode("utf-8"))
def receiveData(self): iThread = threading.Thread(target=self.sendMsg) iThread.daemon = True iThread.start() while True: data = streamData(self.client) if not data: print("[*] Connection closed by the server") sys.exit() if self.export == True: timestamp = datetime.now() chat_file = f"./exported/chat{str(timestamp)}.txt" try: with open(chat_file, "wb+") as chat: chat.write(data["cont"]) print("[*] Writing to file...") print( f"[*] Finished! You can find the file at {chat_file}") self.export = False print('\n' + "You> ", end="") except: self.export = False print('\r' + "[*] Something went wrong" + '\n' + "You> ", end="") else: if self.help == True: for command in data: print('\r' + command + " : " + data[command]) print('\r' + "You> ", end="") self.help = False else: print('\r' + data["username"] + "> " + data["cont"] + '\n' + "You> ", end="")
def handler(self, client_socket, address): while True: try: data = streamData(client_socket).decode("utf-8") data = Message.from_json(data) except ConnectionResetError: print( f"*** [{address[0]}] unexpectedly closed the connetion, received only an RST packet." ) self.closeConnection(client_socket, address) break except AttributeError: print(f"*** [{address[0]}] disconnected") self.closeConnection(client_socket, address) break if data.typ == 'setuser': self.checkUsername(client_socket, address, data) if self.temp_f == True: continue else: if data.cont != '': if data.typ == 'default': self.logChat(data.cont) self.current(data.cont) else: self.logChat(data.cont) if data.typ == 'export': print("*** Sending chat...") self.exportChat(client_socket, address) elif data.typ == 'help': print("*** Sending command list...") self.commandList(client_socket) else: for connection in self.connections: if connection != client_socket: connection.send(data.pack().encode("utf-8"))
def recvKey(self): return streamData(self.client)
def recvVector(self): iv = streamData(self.client).decode("utf-8") return Message.from_json(iv)
def recvServerKey(self): # receives the servers public key and uses it to generate the final decryption key serverKey = Message.from_json(streamData(self.client).decode("utf-8")) return clientDH.update(int(serverKey.cont))