From ab630d43f1f1314bfe9bfff08f90fe5863e6b51d Mon Sep 17 00:00:00 2001 From: nordsoft Date: Thu, 10 Nov 2022 02:44:34 +0400 Subject: [PATCH] Implement proper logging --- proxyServer.py | 63 ++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/proxyServer.py b/proxyServer.py index 6c52ab7f8..1fd25ced4 100755 --- a/proxyServer.py +++ b/proxyServer.py @@ -2,6 +2,7 @@ import socket import re import uuid import struct +import logging from threading import Thread PROTOCOL_VERSION_MIN = 1 @@ -11,6 +12,15 @@ PROTOCOL_VERSION_MAX = 1 SERVER_HOST = "0.0.0.0" SERVER_PORT = 5002 # port we want to use +#logging +logHandlerHighlevel = logging.FileHandler('proxyServer.log') +logHandlerHighlevel.setLevel(logging.WARNING) + +logHandlerLowlevel = logging.FileHandler('proxyServer_debug.log') +logHandlerLowlevel.setLevel(logging.DEBUG) + +logging.basicConfig(handlers=[logHandlerHighlevel, logHandlerLowlevel], level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S') + def receive_packed(sock): # Read message length and unpack it into an integer raw_msglen = recvall(sock, 4) @@ -200,7 +210,7 @@ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((SERVER_HOST, SERVER_PORT)) # listen for upcoming connections s.listen(10) -print(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}") +logging.critical(f"[*] Listening as {SERVER_HOST}:{SERVER_PORT}") # active rooms rooms = {} @@ -213,6 +223,8 @@ client_sockets = {} def handleDisconnection(client: socket): + logging.warning(f"[!] Disconnecting client {client}") + if not client in client_sockets: return @@ -234,7 +246,7 @@ def handleDisconnection(client: socket): if sender.client.session in sessions: sender.client.session.removeConnection(client) if not len(sender.client.session.connections): - print(f"[*] Destroying session {sender.client.session.name}") + logging.warning(f"[*] Destroying session {sender.client.session.name}") sessions.remove(sender.client.session) client.close() @@ -277,7 +289,7 @@ def deleteRoom(room: Room): msg2 = msg + f":{client_sockets[player].client.username}" send(player, msg2) - print(f"[*] Destroying room {room.name}") + logging.warning(f"[*] Destroying room {room.name}") rooms.pop(room.name) @@ -293,6 +305,7 @@ def startRoom(room: Room): session = Session() session.name = room.name sessions.append(session) + logging.warning(f"[*] Starting session {session.name}") session.host_uuid = str(uuid.uuid4()) hostMessage = f":>>HOST:{session.host_uuid}:{room.joined - 1}" #one client will be connected locally #host message must be before start message @@ -308,7 +321,7 @@ def startRoom(room: Room): client_sockets.pop(player) #this room shall not exist anymore - print(f"[*] Destroying room {room.name}") + logging.info(f"[*] Destroying room {room.name}") rooms.pop(room.name) @@ -321,15 +334,14 @@ def dispatch(cs: socket, sender: Sender, arr: bytes): msg = str(arr) if msg.find("Aiya!") != -1: sender.client = ClientPipe() - print(" vcmi recognized") + logging.debug(" vcmi recognized") if sender.isPipe(): if sender.client.auth: #if already playing - sending raw bytes as is sender.client.prevmessages.append(arr) - print(" storing message") else: sender.client.prevmessages.append(struct.pack(' PROTOCOL_VERSION_MAX: - print(f"[!] Error: client has incompatbile protocol version {arr[0]}") + logging.critical(f"[!] Error: client has incompatbile protocol version {arr[0]}") send(cs, ":>>ERROR:Cannot connect to remote server due to protocol incompatibility") return @@ -426,7 +434,7 @@ def dispatch(cs: socket, sender: Sender, arr: bytes): _open = msg.partition('<') _close = _open[2].partition('>') if _open[0] != '' or _open[1] == '' or _open[2] == '' or _close[0] == '' or _close[1] == '': - print(f"[!] Incorrect message from {sender.address}: {msg}") + logging.error(f"[!] Incorrect message from {sender.address}: {msg}") return _nextTag = _close[2].partition('<') @@ -436,7 +444,7 @@ def dispatch(cs: socket, sender: Sender, arr: bytes): #greetings to the server if tag == "GREETINGS": if sender.client.auth: - print(f"[!] Greetings from authorized user {sender.client.username} {sender.address}") + logging.error(f"[!] Greetings from authorized user {sender.client.username} {sender.address}") send(cs, ":>>ERROR:User already authorized") return @@ -449,14 +457,14 @@ def dispatch(cs: socket, sender: Sender, arr: bytes): send(cs, f":>>ERROR:Can't connect with the name {tag_value}. This login is already occpupied") return - print(f"[*] {sender.address} autorized as {tag_value}") + logging.info(f"[*] {sender.address} autorized as {tag_value}") sender.client.username = tag_value sender.client.auth = True sendRooms(cs) #VCMI version received if tag == "VER" and sender.client.auth: - print(f"[*] User {sender.client.username} has version {tag_value}") + logging.info(f"[*] User {sender.client.username} has version {tag_value}") sender.client.vcmiversion = tag_value #message received @@ -573,7 +581,6 @@ def listen_for_client(cs): msg = receive_packed(cs) if msg == None or msg == b'': - print(f"[!] Disconnecting client {cs}") handleDisconnection(cs) return @@ -581,7 +588,7 @@ def listen_for_client(cs): except Exception as e: # client no longer connected - print(f"[!] Error: {e}") + logging.error(f"[!] Error: {e}") handleDisconnection(cs) return @@ -589,7 +596,7 @@ def listen_for_client(cs): while True: # we keep listening for new connections all the time client_socket, client_address = s.accept() - print(f"[+] {client_address} connected.") + logging.warning(f"[+] {client_address} connected.") # add the new connected client to connected sockets client_sockets[client_socket] = Sender() client_sockets[client_socket].address = client_address