Esempio n. 1
0
    def parse_recv_message(self, msg):
        """ Method parse/decrypt message """

        msg = Msg.from_formated(msg)
        encryptor = self.get_encryptor(msg.sender)
        msg.text = encryptor.decrypt_msg(msg.text.encode()).decode()
        return msg.sender, msg.text
Esempio n. 2
0
    def accepting_chat(self, resp_mes):
        """ Method of handle request to start chat """

        r_msg = Msg.from_formated(resp_mes)
        pub = import_pub_key(r_msg.text.encode())

        key = self.storage.get_key(r_msg.sender)
        if key is not None:
            encryptor = ClientCrypt(key)
        else:
            encryptor = ClientCrypt.gen_secret(self.username, r_msg.sender)
            self.storage.add_chat_key(r_msg.sender, encryptor.secret)
        self.set_encryptor(r_msg.sender, encryptor)
        enc_key = encrypt_rsa(pub, encryptor.secret)
        msg = Msg(enc_key.decode(), self.username, r_msg.sender)
        accept_req = Request(RequestAction.ACCEPT_CHAT, msg)
        self.__send_request(accept_req)
Esempio n. 3
0
    def send_msg(self, text, to):
        """ Method send messge to server """

        encryptor = self.get_encryptor(to)
        text = encryptor.encript_msg(text.encode()).decode()
        msg = Msg(text, self.user, to)
        self.storage.add_message(msg.to, msg.text)
        request = Request(RequestAction.MESSAGE, msg)
        self.__send_request(request)
    def __req_accept_chat_handler(self, i_req, client, *args):
        """ Mathod the handler accept chat request """

        msg = Msg.from_dict(i_req.body)
        if msg.to not in self.users:
            self.logger.warning(f'{msg.to} not found')
            return
        self.__send_to_client(self.users[msg.to],
                              Response(ACCEPT_CHAT, str(msg)))
Esempio n. 5
0
    def parse_recv_message(self, msg):
        """ Method parse/decrypt message """

        msg = Msg.from_formated(msg)
        if msg.to != '@ALL':
            encryptor = self.get_encryptor(msg.sender)
            msg.text = encryptor.decrypt_msg(msg.text.encode()).decode()
        else:
            msg.text = b64decode(msg.text).decode()
        return msg.sender, msg.text, msg.to
Esempio n. 6
0
    def accepted_chat(self, resp_mes):
        """ Method of handle response about confirm start of chat """

        msg = Msg.from_formated(resp_mes)
        encryptor = self.get_encryptor(msg.sender)
        if encryptor is not None:
            return
        secret = decrypt_rsa(self.priv_key, msg.text.encode())
        self.set_encryptor(msg.sender, ClientCrypt(secret))
        self.storage.add_chat_key(msg.sender, secret)
Esempio n. 7
0
    def start_chat(self, contact):
        """ Method initialization of chat with contact """

        key = self.storage.get_key(contact)
        if key is not None:
            self.set_encryptor(contact, ClientCrypt(key))

        prv, pub = gen_keys()
        self.priv_key = prv
        msg = Msg(pub.export_key().decode(), self.username, contact)
        start_req = Request(RequestAction.START_CHAT, msg)
        self.__send_request(start_req)
    def __req_message_handler(self, i_req, client, *args):
        """ Mathod the handler message request """

        other_clients = args[0]
        msg = Msg.from_dict(i_req.body)
        self.storage.user_stat_update(msg.sender, ch_sent=1)
        if msg.to.upper() != 'ALL' and msg.to in self.users:
            self.storage.user_stat_update(msg.to, ch_recv=1)
            self.storage.add_message(msg.sender, msg.to, msg.text)
            self.__send_to_client(self.users[msg.to],
                                  Response(LETTER, str(msg)))
        else:
            self.__send_to_all(other_clients, Response(LETTER, str(msg)))
            for u in self.storage.get_users_online():
                if str(u) == msg.sender:
                    continue
                self.storage.user_stat_update(str(u), ch_recv=1)
                self.storage.add_message(msg.sender, str(u), msg.text)
    def __req_message_handler(self, i_req, client, *args):
        """ Mathod the handler message request """

        other_clients = args[0]
        msg = Msg.from_dict(i_req.body)
        self.storage.user_stat_update(msg.sender, ch_sent=1)
        if msg.to.upper() != '@ALL' and msg.to in self.users:
            self.storage.user_stat_update(msg.to, ch_recv=1)
            self.storage.add_message(msg.sender, msg.to, msg.text)
            self.__send_to_client(self.users[msg.to],
                                  Response(LETTER, str(msg)))
        else:
            text = b64decode(msg.text).decode().lower()
            spam_filter = [s for s in self.SPAM if s in text]
            if len(spam_filter) > 0:
                self.logger.warning('SPAM DETECT')
                return
            self.storage.add_message_to_room(msg.to.upper()[1:], msg.text,
                                             msg.sender)

            self.__send_to_all(other_clients, Response(LETTER, str(msg)))
