From 6a14ee9dcfbb737ba5c46bb5a10fe98f815f7981 Mon Sep 17 00:00:00 2001 From: ZKWolf Date: Mon, 26 Jun 2023 19:00:43 +0200 Subject: [PATCH] Debugging Endpoint write data to DB --- src/config/example_config.yaml | 4 +++- src/endpoints/unknown.py | 22 +-------------------- src/endpoints/web.py | 36 ++++++++++++++++++++++++++++++++++ src/flask_definitions.py | 3 ++- src/logic/mongodb_handler.py | 22 +++++++++++++++++++++ 5 files changed, 64 insertions(+), 23 deletions(-) diff --git a/src/config/example_config.yaml b/src/config/example_config.yaml index 498c638..3ceb555 100644 --- a/src/config/example_config.yaml +++ b/src/config/example_config.yaml @@ -6,4 +6,6 @@ steam: mongodb: host: mongodb://IP:27017/ db: db_name - collection: collection_name \ No newline at end of file + collection: collection_name +api: + allowed_tokens: token1, token2, token3, token4 \ No newline at end of file diff --git a/src/endpoints/unknown.py b/src/endpoints/unknown.py index 3138ac0..2529dc8 100644 --- a/src/endpoints/unknown.py +++ b/src/endpoints/unknown.py @@ -777,24 +777,4 @@ def catalog_get(game_version): @app.errorhandler(404) def debug_404(e): - print("##################################################################################") - print("DEBUG 404") - print("##################################################################################") - print('HTTP Headers:') - for header in request.headers: - print(f'{header[0]}: {header[1]}') - - print('Cookies:') - for key, value in request.cookies.items(): - print(f'{key}: {value}') - - print('Endpoint: ', request.endpoint) - print('Path: ', request.path) - print('Method: ', request.method) - - if request.json: - print('JSON Payload:') - print(request.json) - - print("##################################################################################") - return jsonify({"status": "error", "code": 404}) + return jsonify({"message": "Endpoint not found"}), 404 diff --git a/src/endpoints/web.py b/src/endpoints/web.py index eb6ca20..38e18f0 100644 --- a/src/endpoints/web.py +++ b/src/endpoints/web.py @@ -46,3 +46,39 @@ def debug_user(steamid): finished_challanges=user_data.get('finished_challanges'), open_challanges=user_data.get('open_challanges') ) + + +@app.route("/debug/mirrors", methods=["POST", "GET"]) +def debug_mirrors_write(): + get_remote_ip() + if request.method == "POST": + try: + api_token = request.cookies.get("api_token") + if api_token is None: + return jsonify({"status": "error", "message": "No api token found"}, 401) + else: + if api_token in allowed_tokens: + + steam_user_id = request.json.get("steamid") + data_b = request.json.get("data") + + if data_b or steam_user_id: + return_val = mongo.write_data_with_list(steamid=steam_user_id, items_dict=data_b, server=mongo_host, db=mongo_db, + collection=mongo_collection) + if return_val == None: + return jsonify({"status": "error", "message": "Error."}, 400) + else: + return jsonify({"status": "success", "message": "Data written."}, 200) + else: + return jsonify({"status": "error", "message": "No data or Steamid found."}, 400) + else: + return jsonify({"status": "error", "message": "Invalid api token"}, 401) + + except TimeoutError: + print("Timeout error") + return jsonify({"status": "error"}) + except Exception as e: + print(e) + logger.graylog_logger(level="error", handler="logging_debug_mirror_write", message=e) + if request.method == "GET": + return jsonify({"message": "Endpoint not found"}), 404 diff --git a/src/flask_definitions.py b/src/flask_definitions.py index b4d54af..3575ca3 100644 --- a/src/flask_definitions.py +++ b/src/flask_definitions.py @@ -10,4 +10,5 @@ graylog_server = config['graylog']['host'] steam_api_key = config['steam']['api_key'] mongo_host = config['mongodb']['host'] mongo_db = config['mongodb']['db'] -mongo_collection = config['mongodb']['collection'] \ No newline at end of file +mongo_collection = config['mongodb']['collection'] +allowed_tokens = config['api']['allowed_tokens'] diff --git a/src/logic/mongodb_handler.py b/src/logic/mongodb_handler.py index 0d8806f..e2c8e10 100644 --- a/src/logic/mongodb_handler.py +++ b/src/logic/mongodb_handler.py @@ -147,5 +147,27 @@ class Mongo: logger.graylog_logger(level="error", handler="mongo_get_data_with_list", message=e) return None + def write_data_with_list(self, steamid, items_dict, server, db, collection): + try: + steam_id = str(steamid) + self.dyn_db = db + self.dyn_collection = collection + client = pymongo.MongoClient(server) + self.dyn_db = client[self.dyn_db] + self.dyn_collection = self.dyn_db[self.dyn_collection] + existing_document = self.dyn_collection.find_one({'steamid': steamid}) + + if existing_document: + update_query = {'$set': items_dict} + self.dyn_collection.update_one({'steamid': steamid}, update_query) + return {"status": "success", "message": "Data updated"} + else: + print(f"No user found with steamid: {steam_id}") + return None + except Exception as e: + print(e) + logger.graylog_logger(level="error", handler="mongo_write_data_with_list", message=e) + return None + mongo = Mongo()