fix loggedout_counter

master
Aevann 2023-06-29 18:56:16 +03:00
parent 9519020de8
commit 512fe8d710
4 changed files with 49 additions and 51 deletions

View File

@ -1,10 +1,7 @@
import time
import secrets
from os import environ, listdir, path
import user_agents
from flask import g, session, has_request_context, request
from jinja2 import pass_context
@ -76,50 +73,6 @@ def selected_tab(request):
return 'home'
@app.context_processor
def calc_users():
loggedin_counter = 0
loggedout_counter = 0
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 {}
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"] = secrets.token_hex(49)
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)
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)
loggedin_counter = len(loggedin)
loggedout_counter = len(loggedout)
if loggedout_counter > 1000:
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 {'loggedin_counter':loggedin_counter,
'loggedout_counter':loggedout_counter,
'loggedin_chat':loggedin_chat}
def current_registered_users():
return "{:,}".format(g.db.query(User).count())

View File

@ -1,4 +1,6 @@
import time
import secrets
import user_agents
from flask import g, request, session
from files.classes.clients import ClientAuth
@ -24,6 +26,47 @@ def get_ID():
return f'{SITE}-{x}'
def calc_users():
g.loggedin_counter = 0
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())
if not session.get("session_id"):
session.permanent = True
session["session_id"] = secrets.token_hex(49)
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)
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 > 1000:
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')
print('fuck', flush=True)
def get_logged_in_user():
if hasattr(g, 'v') and g.v: return g.v
if not hasattr(g, 'db'): g.db = db_session()
@ -78,6 +121,8 @@ def get_logged_in_user():
g.is_api_or_xhr = bool((v and v.client) or request.headers.get("xhr"))
calc_users()
return v
def auth_desired(f):

View File

@ -37,10 +37,10 @@
' Carp alts currently online',
] -%}
{% endif %}
{{loggedin_counter+loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{loggedin_counter}} logged in)
{{g.loggedin_counter+g.loggedout_counter}} {{VISITORS_HERE_FLAVOR|random|safe}} ({{g.loggedin_counter}} logged in)
{% endif %}
{% else %}
{{loggedin_counter+loggedout_counter}} people here now ({{loggedin_counter}} logged in)
{{g.loggedin_counter+g.loggedout_counter}} people here now ({{g.loggedin_counter}} logged in)
{% endif %}
</div>
{% else %}
@ -178,7 +178,7 @@
<a class="nav-link position-relative" href="/chat">
<i class="fas fa-messages" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Chat"></i>
<b id="chat-count-header-bar" class="text-lg" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">
{{loggedin_chat}}
{{g.loggedin_chat}}
</b>
</a>
</li>

View File

@ -50,7 +50,7 @@
<button type="button" class="nobackground border-0 col px-0 btn btn-dead m-0 pt-0">
<a href="/chat" class="text-decoration-none">
<div class="text-center {% if request|selected_tab=='chat' %}text-primary{% else %}text-muted{% endif %}">
<b class="text-lg" style="padding-top:10px" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">{{loggedin_chat}}</b>
<b class="text-lg" style="padding-top:10px" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Users in chat right now">{{g.loggedin_chat}}</b>
<div class="text-small-extra">Chat</div>
</div>
</a>