async def _on_connect(reader: asyncio.StreamReader, writer: asyncio.StreamWriter): timeout = 5 client_login = None client_endpoint = writer.get_extra_info("peername") client_ip = client_endpoint[0] client_port = client_endpoint[1] Logger.log(f"Accepted connection from {client_ip}:{client_port}.", "client") while True: try: code, data = await self._get_data(reader, timeout) if code == 0: # Ping await self._send_data(writer, 0) Logger.log(f"Ping was sent to {client_ip}:{client_port}.", "client") elif code == 2: # Login received_login, data = Extentions.bytes_to_defstr(data) if len(data) == 0: preferred_port = "3502" else: preferred_port, _ = Extentions.bytes_to_defstr(data) if received_login == "server": await self._send_data(writer, 252, Extentions.defstr_to_bytes("'server' is service login!")) continue client_login = received_login self.update_ip_callback(client_login, client_ip + ":" + preferred_port) await self._send_data(writer, 2, Extentions.defstr_to_bytes(self.login)) timeout = 60 Logger.log(f"Login {client_ip}:{client_port} as '{client_login}' was confirmed.") elif code == 3: # IP updating request login_count, data = Extentions.bytes_to_int(data) ips = Extentions.int_to_bytes(login_count) while login_count > 0: requested_login, data = Extentions.bytes_to_defstr(data) if requested_login == "server": if self._server_endpoint == []: requested_ip = "" requested_time = "" else: requested_ip, requested_time = self._server_endpoint requested_time = requested_time.strftime("%H:%M:%S.%f %d.%m.%Y") requested_line = (Extentions.defstr_to_bytes(requested_ip) + Extentions.defstr_to_bytes(requested_time)) ips += requested_line elif requested_login == self.login: requested_ip = socket.gethostbyname(socket.gethostname()) requested_time = datetime.now().strftime("%H:%M:%S.%f %d.%m.%Y") requested_line = (Extentions.defstr_to_bytes(requested_ip) + Extentions.defstr_to_bytes(requested_time)) ips += requested_line else: tmp = self._database.search_ip_and_last_time(requested_login) if tmp[0] == "0.0.0.0": requested_ip = "" requested_time = "" else: requested_ip, requested_time = tmp requested_time = requested_time.strftime("%H:%M:%S.%f %d.%m.%Y") requested_line = (Extentions.defstr_to_bytes(requested_ip) + Extentions.defstr_to_bytes(requested_time)) ips += requested_line login_count -= 1 await self._send_data(writer, 3, ips) Logger.log(f"Requested IPs was sent to {client_ip}:{client_port}.") elif code == 5: # Text message if client_login is not None and client_login != "guest": self.on_receive_msg_callback(data, client_login, client_ip + ":" + preferred_port) await self._send_data(writer, 5) Logger.log(f"Message from '{client_login}' ({client_ip}:{client_port}) was recieved.") else: await self._send_data(writer, 253, Extentions.defstr_to_bytes("You should login first.")) elif code == 6: # File message pass # TODO: Add handling file messages else: msg, _ = Extentions.bytes_to_str(Extentions.int_to_bytes(code) + data) Logger.log(f"Following message was resieved from {client_ip}:{client_port}:\n{msg}", "client") except ConnectionResetError: Logger.log(f"Connection from {client_ip}:{client_port} closed by peer.", "client") break except TimeoutError: Logger.log(f"Connection from {client_ip}:{client_port} closed by timeout.", "client") break writer.close()
async def _on_connect(reader: asyncio.StreamReader, writer: asyncio.StreamWriter): timeout = 5 login = None client_endpoint = writer.get_extra_info("peername") client_ip = client_endpoint[0] if client_ip == "127.0.0.1": client_ip = socket.gethostbyname(socket.gethostname()) client_port = client_endpoint[1] Logger.log(f"Accepted connection from {client_ip}:{client_port}.") while True: try: code, data = await _get_data(reader, timeout) if code == 0: # Ping await _send_data(writer, 0) Logger.log(f"Ping was sent to {client_ip}:{client_port}.") elif code == 1: # Registration received_login, data = Extentions.bytes_to_defstr(data) if received_login == "server" or received_login == "guest": await _send_data( writer, 252, Extentions.defstr_to_bytes( "'server' is service login!")) continue login = received_login pass_hash, data = Extentions.bytes_to_defstr(data) if data == bytes(): preferred_port = "3502" else: preferred_port, _ = Extentions.bytes_to_defstr(data) Logger.log( f"Registration {client_ip}:{client_port} as '{login}'...") if (_database.search_ip_and_last_time(login)[0] != "0.0.0.0"): await _send_data( writer, 254, Extentions.defstr_to_bytes( "This login is already registered.")) Logger.log( f"Registration {client_ip}:{client_port} as '{login}' was refused." ) continue _database.add_client(login, pass_hash, client_ip + ":" + preferred_port) pass_hash = None await _send_data(writer, 1) Logger.log( f"Registration {client_ip}:{client_port} as '{login}' was confirmed." ) timeout = 60 elif code == 2: # Login received_login, data = Extentions.bytes_to_defstr(data) if received_login == "server" or received_login == "guest": await _send_data( writer, 252, Extentions.defstr_to_bytes( "'server' is service login!")) continue login = received_login pass_hash, data = Extentions.bytes_to_defstr(data) if data == bytes(): preferred_port = "3502" else: preferred_port, _ = Extentions.bytes_to_defstr(data) Logger.log(f"Login {client_ip}:{client_port} as '{login}'...") if (_database.search_password(login) != pass_hash): await _send_data( writer, 255, Extentions.defstr_to_bytes("Password is incorrect.")) Logger.log( f"Login {client_ip}:{client_port} as '{login}' was refused." ) continue pass_hash = "" await _send_data(writer, 2) Logger.log( f"Login {client_ip}:{client_port} as '{login}' was confirmed." ) timeout = 60 _database.update_ip(login, client_ip + ":" + preferred_port) elif code == 3: # IP updating request Logger.log(f"IPs was requested by {client_ip}:{client_port}") login_count, data = Extentions.bytes_to_int(data) ips = Extentions.int_to_bytes(login_count) while login_count > 0: requested_login, data = Extentions.bytes_to_defstr(data) tmp = _database.search_ip_and_last_time(requested_login) if tmp[0] == "0.0.0.0": requested_ip = "" requested_time = "" else: requested_ip, requested_time = tmp requested_time = requested_time.strftime( "%H:%M:%S.%f %d.%m.%Y") requested_line = ( Extentions.defstr_to_bytes(requested_ip) + Extentions.defstr_to_bytes(requested_time)) ips += requested_line login_count -= 1 await _send_data(writer, 3, ips) Logger.log( f"Requested IPs was sent to {client_ip}:{client_port}.") elif code == 4: # Delete user request if login is None: await _send_data( writer, 253, Extentions.defstr_to_bytes("You should login first.")) else: pass_hash, _ = Extentions.bytes_to_defstr(data) Logger.log( f"Deleting of '{login}' was requested by {client_ip}:{client_port}..." ) if (_database.search_password(login) != pass_hash): await _send_data( writer, 255, Extentions.defstr_to_bytes( "Password is incorrect.")) Logger.log( f"Deleting of '{login}' requested by {client_ip}:{client_port} was refused." ) continue _database.del_client(login) Logger.log( f"Deleting of '{login}' requested by {client_ip}:{client_port} was confirmed." ) await _send_data(writer, 4) timeout = 5 login = "" pass_hash = "" else: msg, _ = Extentions.bytes_to_str( Extentions.int_to_bytes(code) + data) Logger.log( f"Following message was resieved from {client_ip}:{client_port}:\n{msg}" ) except ConnectionResetError: Logger.log( f"Connection from {client_ip}:{client_port} closed by peer.") break except TimeoutError: Logger.log( f"Connection from {client_ip}:{client_port} closed by timeout." ) break writer.close()