Working on 1.1.22
This commit is contained in:
parent
316a0e3e1e
commit
6a044ecaf8
|
|
@ -58,3 +58,11 @@ How does encryption work?
|
||||||
|
|
||||||
* Sometime WS just drop connection
|
* Sometime WS just drop connection
|
||||||
* Client input message problem. To start input, you need to press enter first, only after that you got pop up with message. Tried to fix, but nothing worked.
|
* Client input message problem. To start input, you need to press enter first, only after that you got pop up with message. Tried to fix, but nothing worked.
|
||||||
|
|
||||||
|
# 1.1.22
|
||||||
|
|
||||||
|
- Renderer logics have been separated
|
||||||
|
- A new renderer have been implemented
|
||||||
|
- Thread closing logics have been changed. Now, it is easy to quit from the client without any unexpected behavior
|
||||||
|
- Now, displaying messages is limited to the last N messages. The default value is 5
|
||||||
|
- WS dropped connection, probably fixed
|
||||||
|
|
@ -1,25 +1,24 @@
|
||||||
import os
|
|
||||||
import ast
|
import ast
|
||||||
import time
|
import time
|
||||||
import platform
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
from colorama import init
|
|
||||||
from websocket import create_connection
|
from websocket import create_connection
|
||||||
|
|
||||||
from cmd_chat.client.core.crypto import RSAService
|
from cmd_chat.client.core.crypto import RSAService
|
||||||
from cmd_chat.client.config import (
|
from cmd_chat.client.core.default_renderer import DefaultClientRenderer
|
||||||
COLORS,
|
from cmd_chat.client.core.rich_renderer import RichClientRenderer
|
||||||
RENDER_TIME
|
|
||||||
)
|
from cmd_chat.client.config import RENDER_TIME
|
||||||
|
|
||||||
|
|
||||||
init()
|
class Client(RSAService, RichClientRenderer):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
class Client(RSAService):
|
self,
|
||||||
|
server: str,
|
||||||
def __init__(self, server: str, port: int, username: str):
|
port: int,
|
||||||
|
username: str
|
||||||
|
):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
# Server info
|
# Server info
|
||||||
self.server = server
|
self.server = server
|
||||||
|
|
@ -35,77 +34,34 @@ class Client(RSAService):
|
||||||
"action": "close",
|
"action": "close",
|
||||||
"username": self.username
|
"username": self.username
|
||||||
})
|
})
|
||||||
|
# Threads
|
||||||
def __get_os(self) -> str:
|
self.__stop_threads = False
|
||||||
""" checking what kind of platform you need
|
|
||||||
"""
|
|
||||||
if "Linux" in str(platform.platform()):
|
|
||||||
return "Linux"
|
|
||||||
return "Windows"
|
|
||||||
|
|
||||||
def send_info(self):
|
def send_info(self):
|
||||||
""" sending message to websocket
|
""" sending message to websocket
|
||||||
"""
|
"""
|
||||||
ws = create_connection(f"{self.ws_url}/talk")
|
ws = create_connection(f"{self.ws_url}/talk")
|
||||||
while True:
|
while not self.__stop_threads:
|
||||||
try:
|
try:
|
||||||
user_input = input("You're message: ")
|
user_input = input("You're message: ")
|
||||||
|
if user_input == "q":
|
||||||
|
self.__stop_threads = True
|
||||||
message = f'{self.username}: {user_input}'
|
message = f'{self.username}: {user_input}'
|
||||||
socket_message = str({
|
socket_message = str({
|
||||||
"text": self._encrypt(message),
|
"text": self._encrypt(message),
|
||||||
"username": self.username
|
"username": self.username
|
||||||
})
|
})
|
||||||
ws.send(
|
ws.send(payload=socket_message.encode())
|
||||||
payload=socket_message.encode()
|
|
||||||
)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
ws.send(self.close_response)
|
ws.send(self.close_response)
|
||||||
ws.close()
|
ws.close()
|
||||||
quit()
|
self.__stop_threads = True
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
ws.send(self.close_response)
|
ws.send(self.close_response)
|
||||||
ws.close()
|
ws.close()
|
||||||
print("Something went wrong! ", exc)
|
print("Something went wrong! ", exc)
|
||||||
quit()
|
self.__stop_threads = True
|
||||||
|
raise exc
|
||||||
def __print_message(self, message: str) -> str:
|
|
||||||
""" generating string with message in required format
|
|
||||||
"""
|
|
||||||
message = message.split(":")
|
|
||||||
if message[0] == self.username:
|
|
||||||
return COLORS["my_username_color"] + message[0] + ": " + message[1] + COLORS["text_color"]
|
|
||||||
return message[0] + ": " + message[1] + COLORS["text_color"]
|
|
||||||
|
|
||||||
def __clear_console(self):
|
|
||||||
# For windows clear command its cls
|
|
||||||
# For linux clear command its clear
|
|
||||||
if self.__get_os() == "Linux":
|
|
||||||
os.system("clear")
|
|
||||||
else:
|
|
||||||
os.system("cls")
|
|
||||||
|
|
||||||
def __print_ip(
|
|
||||||
self,
|
|
||||||
ip: str
|
|
||||||
) -> str:
|
|
||||||
return f"IP: " + COLORS["ip_color"] + ip + COLORS["text_color"]
|
|
||||||
|
|
||||||
def __print_username(
|
|
||||||
self,
|
|
||||||
username: str
|
|
||||||
) -> str:
|
|
||||||
return f"USERNAME: " + COLORS["ip_color"] + username + COLORS["username_color"]
|
|
||||||
|
|
||||||
def __print_chat(self, response: list[str]) -> str:
|
|
||||||
for i, msg in enumerate(response["messages"]):
|
|
||||||
actual_message = self._decrypt(msg)
|
|
||||||
if i == 0:
|
|
||||||
for user in response["users_in_chat"]:
|
|
||||||
print(self.__print_ip(user.split(",")[0]))
|
|
||||||
print(self.__print_username(user.split(",")[1]))
|
|
||||||
print(f"\n{self.__print_message(actual_message)}")
|
|
||||||
else:
|
|
||||||
print(f"{self.__print_message(actual_message)}")
|
|
||||||
|
|
||||||
def update_info(self):
|
def update_info(self):
|
||||||
""" connecting to websocket,
|
""" connecting to websocket,
|
||||||
|
|
@ -114,27 +70,30 @@ class Client(RSAService):
|
||||||
"""
|
"""
|
||||||
ws = create_connection(f"{self.ws_url}/update")
|
ws = create_connection(f"{self.ws_url}/update")
|
||||||
last_try = None
|
last_try = None
|
||||||
while True:
|
while not self.__stop_threads:
|
||||||
try:
|
try:
|
||||||
time.sleep(RENDER_TIME)
|
time.sleep(RENDER_TIME)
|
||||||
response = ast.literal_eval(ws.recv().decode('utf-8'))
|
response = ast.literal_eval(ws.recv().decode('utf-8'))
|
||||||
if last_try == response:
|
if last_try == response:
|
||||||
continue
|
continue
|
||||||
last_try = response
|
last_try = response
|
||||||
self.__clear_console()
|
self.clear_console()
|
||||||
if len(last_try["messages"]) > 0:
|
if len(last_try["messages"]) > 0:
|
||||||
self.__print_chat(
|
self.print_chat(response = last_try)
|
||||||
response = last_try
|
|
||||||
)
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
ws.send(self.close_response)
|
ws.send(self.close_response)
|
||||||
ws.close()
|
ws.close()
|
||||||
quit()
|
self.__stop_threads = True
|
||||||
|
except ConnectionAbortedError:
|
||||||
|
# Reconnect if somehow client was disconnected
|
||||||
|
ws = create_connection(f"{self.ws_url}/update")
|
||||||
|
continue
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
ws.send(self.close_response)
|
ws.send(self.close_response)
|
||||||
ws.close()
|
ws.close()
|
||||||
print("Something went wrong! ", exc)
|
print("Something went wrong! ", exc)
|
||||||
quit()
|
self.__stop_threads = True
|
||||||
|
raise exc
|
||||||
|
|
||||||
def _validate_keys(self) -> None:
|
def _validate_keys(self) -> None:
|
||||||
self._request_key(self.key_url, self.username)
|
self._request_key(self.key_url, self.username)
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,4 @@ COLORS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
RENDER_TIME = 0.05
|
RENDER_TIME = 0.05
|
||||||
|
MESSAGES_TO_SHOW = 5
|
||||||
24
cmd_chat/client/core/abs/abs_renderer.py
Normal file
24
cmd_chat/client/core/abs/abs_renderer.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class ClientRenderer(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def print_message(self, message: str) -> str:
|
||||||
|
raise NotImplementedError("Need to implement print_message")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def clear_console(self, message: str) -> str:
|
||||||
|
raise NotImplementedError("Need to implement clear_console")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def print_ip(self, url: str, username: str):
|
||||||
|
raise NotImplementedError("Need to implement print_ip")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def print_username(self):
|
||||||
|
raise NotImplementedError("Need to implement print_username")
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def print_chat(self) -> list[str]:
|
||||||
|
raise NotImplementedError("Need to implement print_chat")
|
||||||
59
cmd_chat/client/core/default_renderer.py
Normal file
59
cmd_chat/client/core/default_renderer.py
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
|
||||||
|
from cmd_chat.client.core.abs.abs_renderer import ClientRenderer
|
||||||
|
from cmd_chat.client.config import COLORS
|
||||||
|
|
||||||
|
from colorama import init
|
||||||
|
|
||||||
|
init()
|
||||||
|
|
||||||
|
|
||||||
|
class DefaultClientRenderer(ClientRenderer):
|
||||||
|
|
||||||
|
def __get_os(self) -> str:
|
||||||
|
""" checking what kind of platform you need
|
||||||
|
"""
|
||||||
|
if "Linux" in str(platform.platform()):
|
||||||
|
return "Linux"
|
||||||
|
return "Windows"
|
||||||
|
|
||||||
|
def print_message(self, message: str) -> str:
|
||||||
|
""" generating string with message in required format
|
||||||
|
"""
|
||||||
|
message = message.split(":")
|
||||||
|
if message[0] == self.username:
|
||||||
|
return COLORS["my_username_color"] + message[0] + ": " + message[1] + COLORS["text_color"]
|
||||||
|
return message[0] + ": " + message[1] + COLORS["text_color"]
|
||||||
|
|
||||||
|
def clear_console(self):
|
||||||
|
# For windows clear command its cls
|
||||||
|
# For linux clear command its clear
|
||||||
|
if self.__get_os() == "Linux":
|
||||||
|
os.system("clear")
|
||||||
|
else:
|
||||||
|
os.system("cls")
|
||||||
|
|
||||||
|
def print_ip(
|
||||||
|
self,
|
||||||
|
ip: str
|
||||||
|
) -> str:
|
||||||
|
return f"IP: " + COLORS["ip_color"] + ip + COLORS["text_color"]
|
||||||
|
|
||||||
|
def print_username(
|
||||||
|
self,
|
||||||
|
username: str
|
||||||
|
) -> str:
|
||||||
|
return f"USERNAME: " + COLORS["ip_color"] + username + COLORS["username_color"]
|
||||||
|
|
||||||
|
def print_chat(self, response: list[str]) -> str:
|
||||||
|
for i, msg in enumerate(response["messages"]):
|
||||||
|
actual_message = self._decrypt(msg)
|
||||||
|
if i == 0:
|
||||||
|
for user in response["users_in_chat"]:
|
||||||
|
print(self.print_ip(user.split(",")[0]))
|
||||||
|
print(self.print_username(user.split(",")[1]))
|
||||||
|
print("Write 'q' to quit from chat")
|
||||||
|
print(f"\n{self.print_message(actual_message)}")
|
||||||
|
else:
|
||||||
|
print(f"{self.print_message(actual_message)}")
|
||||||
77
cmd_chat/client/core/rich_renderer.py
Normal file
77
cmd_chat/client/core/rich_renderer.py
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
|
||||||
|
from rich.text import Text
|
||||||
|
from rich.style import Style
|
||||||
|
from rich.console import Console
|
||||||
|
|
||||||
|
from rich.table import Table
|
||||||
|
from cmd_chat.client.core.abs.abs_renderer import ClientRenderer
|
||||||
|
from cmd_chat.client.config import MESSAGES_TO_SHOW
|
||||||
|
|
||||||
|
|
||||||
|
console = Console(width=75)
|
||||||
|
|
||||||
|
|
||||||
|
class RichClientRenderer(ClientRenderer):
|
||||||
|
|
||||||
|
def __get_os(self) -> str:
|
||||||
|
""" checking what kind of platform you need
|
||||||
|
"""
|
||||||
|
if "Linux" in str(platform.platform()):
|
||||||
|
return "Linux"
|
||||||
|
return "Windows"
|
||||||
|
|
||||||
|
def print_message(self, message: str) -> Text:
|
||||||
|
""" generating string with message in required format
|
||||||
|
"""
|
||||||
|
message = message.split(":")
|
||||||
|
if message[0] == self.username:
|
||||||
|
return \
|
||||||
|
Text(text=message[0], style="bold") + \
|
||||||
|
Text(text=": ", style="bold") + \
|
||||||
|
Text(text=message[1], style="underline")
|
||||||
|
return \
|
||||||
|
Text(text=message[0], style="bold") + \
|
||||||
|
Text(text=": ", style="bold") + \
|
||||||
|
Text(text=message[1], style="underline")
|
||||||
|
|
||||||
|
def clear_console(self):
|
||||||
|
# For windows clear command its cls
|
||||||
|
# For linux clear command its clear
|
||||||
|
if self.__get_os() == "Linux":
|
||||||
|
os.system("clear")
|
||||||
|
else:
|
||||||
|
os.system("cls")
|
||||||
|
|
||||||
|
def print_ip(
|
||||||
|
self,
|
||||||
|
ip: str
|
||||||
|
) -> str:
|
||||||
|
return ip
|
||||||
|
|
||||||
|
def print_username(
|
||||||
|
self,
|
||||||
|
username: str
|
||||||
|
) -> str:
|
||||||
|
return username
|
||||||
|
|
||||||
|
def print_chat(self, response: list[str]) -> str:
|
||||||
|
self.clear_console()
|
||||||
|
for i, msg in enumerate(response["messages"][-MESSAGES_TO_SHOW:]):
|
||||||
|
actual_message = self._decrypt(msg)
|
||||||
|
if i == 0:
|
||||||
|
console.print("Users in chat:", justify="left")
|
||||||
|
table = Table(show_header=True, header_style="bold magenta")
|
||||||
|
table.add_column("IP", style="dim", width=12)
|
||||||
|
table.add_column("USERNAME")
|
||||||
|
for user in response["users_in_chat"]:
|
||||||
|
table.add_row(
|
||||||
|
self.print_ip(user.split(',')[0]),
|
||||||
|
self.print_username(user.split(",")[1])
|
||||||
|
)
|
||||||
|
console.print(table)
|
||||||
|
console.print("Write 'q' to quit from chat", justify="left")
|
||||||
|
console.print(f"\n{self.print_message(actual_message)}")
|
||||||
|
else:
|
||||||
|
console.print(f"{self.print_message(actual_message)}")
|
||||||
|
|
@ -6,3 +6,4 @@ colorama
|
||||||
pydantic
|
pydantic
|
||||||
websocket-client
|
websocket-client
|
||||||
flask
|
flask
|
||||||
|
rich
|
||||||
2
setup.py
2
setup.py
|
|
@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="secured_console_chat",
|
name="secured_console_chat",
|
||||||
version="1.1.21",
|
version="1.1.22",
|
||||||
author="dinosaurtirex",
|
author="dinosaurtirex",
|
||||||
author_email="sneakybeaky18@gmail.com",
|
author_email="sneakybeaky18@gmail.com",
|
||||||
packages=[
|
packages=[
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user