Update logging configuration

This commit is contained in:
Jose Henrique 2023-12-04 22:22:57 -03:00
parent ae2b9d47ed
commit 63fee5d1b8
2 changed files with 51 additions and 22 deletions

View File

@ -2,15 +2,45 @@ import socket
import select import select
from threading import Thread from threading import Thread
import time import time
import sys
import logging import logging
import logging.handlers import logging.handlers
from logging import config
LOGGER_HOST = "localhost"
LOGGER_PORT = 514
CURRENT_THREADS = 0 CURRENT_THREADS = 0
MAX_THREADS = 10 MAX_THREADS = 10
BACKLOG = 50 BACKLOG = 50
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'stdout': {
'class': 'logging.StreamHandler',
'stream': sys.stdout,
'formatter': 'verbose',
},
'sys-logger6': {
'class': 'logging.handlers.SysLogHandler',
"address": ("127.0.0.1", 514),
'facility': "local6",
'formatter': 'verbose',
},
},
'loggers': {
'PythonProxy': {
'handlers': ['sys-logger6', 'stdout'],
'level': logging.DEBUG,
'propagate': True,
},
}
}
config.dictConfig(LOGGING)
class Logger: class Logger:
_instance = None _instance = None
@ -19,21 +49,13 @@ class Logger:
raise Exception("Logger is a singleton!") raise Exception("Logger is a singleton!")
self.logger = logging.getLogger('PythonProxy') self.logger = logging.getLogger('PythonProxy')
self.logger.setLevel(logging.DEBUG) self.logger.info('Initiating Proxy logger!')
handler = logging.handlers.SysLogHandler(address = (LOGGER_HOST, LOGGER_PORT))
self.logger.addHandler(handler)
self.logger.debug('Initiating Proxy logger!')
def log(self, message:str): def log(self, message:str):
print('INFO:', message) self.logger.info(message)
self.logger.debug(message)
def critical(self, message:str): def critical(self, message:str):
self.logger.critical(message) self.logger.critical(message)
print('CRITICAL:', message)
@classmethod @classmethod
def instance(self): def instance(self):
@ -116,6 +138,7 @@ def connectionHandle(client_socket, client_address):
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n") client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\n")
client_socket.sendall(body.encode()) client_socket.sendall(body.encode())
client_socket.close() client_socket.close()
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403")
return return
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -127,11 +150,6 @@ def connectionHandle(client_socket, client_address):
if not len(triple): if not len(triple):
break break
try: try:
if client_socket in triple:
data = client_socket.recv(16 * 1024)
if not data:
break
server_socket.send(data)
if server_socket in triple: if server_socket in triple:
data = server_socket.recv(16 * 1024) data = server_socket.recv(16 * 1024)
if not data: if not data:
@ -143,9 +161,16 @@ def connectionHandle(client_socket, client_address):
if is_valid_status_code(status_code): if is_valid_status_code(status_code):
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}") logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
except UnicodeDecodeError: except UnicodeDecodeError:
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
pass pass
client_socket.send(data) client_socket.send(data)
if client_socket in triple:
data = client_socket.recv(16 * 1024)
if not data:
break
server_socket.send(data)
except ConnectionAbortedError: except ConnectionAbortedError:
break break
@ -174,7 +199,11 @@ def verify_code_integrity():
print('[Code Integrity] proxy.py is verified!') print('[Code Integrity] proxy.py is verified!')
if __name__ == '__main__': if __name__ == '__main__':
verify_code_integrity() # verify_code_integrity()
try:
ser = Server(host="0.0.0.0", port=8080) ser = Server(host="0.0.0.0", port=8080)
ser.start() ser.start()
except KeyboardInterrupt:
print("Shutting down...")
exit(0)

View File

@ -1 +1 @@
24d2be62b48be63f31093585f52f7887 417fa4cbd12ae81f36e8f418a4581237