def open_new_private_chat(): continue_listening.clear() time.sleep(0.05) swear_protected = is_swear_protected.get() packets = protocol.PacketMaker(CREATE_CHAT, content=int_to_bytes(swear_protected)) for packet in packets: server.send(packet) packet = server.recv(PACKET_SIZE) msg = b"" request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) msg += packet[HEADER_SIZE:] if packet_amount - packet_number > 1: for _ in range(packet_amount - 1): packet = server.recv(PACKET_SIZE) msg += packet[HEADER_SIZE:] server_response = decrypt(msg, key).decode("utf-8") chat_name, chat_id = server_response.split("~~~") user_values.group_key = get_random_bytes(32) user_values.rsa_group_key = RSA.generate(2048) user_values.pin_code = chat_id user_values.chat_name = chat_name user_values.is_safe_chat = bool(swear_protected) user_values.on_chat_raise() stop_listening.set() chat_frame.tkraise()
def on_send_file(): filepath = askopenfilename() if filepath: file_format = extract_file_name(filepath).split(".")[-1] request = SEND_FILE if file_format.upper() in image_file_formats: request = SEND_IMG if os.path.getsize(filepath) / 1000 > 1400: win32api.MessageBox( 0, "hay, sorry you can't send images over 1000 kb", 'sorry', 0x00001000) return else: if os.path.getsize(filepath) / 1000 > 700: win32api.MessageBox( 0, "hay, sorry you can't send files over 700 kb", 'sorry', 0x00001000) return packets = protocol.PacketMaker( request, shared_secret=user_values.group_key, username=user_values.username.encode("utf-8"), file_path=filepath) print("starting to send file") file_packet_list[packets.id] = packets for packet in packets: server.send(packet) print("sending complete")
def listener(): if continue_listening.isSet(): packets = protocol.PacketMaker(GET_CHATS) server.send(next(packets)) packet = server.recv(PACKET_SIZE) msg = b"" request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) msg += packet[HEADER_SIZE:] if packet_amount - packet_number > 1: for _ in range(packet_amount - 1): packet = server.recv(PACKET_SIZE) msg += packet[HEADER_SIZE:] msg = decrypt(msg, key).decode("utf-8") chat_list = json.loads(msg) cur_list = chat_listbox.get(0, "end") for chat in chat_list: if chat not in cur_list: chat_types[chat] = chat_list[chat] chat_listbox.insert(tkinter.END, chat) i = 0 for chat in cur_list: if chat not in chat_list: chat_listbox.delete(i) i += 1 chat_picker_frame.after(1000, listener) else: if not stop_listening.isSet(): chat_picker_frame.after(2000, listener)
def on_submit(*args): username = username_entry.get() password = password_entry.get() if username and password: content = username.encode("utf-8") + bytes(USERNAME_MAX_LEN - len(username)) content += hash_password(password).encode("utf-8") + bytes(PASSWORD_MAX_LEN - len(password)) packets = protocol.PacketMaker(LOGIN, shared_secret=key, content=content) for packet in packets: server.send(packet) request, can_join_chat = protocol_digest.is_logged_in(server.recv(PACKET_SIZE)) if can_join_chat: server_error.grid_forget() user_values.username = username tkinter.Label(chat_picker_frame, text=f"hello {user_values.username}!", font="arial 23").grid(row=0, column=0, columnspan=3, sticky="NWE") user_values.on_raise_chat_picker() chat_picker_frame.tkraise() else: if request == REG_LOGIN_FAIL: logged_in_error.grid_forget() server_error.grid(row=4, column=0, columnspan=2) elif request == USER_LOGGED_IN: server_error.grid_forget() logged_in_error.grid(row=4, column=0, columnspan=2) else: length_error["fg"] = "red"
def leave_group(): main_root.title("sendme") user_values.pin_code = 0 user_values.chat_name = "" root_menu_bar.delete(tkinter.END) msg = f"{user_values.username} is leaving this chat" packets = protocol.PacketMaker(SEND_MSG, shared_secret=user_values.group_key, content=msg.encode("utf-8")) for packet in packets: server.send(packet) packets = protocol.PacketMaker(LEAVE_CHAT) server.send(next(packets)) chat_entry.delete(0, tkinter.END) list_box.delete(0, tkinter.END) user_values.on_raise_chat_picker() chat_picker_frame.tkraise()
def run(self): while self.user_values.pin_code or self.user_values.chat_name: if self.request_queue: if self.request_queue[0][0] == GET_GROUP_KEY: client_public_key = RSA.import_key( self.request_queue[0][1].strip(b"\x00")) content = AES_crypto.rsa_encrypt( self.user_values.rsa_group_key.export_key(), client_public_key) content += b"end_private_key" encrypted_group_key = int_to_bytes( pow(bytes_to_int(self.user_values.group_key), self.user_values.rsa_group_key.e, self.user_values.rsa_group_key.n)) content += encrypted_group_key packets = protocol.PacketMaker(SEND_GROUP_KEYS, content=content) for packet in packets: self.server.send(packet) del self.request_queue[0] elif self.request_queue[0][0] in [SEND_FILE, SEND_IMG]: file_content = protocol_digest.decrypt( self.request_queue[0][1][1], self.user_values.group_key) file_name = protocol_digest.decrypt( self.request_queue[0][1][0], self.user_values.group_key) with open(".\\files\\" + file_name.decode("utf-8"), "wb") as file_: file_.write(file_content) del self.request_queue[0] elif self.request_queue[0][0] == GET_GROUP_INFO: top = tkinter.Toplevel() self.top = top top.title("group info") top.minsize(250, 250) group_users = decrypt(self.request_queue[0][1], self.key).decode("utf-8") content = f"{self.user_values.chat_name}\n" if self.user_values.pin_code: pyperclip.copy(self.user_values.pin_code) content += f"\nGroup code - {self.user_values.pin_code}\n" content += "\nusers\n" content += group_users about_label = tkinter.Label(top, text=content) about_label.pack() del self.request_queue[0] else: print("no requestes") print("request - " + bytes_to_requst[self.request_queue[0]]) time.sleep(0.05)
def on_submit(*args): date_error.grid_forget() email_error.grid_forget() re_password_error.grid_forget() username_error.grid_forget() password_error.grid_forget() server_error.grid_forget() username = username_entry.get() password = password_entry.get() re_password = re_password_entry.get() birthday = birthday_picker.get_date() today = datetime.date.today() email = email_entry.get() send = True if birthday > today: send = False date_error.grid(row=6, column=0, columnspan=2) if not is_valid_email(email): send = False email_error.grid(row=8, column=0, columnspan=2) if password != re_password: send = False re_password_error.grid(row=4, column=0, columnspan=2) if len(username) < USERNAME_MIN_LEN or len( username) > USERNAME_MAX_LEN: send = False username_error.grid(row=10, column=0, columnspan=2) if len(password) < PASSWORD_MIN_LEN or len( password) > PASSWORD_MAX_LEN: send = False password_error.grid(row=9, column=0, columnspan=2) if send: content = to_json(username.encode("utf-8"), hash_password(password).encode("utf-8"), email.encode("utf-8"), bytes([birthday.day]), bytes([birthday.month]), int_to_bytes(birthday.year)) packets = protocol.PacketMaker(REGISTER, key, content=content) for packet in packets: server.send(packet) server_response = server.recv(PACKET_SIZE) can_auth, reason = protocol_digest.can_auth_email(server_response) if can_auth: user_values.username = username email_validate_frame.tkraise() else: server_error_var.set(error_msgs[reason]) server_error.grid(row=11, column=0, columnspan=2)
def on_raise(): main_root.title(f"sendme - {user_values.chat_name}") file_menu = tkinter.Menu(menu_bar, tearoff=0) file_menu.add_command(label="grope info", command=group_info) file_menu.add_separator() file_menu.add_command(label="leave grope", command=leave_group) menu_bar.add_cascade(label="options", menu=file_menu) msg = f"{user_values.username} has entered the chat".encode("utf-8") list_box.insert(tkinter.END, msg) user_values.process_thread = ProcessPackets(server, user_values, key) user_values.process_thread.start() chat_frame.after(100, msg_listener) if user_values.group_key: packets = protocol.PacketMaker(SEND_MSG, shared_secret=user_values.group_key, content=msg) for packet in packets: server.send(packet)
def on_send(*args): msg = chat_entry.get() if not msg: return if user_values.is_safe_chat: if is_swear(msg): win32api.MessageBox(0, "you can't swear in hear!", 'warning', 0x00001000) return msg = f"<{user_values.username}>:" + msg list_box.insert(tkinter.END, msg) packets = protocol.PacketMaker(SEND_MSG, shared_secret=user_values.group_key, content=msg.encode("utf-8")) for packet in packets: server.send(packet) chat_entry.delete(0, "end")
def join_chat(*args): continue_listening.clear() pin_error.grid_forget() pin_code = pin_code_entry.get() if not pin_code: pin_error.grid(row=3, column=0, sticky="N") else: rsa_key = RSA.generate(2048) content = { "chat_name": value, "pin_code": pin_code, "rsa_key": rsa_key.export_key("PEM").decode("utf-8") } content = json.dumps(content).encode("utf-8") packets = protocol.PacketMaker(JOIN_CHAT, shared_secret=key, content=content) for packet in packets: server.send(packet) request, server_response = get_server_response(server) if request == SEND_GROUP_KEYS: group_private_key, group_dh_key = server_response.strip( b'\x00').split(b"end_private_key") user_values.rsa_group_key = AES_crypto.rsa_decrypt( group_private_key, rsa_key) group_private_key = RSA.import_key( user_values.rsa_group_key) group_dh_key = pow(bytes_to_int(group_dh_key), group_private_key.d, group_private_key.n) user_values.group_key = int_to_bytes(group_dh_key) top.withdraw() pin_code_entry.delete(0, "end") user_values.pin_code = pin_code user_values.chat_name = value stop_listening.set() chat_frame.tkraise() user_values.on_chat_raise() elif request == CANT_JOIN_CHAT: pin_error.grid(row=3, column=0, sticky="N")
def open_new_public_chat(): continue_listening.clear() swear_protected = is_swear_protected.get() time.sleep(0.5) rsa_key = RSA.generate(2048) if swear_protected == 0: content = rsa_key.public_key().export_key() + b'\x00' elif swear_protected == 1: content = rsa_key.public_key().export_key() + b'\x01' packets = protocol.PacketMaker(CREATE_PUBLIC_CHAT, content=content, shared_secret=key) for packet in packets: server.send(packet) packet = server.recv(PACKET_SIZE) msg = b"" request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) msg += packet[HEADER_SIZE:] if packet_amount - packet_number > 1: for _ in range(packet_amount - 1): packet = server.recv(PACKET_SIZE) msg += packet[HEADER_SIZE:] user_values.group_key = AES_crypto.rsa_decrypt(msg, rsa_key) user_values.chat_name = user_values.username + "'s public chat" if swear_protected: user_values.chat_name += " (swear protected)" user_values.is_safe_chat = True else: user_values.is_safe_chat = False user_values.on_chat_raise() stop_listening.set() chat_frame.tkraise()
def msg_listener(*args): if user_values.pin_code or user_values.chat_name: server.settimeout(0.04) try: packet = server.recv(PACKET_SIZE) server.settimeout(None) msg_queue = [] request, request_id, packet_amount, packet_number, flag = protocol_digest.buffer_extractor( packet[:HEADER_SIZE]) msg_queue.append(packet) server.settimeout(1) if packet_amount - packet_number > 1: for _ in range(packet_amount - 1): packet = server.recv(PACKET_SIZE) msg_queue.append(packet) server.settimeout(None) # need to check packet validity if packet_amount - len(msg_queue): print("f**k, something went wrong") # not every packet was sent for some reason s = list(range(packet_amount)) for packet in msg_queue: request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) s.remove(packet_number) print(s) content = request_id + b"," + " ".join(s).encode("utf-8") packets = protocol.PacketMaker(RESEND_PACKETS, content=content, shared_secret=key) for packet in packets: print(packet) server.send(packet) packet = server.recv(PACKET_SIZE) request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) msg_queue.insert(packet_number, packet) if packet_amount > 1: for _ in range(packet_amount - packet_number - 1): packet = self.client.recv(PACKET_SIZE) self.queue_requests.insert(packet_number, packet) if request == SEND_MSG: msg = b'' for packet in msg_queue: msg += packet[HEADER_SIZE:] msg = decrypt(msg, user_values.group_key) list_box.insert(tkinter.END, msg.decode("utf-8")) elif request in [SEND_FILE, SEND_IMG]: print(packet_amount - len(msg_queue)) username = b'' file_name = b'' file_content = b'' for packet in msg_queue: request, request_id, packet_amount, packet_number, flag = protocol_digest.buffer_extractor( packet[:HEADER_SIZE]) if flag == FILE_NAME_PACKET: file_name += packet[HEADER_SIZE:] elif flag == CONTENT_PACKET: file_content += packet[HEADER_SIZE:] elif flag == USERNAME_PACKET: username += packet[HEADER_SIZE:] user_values.process_thread.add_request( [request, [file_name, file_content]]) username = decrypt(username, user_values.group_key) list_box.insert(tkinter.END, username.decode("utf-8") + " sent a file") file_path = os.getcwd() + "\\files" clickable_links[username.decode("utf-8") + " sent a file"] = file_path elif request == SENDING_COMPLITED: msg = b'' for packet in msg_queue: msg += packet[HEADER_SIZE:] this_request_id = decrypt(msg, key) del file_packet_list[this_request_id] elif request == RESEND_PACKETS: msg = b'' for packet in msg_queue: msg += packet[HEADER_SIZE:] this_request_id, item_list = decrypt(msg, key) item_list = item_list.split(" ") for number in item_list: packet = file_packet_list[this_request_id].index( number) server.send(packet) elif request == GET_GROUP_INFO: msg = b'' for packet in msg_queue: msg += packet[HEADER_SIZE:] user_values.process_thread.add_request([request, msg]) else: msg = b'' for packet in msg_queue: msg += packet[HEADER_SIZE:] user_values.process_thread.add_request([request, msg]) except Exception as e: if type(e) != socket.timeout: raise e else: server.settimeout(None) finally: chat_frame.after(300, msg_listener)
def group_info(): packets = protocol.PacketMaker(GET_GROUP_INFO) server.send(next(packets))
def on_select(event): if not continue_listening.isSet(): return continue_listening.clear() courser = chat_listbox.curselection() if not courser: return value = chat_listbox.get(courser) if value[-11:] == "(safe chat)": user_values.is_safe_chat = True else: user_values.is_safe_chat = False if chat_types[value] == "private": top = tkinter.Toplevel() def join_chat(*args): continue_listening.clear() pin_error.grid_forget() pin_code = pin_code_entry.get() if not pin_code: pin_error.grid(row=3, column=0, sticky="N") else: rsa_key = RSA.generate(2048) content = { "chat_name": value, "pin_code": pin_code, "rsa_key": rsa_key.export_key("PEM").decode("utf-8") } content = json.dumps(content).encode("utf-8") packets = protocol.PacketMaker(JOIN_CHAT, shared_secret=key, content=content) for packet in packets: server.send(packet) request, server_response = get_server_response(server) if request == SEND_GROUP_KEYS: group_private_key, group_dh_key = server_response.strip( b'\x00').split(b"end_private_key") user_values.rsa_group_key = AES_crypto.rsa_decrypt( group_private_key, rsa_key) group_private_key = RSA.import_key( user_values.rsa_group_key) group_dh_key = pow(bytes_to_int(group_dh_key), group_private_key.d, group_private_key.n) user_values.group_key = int_to_bytes(group_dh_key) top.withdraw() pin_code_entry.delete(0, "end") user_values.pin_code = pin_code user_values.chat_name = value stop_listening.set() chat_frame.tkraise() user_values.on_chat_raise() elif request == CANT_JOIN_CHAT: pin_error.grid(row=3, column=0, sticky="N") top.title("password inserter") tkinter.Label(top, text=f"want to join {value}?\nenter password", font=15).grid(row=1, column=0, sticky="S") pin_code_entry = tkinter.Entry(top, text="enter password", font=15) pin_code_entry.grid(row=2, column=0, pady=20, sticky="NEWS") pin_error = tkinter.Label( top, text="the pin-code you entered is incorrect", fg="red") tkinter.Button(top, text="join chat", font=15, command=join_chat).grid(row=4, column=0, sticky="N") else: stop_listening.set() rsa_key = RSA.generate(2048) content = dict(chat_name=value, rsa_key=rsa_key.export_key("PEM").decode("utf-8")) content = json.dumps(content).encode("utf-8") packets = protocol.PacketMaker(JOIN_PASSWORD_LESS_CHAT, content=content, shared_secret=key) for packet in packets: server.send(packet) packet = server.recv(PACKET_SIZE) request, request_id, packet_amount, packet_number, flag = buffer_extractor( packet[:HEADER_SIZE]) full_msg = packet[HEADER_SIZE:] if packet_amount > 1: for _ in range(packet_amount - 1): packet = server.recv(PACKET_SIZE) full_msg += packet[HEADER_SIZE:] user_values.group_key = AES_crypto.rsa_decrypt(full_msg, rsa_key) user_values.chat_name = value chat_frame.tkraise() user_values.on_chat_raise() stop_listening.set()