Exemple #1
0
    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)
Exemple #2
0
    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 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 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)
Exemple #5
0
def extract_chat_id(packet, private_key):
    # request, request_id, packet_amount, packet_number, flag = buffer_extractor(packet[:HEADER_SIZE])
    chat_id = packet[HEADER_SIZE:].strip(b'\x00')
    chat_id = decrypt(chat_id, private_key)
    return chat_id