diff options
author | Thomas Schmid <schmid-thomas@gmx.net> | 2022-04-25 01:32:51 +0300 |
---|---|---|
committer | Thomas Schmid <schmid-thomas@gmx.net> | 2022-04-25 01:32:51 +0300 |
commit | dcb488852675b2e22e0edb42e02a24bf12e0e4c1 (patch) | |
tree | a42b2725c1c21afd366e10267db95505c3fc43bd | |
parent | 20c2b6e501d672f95ce06ba02d4d7fd46602b2af (diff) |
Use a logger in webapp
-rw-r--r-- | src/web/main.py | 15 | ||||
-rw-r--r-- | src/web/script/handler/config.py | 20 | ||||
-rw-r--r-- | src/web/script/handler/websocket.py | 5 | ||||
-rw-r--r-- | src/web/script/http.py | 1 | ||||
-rw-r--r-- | src/web/script/messagepump.py | 9 | ||||
-rw-r--r-- | src/web/script/sieve/__main__.py | 6 | ||||
-rw-r--r-- | src/web/script/sieve/sievesocket.py | 7 | ||||
-rw-r--r-- | src/web/script/webserver.py | 9 | ||||
-rw-r--r-- | src/web/script/websocket.py | 5 |
9 files changed, 51 insertions, 26 deletions
diff --git a/src/web/main.py b/src/web/main.py index a5571550..65de85f3 100644 --- a/src/web/main.py +++ b/src/web/main.py @@ -1,4 +1,5 @@ import pathlib +import logging from argparse import ArgumentParser @@ -13,9 +14,21 @@ from script.config.config import Config parser = ArgumentParser(description='Starts the websocket to sieve proxy.') parser.add_argument("--config", help="The configuration file if omitted it will fallback to ./config.ini") +parser.add_argument('--verbose', '-v', action='count', default=1) args = parser.parse_args() +args.verbose = 40 - (10*args.verbose) if args.verbose > 0 else 0 + +logging.basicConfig(level=args.verbose, format='%(asctime)s %(levelname)s [%(funcName)s] %(filename)s : %(message)s', + datefmt='%Y-%m-%d %H:%M:%S') + +logging.debug('im a DEBUG message') +logging.info('im a INFO message') +logging.warning('im a WARNING message') +logging.critical('im a CRITICAL message') +logging.error('im a ERROR message') + if args.config is None: args.config = "config.ini" @@ -24,7 +37,7 @@ configfile = pathlib.Path( args.config) if not configfile.exists(): - raise Exception("No such config file "+ configfile) + raise Exception(f"No such config file {configfile}") print(f"Loading config from {configfile}") config = Config().load(configfile) diff --git a/src/web/script/handler/config.py b/src/web/script/handler/config.py index 1d19a93d..34aeddb1 100644 --- a/src/web/script/handler/config.py +++ b/src/web/script/handler/config.py @@ -1,4 +1,5 @@ import json +import logging from ..http import HttpResponse @@ -22,13 +23,18 @@ class ConfigHandler: for account in self.__config.get_accounts(): - data[account.get_id()] = { - 'displayname' : account.get_name(), - 'username' : account.get_auth_username(request), - 'authenticate' : account.can_authenticate(), - 'authorize' : account.can_authorize(), - 'endpoint' : f"websocket/{account.get_id()}" - } + try: + + data[account.get_id()] = { + 'displayname' : account.get_name(), + 'username' : account.get_auth_username(request), + 'authenticate' : account.can_authenticate(), + 'authorize' : account.can_authorize(), + 'endpoint' : f"websocket/{account.get_id()}" + } + + except Exception as ex: + logging.warning(f"Skipping invalid account configuration {account.get_name()}, cause {ex}") # if config from request # Reverse proxy default header diff --git a/src/web/script/handler/websocket.py b/src/web/script/handler/websocket.py index 8770ceb1..b9af11f7 100644 --- a/src/web/script/handler/websocket.py +++ b/src/web/script/handler/websocket.py @@ -1,3 +1,4 @@ +import logging from ..websocket import WebSocket from ..sieve.sievesocket import SieveSocket @@ -19,7 +20,7 @@ class WebSocketHandler: def handle_request(self, context, request) -> None: - print(f"Websocket Request for {request.path}") + logging.info(f"Websocket Request for {request.path}") account = self.__config.get_account_by_id( request.path[len("/websocket/"):]) @@ -34,7 +35,7 @@ class WebSocketHandler: sievesocket.start_tls() if not account.can_authenticate(): - print(f"Do Proxy authentication for {account.get_name()}") + logging.info(f"Do Proxy authentication for {account.get_name()}") sievesocket.authenticate( account.get_sieve_user(request), account.get_sieve_password(request), diff --git a/src/web/script/http.py b/src/web/script/http.py index b3e51ccb..d36a2a60 100644 --- a/src/web/script/http.py +++ b/src/web/script/http.py @@ -26,7 +26,6 @@ class HttpRequest: @property def query(self) -> str: - print(self.__request[1].split("?", 1)) return self.__request[1].split("?", 1)[1] @property diff --git a/src/web/script/messagepump.py b/src/web/script/messagepump.py index 7e63ed16..60fe3434 100644 --- a/src/web/script/messagepump.py +++ b/src/web/script/messagepump.py @@ -1,4 +1,5 @@ import select +import logging class MessagePump: @@ -25,18 +26,18 @@ class MessagePump: data = server.recv() if data == b'': - print("Server terminated") + logging.info("Server terminated") return - print(data) + logging.debug(data) client.send(data) if client in sockets: data = client.recv() if data == b'': - print(" Client terminated") + logging.info(" Client terminated") return - print(data) + logging.debug(data) server.send(data) diff --git a/src/web/script/sieve/__main__.py b/src/web/script/sieve/__main__.py index bb2fec58..e4607619 100644 --- a/src/web/script/sieve/__main__.py +++ b/src/web/script/sieve/__main__.py @@ -1,3 +1,5 @@ +import logging + from argparse import ArgumentParser from getpass import getpass @@ -19,10 +21,10 @@ if args.password is None: args.password = getpass() with SieveSocket(args.host, args.port) as socket: - print(socket.capabilities) + logging.debug(socket.capabilities) socket.start_tls() socket.authenticate(args.username, args.password, "") socket.send(b"LISTSCRIPTS\r\n") - print(socket.recv()) + logging.debug(socket.recv()) diff --git a/src/web/script/sieve/sievesocket.py b/src/web/script/sieve/sievesocket.py index 3e4baa93..7e5216e7 100644 --- a/src/web/script/sieve/sievesocket.py +++ b/src/web/script/sieve/sievesocket.py @@ -1,6 +1,7 @@ import ssl import socket import select +import logging from base64 import b64encode @@ -22,9 +23,9 @@ class SieveSocket: return self def __exit__(self, exc_type, exc_val, exc_tb) -> None: - print(exc_type) - print(exc_val) - print(exc_tb) + logging.debug(exc_type) + logging.debug(exc_val) + logging.debug(exc_tb) self.disconnect() def connect(self): diff --git a/src/web/script/webserver.py b/src/web/script/webserver.py index 2cd40093..95dcb546 100644 --- a/src/web/script/webserver.py +++ b/src/web/script/webserver.py @@ -2,6 +2,7 @@ import ssl import socket import traceback import sys +import logging from concurrent.futures import ThreadPoolExecutor @@ -64,7 +65,7 @@ class WebServer: handler.handle_request(context, request) return - print("404 File not found "+request.url) + logging.warning("404 File not found "+request.url) raise HttpException(404, "File not found "+request.url) except HttpException as ex: @@ -82,8 +83,8 @@ class WebServer: response.send(context,"Internal Server error\r\n\r\n" + "\r\n".join(traceback.format_exception(exc_type, exc_value, exc_tb))) - print(str(ex)) - print("".join(traceback.format_exception(exc_type, exc_value, exc_tb))) + logging.warning(str(ex)) + logging.warning("".join(traceback.format_exception(exc_type, exc_value, exc_tb))) finally: context.socket.shutdown(socket.SHUT_RDWR) @@ -103,7 +104,7 @@ class WebServer: sock.bind((self.__address, self.__port)) sock.listen(5) - print("Listening on https://"+self.__address+":"+str(self.__port)) + logging.info(f"Listening on https://{self.__address}:{self.__port}") while True: # accept connections from outside diff --git a/src/web/script/websocket.py b/src/web/script/websocket.py index 2c675f12..919380dd 100644 --- a/src/web/script/websocket.py +++ b/src/web/script/websocket.py @@ -1,5 +1,6 @@ from hashlib import sha1 from base64 import b64encode +import logging from .http import HttpException, HttpResponse @@ -74,7 +75,7 @@ class WebSocket: return self def __exit__(self, exc_type, exc_val, exc_tb) -> None: - print("On Exit") + logging.debug("On Exit") #TODO send a websocket disconnect message... #self.disconnect() @@ -129,7 +130,7 @@ class WebSocket: text = message[2:] - print(str(code) + " - " + text.decode()) + logging.debug(str(code) + " - " + text.decode()) raise Exception("Connection gracefully closed. "+str(code)+" "+text.decode()) |