Esempio n. 10
0
    def __send_responses(self, requests, o_clients):

        for client, i_req in requests.items():
            other_clients = [c for c in o_clients if c != client]
            self.logger.info(client)
            self.logger.info(i_req)

            if i_req.action == RequestAction.PRESENCE:
                self.__send_to_client(client, Response(OK))
                self.__send_to_all(other_clients,
                                   Response(CONNECTED, i_req.body))
            elif i_req.action == RequestAction.QUIT:
                self.__client_disconnect(client)
            elif i_req.action == RequestAction.MESSAGE:
                msg = Msg.from_dict(i_req.body)
                self.storage.user_stat_update(msg.sender, ch_sent=1)
                if msg.to.upper() != 'ALL' and msg.to in self.users:
                    self.storage.user_stat_update(msg.to, ch_recv=1)
                    self.storage.add_message(msg.sender, msg.to, msg.text)
                    self.__send_to_client(self.users[msg.to],
                                          Response(LETTER, str(msg)))
                else:
                    self.__send_to_all(other_clients,
                                       Response(LETTER, str(msg)))
                    for u in self.storage.get_users_online():
                        if str(u) == msg.sender:
                            continue
                        self.storage.user_stat_update(str(u), ch_recv=1)
                        self.storage.add_message(msg.sender, str(u), msg.text)

            elif i_req.action == RequestAction.COMMAND:
                command, *args = i_req.body.split()
                user = [u for u, c in self.users.items() if c == client].pop()
                if len(args) < 1 or args[0] != user:
                    args.insert(0, user)
                o_resp = self.__execute_command(command, *args)
                self.__send_to_client(client, o_resp)
            else:
                self.__send_to_client(client, Response(INCORRECT_REQUEST))
                self.logger.error(f'Incorrect request:\n {i_req}')
    def __send_responses(self, requests, o_clients):

        for client, i_req in requests.items():
            other_clients = [c for c in o_clients if c != client]
            self.logger.info(client)
            self.logger.info(i_req)

            if i_req.action == RequestAction.PRESENCE:
                prv, pub = gen_keys()
                self.client_keys[i_req.body] = (client, prv)
                self.__send_to_client(client, Response(AUTH, pub.export_key().decode()))
            elif i_req.action == RequestAction.AUTH:
                user = [u for u, c in self.users.items() if c == client]
                if len(user) == 0:
                    self.logger.warning(f'AUTH: user not found')
                    continue
                user = user[0]
                cl, key = self.client_keys[user]
                if cl != client:
                    self.logger.warning('AUTH: connection sockets not equals')
                    continue
                try:
                    password = decrypt_password(key, i_req.body)
                except Exception as e:
                    self.logger.error(e)
                    password = None
                if password is None:
                    self.logger.warning('AUTH: decrypt error')
                    continue
                if not self.storage.authorization_user(user, get_hash_password(password, user.encode())):
                    self.__send_to_client(client, Response(UNAUTHORIZED))
                    self.clients.remove(client)
                    self.users.pop(user)
                    continue
                self.storage.login_user(user, client.getpeername()[0])
                self.__send_to_client(client, Response(OK))
                self.__send_to_all(other_clients, Response(CONNECTED, user))
                pass
            elif i_req.action == RequestAction.QUIT:
                self.__client_disconnect(client)
            elif i_req.action == RequestAction.START_CHAT:
                msg = Msg.from_dict(i_req.body)
                if msg.to not in self.users:
                    self.logger.warning(f'{msg.to} not found')
                    continue
                self.__send_to_client(self.users[msg.to], Response(START_CHAT, str(msg)))
            elif i_req.action == RequestAction.ACCEPT_CHAT:
                msg = Msg.from_dict(i_req.body)
                if msg.to not in self.users:
                    self.logger.warning(f'{msg.to} not found')
                    continue
                self.__send_to_client(self.users[msg.to], Response(ACCEPT_CHAT, str(msg)))
            elif i_req.action == RequestAction.MESSAGE:
                msg = Msg.from_dict(i_req.body)
                self.storage.user_stat_update(msg.sender, ch_sent=1)
                if msg.to.upper() != 'ALL' and msg.to in self.users:
                    self.storage.user_stat_update(msg.to, ch_recv=1)
                    self.storage.add_message(msg.sender, msg.to, msg.text)
                    self.__send_to_client(self.users[msg.to], Response(LETTER, str(msg)))
                else:
                    self.__send_to_all(other_clients, Response(LETTER, str(msg)))
                    for u in self.storage.get_users_online():
                        if str(u) == msg.sender:
                            continue
                        self.storage.user_stat_update(str(u), ch_recv=1)
                        self.storage.add_message(msg.sender, str(u), msg.text)
            elif i_req.action == RequestAction.COMMAND:
                command, *args = i_req.body.split()
                user = [u for u, c in self.users.items() if c == client].pop()
                if len(args) < 1 or args[0] != user:
                    args.insert(0, user)
                o_resp = self.__execute_command(command, *args)
                self.__send_to_client(client, o_resp)
            else:
                self.__send_to_client(client, Response(INCORRECT_REQUEST))
                self.logger.error(f'Incorrect request:\n {i_req}')