2022-11-29 23:50:32 +00:00
|
|
|
import secrets
|
2022-12-05 14:22:19 +00:00
|
|
|
import user_agents
|
2022-11-29 23:50:32 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.const import *
|
|
|
|
from files.helpers.settings import get_setting
|
|
|
|
from files.helpers.cloudflare import CLOUDFLARE_AVAILABLE
|
|
|
|
from files.routes.wrappers import *
|
2022-11-30 01:29:06 +00:00
|
|
|
from files.__main__ import app, limiter
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-11-29 23:50:32 +00:00
|
|
|
def session_init():
|
|
|
|
if not session.get("session_id"):
|
|
|
|
session.permanent = True
|
|
|
|
session["session_id"] = secrets.token_hex(49)
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
2022-11-29 23:50:32 +00:00
|
|
|
g.desires_auth = False
|
2022-11-15 09:19:08 +00:00
|
|
|
if SITE == 'marsey.world' and request.path != '/kofi':
|
|
|
|
abort(404)
|
|
|
|
|
2022-11-30 01:31:28 +00:00
|
|
|
g.agent = request.headers.get("User-Agent", "")
|
2022-11-15 09:19:08 +00:00
|
|
|
if not g.agent and request.path != '/kofi':
|
|
|
|
return 'Please use a "User-Agent" header!', 403
|
|
|
|
|
|
|
|
if request.host != SITE:
|
|
|
|
return {"error": "Unauthorized host provided"}, 403
|
|
|
|
|
|
|
|
if request.headers.get("CF-Worker"): return {"error": "Cloudflare workers are not allowed to access this website."}, 403
|
|
|
|
|
2022-11-30 18:26:07 +00:00
|
|
|
if not get_setting('bots') and request.headers.get("Authorization"): abort(403)
|
2022-11-15 09:19:08 +00:00
|
|
|
|
2022-12-05 14:22:19 +00:00
|
|
|
g.agent_parsed = str(user_agents.parse(g.agent))
|
|
|
|
|
|
|
|
if '; wv) ' in g.agent.lower():
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'webview'
|
2022-12-05 14:22:19 +00:00
|
|
|
elif g.agent_parsed.startswith('Firefox / '):
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'firefox'
|
2022-12-05 14:22:19 +00:00
|
|
|
elif any(g.agent_parsed.startswith(x) for x in ('iPhone', 'iPad', 'iPod', 'PC / Mac OS X')):
|
2022-12-04 19:02:22 +00:00
|
|
|
g.browser = 'apple'
|
|
|
|
else:
|
|
|
|
g.browser = 'chromium'
|
2022-11-20 23:31:26 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
g.is_tor = request.headers.get("cf-ipcountry") == "T1"
|
|
|
|
|
|
|
|
request.path = request.path.rstrip('/')
|
|
|
|
if not request.path: request.path = '/'
|
|
|
|
request.full_path = request.full_path.rstrip('?').rstrip('/')
|
|
|
|
if not request.full_path: request.full_path = '/'
|
|
|
|
|
2022-11-27 01:01:02 +00:00
|
|
|
session_init()
|
2022-11-30 01:29:06 +00:00
|
|
|
limiter.check()
|
|
|
|
g.db = db_session()
|
2022-11-22 23:37:55 +00:00
|
|
|
|
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
|
|
|
if response.status_code < 400:
|
2022-11-29 23:50:32 +00:00
|
|
|
if CLOUDFLARE_AVAILABLE and CLOUDFLARE_COOKIE_VALUE and g.desires_auth:
|
2022-11-15 09:19:08 +00:00
|
|
|
logged_in = bool(getattr(g, 'v', None))
|
2022-11-21 15:36:22 +00:00
|
|
|
response.set_cookie("lo", CLOUDFLARE_COOKIE_VALUE if logged_in else '',
|
|
|
|
max_age=60*60*24*365 if logged_in else 1, samesite="Lax")
|
2022-11-15 15:39:00 +00:00
|
|
|
if getattr(g, 'db', None):
|
|
|
|
g.db.commit()
|
|
|
|
g.db.close()
|
|
|
|
del g.db
|
2022-11-15 09:19:08 +00:00
|
|
|
return response
|
|
|
|
|
2022-11-22 23:37:55 +00:00
|
|
|
|
2022-11-15 09:19:08 +00:00
|
|
|
@app.teardown_appcontext
|
|
|
|
def teardown_request(error):
|
|
|
|
if getattr(g, 'db', None):
|
|
|
|
g.db.rollback()
|
|
|
|
g.db.close()
|
|
|
|
del g.db
|
|
|
|
stdout.flush()
|