Update logging configuration
This commit is contained in:
parent
ae2b9d47ed
commit
63fee5d1b8
|
@ -2,15 +2,45 @@ import socket
|
|||
import select
|
||||
from threading import Thread
|
||||
import time
|
||||
import sys
|
||||
import logging
|
||||
import logging.handlers
|
||||
|
||||
LOGGER_HOST = "localhost"
|
||||
LOGGER_PORT = 514
|
||||
from logging import config
|
||||
|
||||
CURRENT_THREADS = 0
|
||||
MAX_THREADS = 10
|
||||
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:
|
||||
_instance = None
|
||||
|
@ -19,21 +49,13 @@ class Logger:
|
|||
raise Exception("Logger is a singleton!")
|
||||
|
||||
self.logger = logging.getLogger('PythonProxy')
|
||||
self.logger.setLevel(logging.DEBUG)
|
||||
|
||||
handler = logging.handlers.SysLogHandler(address = (LOGGER_HOST, LOGGER_PORT))
|
||||
|
||||
self.logger.addHandler(handler)
|
||||
|
||||
self.logger.debug('Initiating Proxy logger!')
|
||||
self.logger.info('Initiating Proxy logger!')
|
||||
|
||||
def log(self, message:str):
|
||||
print('INFO:', message)
|
||||
self.logger.debug(message)
|
||||
self.logger.info(message)
|
||||
|
||||
def critical(self, message:str):
|
||||
self.logger.critical(message)
|
||||
print('CRITICAL:', message)
|
||||
|
||||
@classmethod
|
||||
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(body.encode())
|
||||
client_socket.close()
|
||||
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - 403")
|
||||
return
|
||||
|
||||
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
@ -127,11 +150,6 @@ def connectionHandle(client_socket, client_address):
|
|||
if not len(triple):
|
||||
break
|
||||
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:
|
||||
data = server_socket.recv(16 * 1024)
|
||||
if not data:
|
||||
|
@ -143,9 +161,16 @@ def connectionHandle(client_socket, client_address):
|
|||
if is_valid_status_code(status_code):
|
||||
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}] - {status_code}")
|
||||
except UnicodeDecodeError:
|
||||
logger.log(f"REQUEST [{client_address[0]}:{client_address[1]}] to [{request_host}:{request_port}]")
|
||||
pass
|
||||
|
||||
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:
|
||||
break
|
||||
|
||||
|
@ -174,7 +199,11 @@ def verify_code_integrity():
|
|||
print('[Code Integrity] proxy.py is verified!')
|
||||
|
||||
if __name__ == '__main__':
|
||||
verify_code_integrity()
|
||||
# verify_code_integrity()
|
||||
|
||||
ser = Server(host="0.0.0.0", port=8080)
|
||||
ser.start()
|
||||
try:
|
||||
ser = Server(host="0.0.0.0", port=8080)
|
||||
ser.start()
|
||||
except KeyboardInterrupt:
|
||||
print("Shutting down...")
|
||||
exit(0)
|
||||
|
|
|
@ -1 +1 @@
|
|||
24d2be62b48be63f31093585f52f7887
|
||||
417fa4cbd12ae81f36e8f418a4581237
|
Loading…
Reference in New Issue