From 1d95ede8db65eaeb7e8fff238b20fb81b3422df6 Mon Sep 17 00:00:00 2001 From: KyuDev Date: Thu, 13 Apr 2023 17:55:01 +0200 Subject: [PATCH] api added --- webserver/main.py | 46 +++++++++++++++++-- webserver/templates/main/dashboard_main1.html | 8 ++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/webserver/main.py b/webserver/main.py index 9938b3e..1bdc43d 100644 --- a/webserver/main.py +++ b/webserver/main.py @@ -14,8 +14,6 @@ from werkzeug.middleware.shared_data import SharedDataMiddleware from oauthlib.oauth2 import WebApplicationClient from discord_webhook import DiscordWebhook, DiscordEmbed -contact_support_dc_webhook = "" - app = Flask(__name__) app.config.update( @@ -40,7 +38,6 @@ class User(UserMixin): def load_user(userid): return User(userid) - def get_username(self): return self.username @@ -50,7 +47,6 @@ def homepage(): return redirect("/login") - @app.route('/login', methods=['GET',"POST"]) def login_general(): if request.method == "POST": @@ -112,11 +108,52 @@ def cpdashy_1_main(): user_data = json.load(f) # Continue here -> log data reading + # start sim button return render_template("main/dashboard_main1.html",sidebar_html_insert=cpdash_get_sidebar().replace("active_state_class1","is-active"), profile_picture=user_data["picture"],profile_username=user_data["username"],profile_userid=user_data["userid"],profile_email=user_data["email"]) else: return redirect('/login') + +@app.route("/d1/startsim", methods=['GET']) #start the sim +def cpdashy_startsim(): + if current_user.is_authenticated: + clear_session_full() + with open("database/temp/attack_start.txt","w") as f: + f.write(str(time.time())) + return redirect("/d1") + else: + return redirect('/login') + + +# API +def clear_session_full(): + os.remove("database/temp/sim_start.txt") + os.remove("database/temp/attack_start.txt") + with open("database/logs/red.json","w") as f: + f.write("{}") + with open("database/logs/blue.json","w") as f: + f.write("{}") + +@app.route("/api/red", methods=['POST']) +def api_red_logs(): + temp_json_n = request.json + print("red log received") + print(temp_json_n) + + if temp_json_n["title"] == "Start of attack": + with open("database/temp/attack_start.txt",'w') as f: + f.write(str(temp_json_n["timestamp"])) + + with open("database/logs/red.json","r") as f: + logs_list = json.load(f) + logs_list.append(temp_json_n) + with open("database/logs/red.json","w") as f: + json.dump(logs_list) + + return("log saved") + + # Error handling @@ -124,7 +161,6 @@ def cpdashy_1_main(): def custom_401(error): return redirect("/") - @app.errorhandler(404) def custom_404(error): return redirect("/") diff --git a/webserver/templates/main/dashboard_main1.html b/webserver/templates/main/dashboard_main1.html index e311f67..f2b8cdb 100644 --- a/webserver/templates/main/dashboard_main1.html +++ b/webserver/templates/main/dashboard_main1.html @@ -1309,14 +1309,14 @@
-
Status
+
Time since Sim start
xxx
- -
Other
-
xxx
+
+
Start Simulation
+
click here