From fa7206848cc1165ee1da1ab846be5e1c9a40dc21 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Thu, 9 Mar 2023 11:29:36 -0500 Subject: [PATCH] general code cleanup for multiple games --- titles/chuni/index.py | 28 +++++++++++++++------------- titles/cm/index.py | 13 ++++++------- titles/mai2/index.py | 14 +++++++------- titles/ongeki/index.py | 15 ++++++++------- titles/wacca/index.py | 22 +++++++++++----------- 5 files changed, 47 insertions(+), 45 deletions(-) diff --git a/titles/chuni/index.py b/titles/chuni/index.py index ce6bda9..5ee03a4 100644 --- a/titles/chuni/index.py +++ b/titles/chuni/index.py @@ -87,7 +87,7 @@ class ChuniServlet(): def render_POST(self, request: Request, version: int, url_path: str) -> bytes: if url_path.lower() == "/ping": - return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8")) + return zlib.compress(b'{"returnCode": "1"}') req_raw = request.content.getvalue() url_split = url_path.split("/") @@ -138,13 +138,13 @@ class ChuniServlet(): except: self.logger.error(f"Failed to decrypt v{version} request to {endpoint} -> {req_raw}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') encrtped = True if not encrtped and self.game_cfg.crypto.encrypted_only: self.logger.error(f"Unencrypted v{version} {endpoint} request, but config is set to encrypted only: {req_raw}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') try: unzip = zlib.decompress(req_raw) @@ -160,17 +160,19 @@ class ChuniServlet(): func_to_find = "handle_" + inflection.underscore(endpoint) + "_request" - try: - handler = getattr(self.versions[internal_ver], func_to_find) - resp = handler(req_data) - - except AttributeError as e: - self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + if not hasattr(self.versions[internal_ver], func_to_find): + self.logger.warning(f"Unhandled v{version} request {endpoint}") + resp = {"returnCode": 1} + + else: - except Exception as e: - self.logger.error(f"Error handling v{version} method {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + try: + handler = getattr(self.versions[internal_ver], func_to_find) + resp = handler(req_data) + + except Exception as e: + self.logger.error(f"Error handling v{version} method {endpoint} - {e}") + return zlib.compress(b'{"stat": "0"}') if resp == None: resp = {'returnCode': 1} diff --git a/titles/cm/index.py b/titles/cm/index.py index b2751b0..ff9ea1f 100644 --- a/titles/cm/index.py +++ b/titles/cm/index.py @@ -87,7 +87,7 @@ class CardMakerServlet(): except zlib.error as e: self.logger.error( f"Failed to decompress v{version} {endpoint} request -> {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') req_data = json.loads(unzip) @@ -95,19 +95,18 @@ class CardMakerServlet(): func_to_find = "handle_" + inflection.underscore(endpoint) + "_request" + if not hasattr(self.versions[internal_ver], func_to_find): + self.logger.warning(f"Unhandled v{version} request {endpoint}") + return zlib.compress(b'{"returnCode": 1}') + try: handler = getattr(self.versions[internal_ver], func_to_find) resp = handler(req_data) - except AttributeError as e: - self.logger.warning( - f"Unhandled v{version} request {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) - except Exception as e: self.logger.error( f"Error handling v{version} method {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') if resp is None: resp = {'returnCode': 1} diff --git a/titles/mai2/index.py b/titles/mai2/index.py index 80b3ac8..305e389 100644 --- a/titles/mai2/index.py +++ b/titles/mai2/index.py @@ -70,7 +70,7 @@ class Mai2Servlet(): def render_POST(self, request: Request, version: int, url_path: str) -> bytes: if url_path.lower() == "/ping": - return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8")) + return zlib.compress(b'{"returnCode": "1"}') req_raw = request.content.getvalue() url = request.uri.decode() @@ -102,7 +102,7 @@ class Mai2Servlet(): except zlib.error as e: self.logger.error(f"Failed to decompress v{version} {endpoint} request -> {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') req_data = json.loads(unzip) @@ -110,17 +110,17 @@ class Mai2Servlet(): func_to_find = "handle_" + inflection.underscore(endpoint) + "_request" + if not hasattr(self.versions[internal_ver], func_to_find): + self.logger.warning(f"Unhandled v{version} request {endpoint}") + return zlib.compress(b'{"returnCode": 1}') + try: handler = getattr(self.versions[internal_ver], func_to_find) resp = handler(req_data) - - except AttributeError as e: - self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) except Exception as e: self.logger.error(f"Error handling v{version} method {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') if resp == None: resp = {'returnCode': 1} diff --git a/titles/ongeki/index.py b/titles/ongeki/index.py index 2bbdb46..09ab18c 100644 --- a/titles/ongeki/index.py +++ b/titles/ongeki/index.py @@ -73,7 +73,7 @@ class OngekiServlet(): def render_POST(self, request: Request, version: int, url_path: str) -> bytes: if url_path.lower() == "/ping": - return zlib.compress(json.dumps({'returnCode': 1}, ensure_ascii=False).encode("utf-8")) + return zlib.compress(b'{"returnCode": 1}') req_raw = request.content.getvalue() url_split = url_path.split("/") @@ -102,13 +102,14 @@ class OngekiServlet(): # doing encrypted. The likelyhood of false positives is low but # technically not 0 self.logger.error("Encryption not supported at this time") + return b"" try: unzip = zlib.decompress(req_raw) except zlib.error as e: self.logger.error(f"Failed to decompress v{version} {endpoint} request -> {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') req_data = json.loads(unzip) @@ -116,17 +117,17 @@ class OngekiServlet(): func_to_find = "handle_" + inflection.underscore(endpoint) + "_request" + if not hasattr(self.versions[internal_ver], func_to_find): + self.logger.warning(f"Unhandled v{version} request {endpoint}") + return zlib.compress(b'{"returnCode": 1}') + try: handler = getattr(self.versions[internal_ver], func_to_find) resp = handler(req_data) - - except AttributeError as e: - self.logger.warning(f"Unhandled v{version} request {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) except Exception as e: self.logger.error(f"Error handling v{version} method {endpoint} - {e}") - return zlib.compress("{\"stat\": \"0\"}".encode("utf-8")) + return zlib.compress(b'{"stat": "0"}') if resp == None: resp = {'returnCode': 1} diff --git a/titles/wacca/index.py b/titles/wacca/index.py index e2f9758..b4a4aea 100644 --- a/titles/wacca/index.py +++ b/titles/wacca/index.py @@ -117,24 +117,24 @@ class WaccaServlet(): self.logger.info(f"v{req_json['appVersion']} {url_path} request from {request.getClientAddress().host} with chipId {req_json['chipId']}") self.logger.debug(req_json) + if not hasattr(self.versions[internal_ver], func_to_find): + self.logger.warn(f"{req_json['appVersion']} has no handler for {func_to_find}") + resp = BaseResponse().make() + return end(resp) + try: handler = getattr(self.versions[internal_ver], func_to_find) - if handler is not None: - resp = handler(req_json) - - else: - self.logger.warn(f"{req_json['appVersion']} has no handler for {func_to_find}") - resp = None - - if resp is None: - resp = BaseResponse().make() + resp = handler(req_json) self.logger.debug(f"{req_json['appVersion']} response {resp}") return end(resp) except Exception as e: self.logger.error(f"{req_json['appVersion']} Error handling method {url_path} -> {e}") - if self.game_cfg.server.loglevel <= logging.DEBUG: + if self.core_cfg.server.is_develop: raise - resp = BaseResponse().make() + + resp = BaseResponse() + resp.status = 1 + resp.message = "A server error occoured." return end(resp)