From 955aaf48e5ec585d25a73a75d194d1c614f3ec50 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 14 Jul 2023 12:20:33 +0300 Subject: [PATCH] revert 2 commits --- files/routes/allroutes.py | 8 ++--- files/routes/wrappers.py | 59 ++++++++++++++++++------------------- files/templates/header.html | 4 ++- 3 files changed, 36 insertions(+), 35 deletions(-) diff --git a/files/routes/allroutes.py b/files/routes/allroutes.py index c7201f5fc..87adf4515 100644 --- a/files/routes/allroutes.py +++ b/files/routes/allroutes.py @@ -11,10 +11,6 @@ from files.__main__ import app, limiter, get_CF, redis_instance def before_request(): g.v = None - g.agent = request.headers.get("User-Agent") - if not g.agent and request.path != '/kofi': - abort(403, 'Please use a "User-Agent" header!') - if request.host != SITE: abort(403, "Unauthorized host provided!") @@ -24,6 +20,10 @@ def before_request(): if request.headers.get("CF-Worker"): abort(403, "Cloudflare workers are not allowed to access this website!") + g.agent = request.headers.get("User-Agent", "") + if not g.agent and request.path != '/kofi': + abort(403, 'Please use a "User-Agent" header!') + if not get_setting('bots') and request.headers.get("Authorization"): abort(403) diff --git a/files/routes/wrappers.py b/files/routes/wrappers.py index 2d888ce20..46648fc59 100644 --- a/files/routes/wrappers.py +++ b/files/routes/wrappers.py @@ -32,40 +32,39 @@ def calc_users(): g.loggedout_counter = 0 g.loggedin_chat = 0 v = getattr(g, 'v', None) if g else None + if has_request_context and g and g.desires_auth and not g.is_api_or_xhr: + loggedin = cache.get(LOGGED_IN_CACHE_KEY) or {} + loggedout = cache.get(LOGGED_OUT_CACHE_KEY) or {} + g.loggedin_chat = cache.get(CHAT_ONLINE_CACHE_KEY) or 0 + timestamp = int(time.time()) - loggedin = cache.get(LOGGED_IN_CACHE_KEY) or {} - loggedout = cache.get(LOGGED_OUT_CACHE_KEY) or {} - g.loggedin_chat = cache.get(CHAT_ONLINE_CACHE_KEY) or 0 - timestamp = int(time.time()) + if not session.get("session_id"): + session.permanent = True + session["session_id"] = str(uuid.uuid4()) - if not session.get("session_id"): - session.permanent = True - session["session_id"] = str(uuid.uuid4()) + if v: + if session["session_id"] in loggedout: del loggedout[session["session_id"]] + loggedin[v.id] = timestamp + else: + ua = str(user_agents.parse(g.agent)) + if 'spider' not in ua.lower() and 'bot' not in ua.lower(): + loggedout[session["session_id"]] = (timestamp, ua) - if v: - if session["session_id"] in loggedout: del loggedout[session["session_id"]] - loggedin[v.id] = timestamp - elif hasattr(g, 'agent') and g.agent: - ua = str(user_agents.parse(g.agent)) - if 'spider' not in ua.lower() and 'bot' not in ua.lower(): - loggedout[session["session_id"]] = (timestamp, ua) - - loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME} - loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME} - cache.set(LOGGED_IN_CACHE_KEY, loggedin) - cache.set(LOGGED_OUT_CACHE_KEY, loggedout) - g.loggedin_counter = len(loggedin) - g.loggedout_counter = len(loggedout) - - if g.loggedout_counter > 3000: - if not get_setting('ddos_detected'): - toggle_setting('ddos_detected') - set_security_level('under_attack') - else: - if get_setting('ddos_detected'): - toggle_setting('ddos_detected') - set_security_level('high') + loggedin = {k: v for k, v in loggedin.items() if (timestamp - v) < LOGGEDIN_ACTIVE_TIME} + loggedout = {k: v for k, v in loggedout.items() if (timestamp - v[0]) < LOGGEDIN_ACTIVE_TIME} + cache.set(LOGGED_IN_CACHE_KEY, loggedin) + cache.set(LOGGED_OUT_CACHE_KEY, loggedout) + g.loggedin_counter = len(loggedin) + g.loggedout_counter = len(loggedout) + if g.loggedout_counter > 3000: + if not get_setting('ddos_detected'): + toggle_setting('ddos_detected') + set_security_level('under_attack') + else: + if get_setting('ddos_detected'): + toggle_setting('ddos_detected') + set_security_level('high') return '' def get_logged_in_user(): diff --git a/files/templates/header.html b/files/templates/header.html index 88c23da90..9e0678acb 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -7,7 +7,7 @@
{{calc_users()}} {% if SITE_NAME == 'rDrama' %} - {% if (range(1,5) | random == 1 and not (v and v.truescore > 1000)) %} + {% if not hasattr(g, "loggedin_counter") or (range(1,5) | random == 1 and not (v and v.truescore > 1000)) %} {% include "journoid_banner.html" %} {% else %} {% if IS_DKD() %} @@ -69,6 +69,8 @@ {% endif %} {% elif hasattr(g, "loggedin_counter") %} {{g.loggedin_counter+g.loggedout_counter}} people here now ({{g.loggedin_counter}} logged in) + {% else %} + Don't make the mistakes seen here. {% endif %}