-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
317 lines (259 loc) · 13.8 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import socket
import threading
import sys
import os
import customtkinter as tk
import logging
import signal
import time
from datetime import datetime
import GUI
from ClientModules import SoundModule, AppConfigParser, Popup, NetworkManager, LogFileClear, Installer
logging.basicConfig(filename='app.log', filemode='a', format='%(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
VERSION = "v0.1"
CLIENT_TYPE = "unregistered"
USERNAME = os.getlogin()
HEADERS = {"client-type" : CLIENT_TYPE, "VERSION" : VERSION, "username" : USERNAME}
users = []
def resource_path():
""" Change CWD to the AppData path """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
os.chdir(base_path)
except Exception:
base_path = os.path.abspath(".")
os.chdir(base_path)
if getattr(sys, 'frozen', False):
program_directory = os.path.dirname(os.path.abspath(sys.executable))
ENV = "PROD" # Run in .exe File. Assume it's a production environment.
if Installer.check_if_first_time_run(program_directory) == True:
print(f'No app.config found, the program is probably being run the first time. Creating Desktop shortcut...')
logging.warn(f'No app.config found, the program is probably being run the first time. Creating Desktop shortcut...')
Popup.show_info_box(f'PyChat-Remastered - Welcome!', f'Thank you for installing PyChat-Remastered!\n\nThe program will now create a shortcut on your Desktop.\nPlease review the content of your app.config - A Server should be preconfigured and point to xcloud.ddns.net on Port 5555.\n\nPyChat-Remastered & the original PyChat are Products of iLollek. All rights reserved.')
Installer.create_desktop_shortcut(program_directory)
resource_path()
AppConfigParser.create_config(program_directory)
else:
program_directory = os.path.dirname(os.path.abspath(__file__))
ENV = "DEV" # Run in .py File. Assume it's a development environment.
AppConfigParser.create_config(program_directory)
def timestamp():
now = datetime.now()
formatted_timestamp = now.strftime("%d %b %Y - %H:%M:%S")
return formatted_timestamp
def log_chatmessage(message: str):
"""Logs a Chatmessage if log_chatmessages in app.config is set to True. Filters out Replies."""
if message.startswith(f'ACK=') == False:
if parser.get_config("log_chatmessages") == "True":
f = open(f'{program_directory}\\chat.log', 'a')
f.write(f'[{timestamp()}] {message}\n')
f.close()
def get_app_config():
"""Starts the AppConfigParser and reads it."""
global appconfig
appconfig = AppConfigParser(f'{program_directory}\\app.config')
appconfig.parse_config()
get_app_config()
def fill_userbox():
"""Fills the Userbox using the global "users" List."""
GUI.App.clear_userbox(root)
for user in users:
GUI.App.insert_into_userbox(root, user)
def close():
"""Closes the PyChat-Remastered Client."""
print(f'Closing PyChat-Remastered...')
logging.info(f'Closing PyChat-Remastered...')
print(f'The GUI needed {ticks} Ticks to start.')
logging.info(f'The GUI needed {ticks} Ticks to start.')
try:
client_socket.send(f'REQ=LEAVE'.encode())
except socket.error as e:
pass
except NameError as e:
pass
os.kill(os.getpid(), signal.SIGTERM) # This is the last line that gets executed.
def run_gui():
global root
GUI.get_theme()
root = tk.CTk()
app = GUI.App(root, get_message_from_user)
root.mainloop()
print("Main Window Closed.")
logging.info("Main Window Closed.")
close()
import logging
class PyChatREMProtocol:
class EventHandler:
def user_join_event(client_socket: socket):
"""Handles a User join Event. (%USERJOIN%)"""
logging.info("User joined event")
SoundModule.play_sound_user_join(appconfig.get_config("soundpack"))
PyChatREMProtocol.RequestSender.request_userlist(client_socket)
def user_leave_event(client_socket: socket):
"""Handles a User leave Event. (%USERLEAVE%)"""
logging.info("User leave event")
SoundModule.play_sound_user_leave(appconfig.get_config("soundpack"))
PyChatREMProtocol.RequestSender.request_userlist(client_socket)
def on_message_received(message: str):
"""Checks if the Message has been sent by the User himself.
If True, it doesn't play a sound. If False, it plays a Sound."""
logging.info("Message received")
if message.startswith(USERNAME) == False:
SoundModule.play_sound_message_received(appconfig.get_config("soundpack"))
def on_message_sent():
"""Plays a sound when a Message is sent."""
logging.info("Message sent")
SoundModule.play_sound_message_sent(appconfig.get_config("soundpack"))
class RequestReceiver:
def response_userlist(request: str):
global users
"""Parses the ACK=USERS reply from the Server and fills it into the "users" global List."""
logging.info("Received user list response")
users.clear()
request = request.split("$")
users = request[1].split("%")
fill_userbox()
def assign_request_to_method(client_socket: socket, request: str):
"""Assigns a Request to the approriate Method for Handling."""
logging.info("Assigning request to method")
if "ACK=USERS" in request:
PyChatREMProtocol.RequestReceiver.response_userlist(request)
class RequestSender:
def request_userlist(client_socket: socket) -> bool:
"""Sends an "Request Userlist" Request to the Server.
Args:
- client_socket (socket): The Client Socket Stream to send the Request
Returns:
- success (bool): True if the Server Accepted, False otherwise"""
logging.info("Sending user list request")
try:
client_socket.send(f'REQ=GETUSERS'.encode())
return True
except socket.error as e:
print(f'Socket Exception: {e}')
logging.error(f'Socket Exception: {e}')
return False
def request_authentification(client_socket: socket, headers: str) -> bool:
"""Sends an Authentification Request to the Server.
Args:
- client_socket (socket): The Client Socket Stream to send the Request
- headers (str): The Standardized Headers for Unauthorized Clients
Returns:
- success (bool): True if the Server Accepted, False otherwise"""
logging.info("Sending authentication request")
try:
client_socket.send(f'REQ=AUTH${str(HEADERS)}'.encode())
reply = client_socket.recv(512).decode()
if reply == "ACK=OK":
return True
else:
return False
except socket.error as e:
print(f'Socket Exception: {e}')
logging.error(f'Socket Exception: {e}')
return False
def receive_messages(client_socket: socket):
PyChatREMProtocol.RequestSender.request_userlist(client_socket)
while True:
try:
# Receive message from server
message = client_socket.recv(1024).decode()
print(f'Received Message: {message}')
if str(message).startswith("[SERVER]") == True:
# This is a Server Announcement / Broadcast.
if str(message).count('%') == 2:
# The Message has 2 Percent-Signs. This could mean that we have some Event.
if "%USERJOIN%" in message:
message = str(message).replace("%USERJOIN%", "")
if USERNAME not in message:
threading.Thread(target=PyChatREMProtocol.EventHandler.user_join_event, args=(client_socket,)).start()
elif "%USERLEAVE%" in message:
message = str(message).replace("%USERLEAVE%", "")
threading.Thread(target=PyChatREMProtocol.EventHandler.user_leave_event, args=(client_socket,)).start()
if str(message) == "Disconnected from Server":
root.title(f'PyChat Remastered - Not Connected!')
if str(message).startswith("ACK=") == False:
GUI.App.insert_into_chatbox(root, message)
threading.Thread(target=PyChatREMProtocol.EventHandler.on_message_received, args=(message,)).start()
log_chatmessage(str(message))
else:
PyChatREMProtocol.RequestReceiver.assign_request_to_method(client_socket, message)
if not message:
# If no message received, close connection
client_socket.close()
print(f'Disconnected from server.')
logging.warn(f'Disconnected from server.')
break
except socket.error as e:
print(f"Error: {e}")
# If an error occurs, close connection
client_socket.close()
print("Disconnected from server.")
logging.warn(f'Disconnected from server.')
root.title(f'PyChat Remastered - Not Connected!')
break
def connect_to_server(domain_or_ip="localhost", port=5555):
"""This Initiates the Connection to the Server using the PyChat-REM Protocol.
Args:
- domain_or_ip (str): The FQDN or IP Address of the Server (defaults to localhost)
- port (int): The TCP Port of the Server (defaults to 5555)"""
try:
global client_socket
root.title(f'PyChat Remastered - Connecting...')
# Create socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to server
client_socket.connect((domain_or_ip, port))
authentification_success = PyChatREMProtocol.RequestSender.request_authentification(client_socket, HEADERS)
if authentification_success == False:
Popup.show_error_box("PyChat-Remastered - Server Authentification Error!", f"The Server you have Connected to is active, however it denied your Request to join the Chatroom. This might be because of your Clients Settings, Configuration or Version.\n\nServer Address: {parser.get_config('server_address')}\nServer Port: {parser.get_config('server_port')}\nClient-Type: {HEADERS['client-type']}\nClient-Version: {HEADERS['VERSION']}")
close()
else:
root.title(f'PyChat Remastered - Connected!')
# Start a new thread to receive messages from the server
receive_thread = threading.Thread(target=PyChatREMProtocol.receive_messages, args=(client_socket,))
receive_thread.start()
while True:
# This is the Heartbeat loop
time.sleep(15)
print(f'Sending Heartbeat...')
client_socket.send(f'REQ=HEARTBEAT'.encode())
print(f'Sent Heartbeat!')
except socket.error as e:
print(f"Error: {e}")
logging.error(f'Error: {e}')
root.title(f'PyChat Remastered - Not Connected!')
def get_message_from_user():
"""Grabs the Message in the tkinter Input (Entry). Is Called via a Callback from the GUI."""
# Here you can implement a function to get a message from the user
# For simplicity, we'll just return a hardcoded message for now
user_message_content = GUI.App.get_entry_content(root)
if len(user_message_content) != 0:
try:
client_socket.send(f'{USERNAME}: {user_message_content}'.encode())
threading.Thread(target=SoundModule.play_sound_message_sent, args=(appconfig.get_config("soundpack"),)).start()
except socket.error as e:
print(f'Error: {e}')
logging.error(f'Error: {e}')
client_socket.close()
root.title(f'PyChat Remastered - Not Connected!')
if __name__ == "__main__":
parser = AppConfigParser(f'{program_directory}\\app.config')
parser.parse_config()
theme = parser.get_config("theme")
soundpack = parser.get_config("soundpack")
LogFileClear.clear_log_if_needed("app.log", "25KB")
server_ip = NetworkManager.get_ip_address(parser.get_config("server_address"))
print(f'Server Address (from app.config): {parser.get_config("server_address")} -> Server IP: {server_ip}')
logging.info(f'Server Address (from app.config): {parser.get_config("server_address")} -> Server IP: {server_ip}')
ticks = 0
gui_thread = threading.Thread(target=run_gui).start()
while True:
try:
PyChatREMProtocol.connect_to_server(server_ip, int(parser.get_config("server_port")))
except NameError as e:
ticks += 1
except socket.error as e:
Popup.show_error_box(f'PyChat-Remastered - Unable to get Server IP', f'The NetworkManager is unable to resolve the Domain you specified in app.config! Are you sure that it is set correctly?\n\nServer Name: {parser.get_config("server_address")}')
close()