MarseyWorld/files/__main__.py

128 lines
5.5 KiB
Python
Raw Normal View History

2021-07-21 01:12:26 +00:00
import gevent.monkey
gevent.monkey.patch_all()
2021-08-19 15:56:18 +00:00
from os import environ
2021-07-21 01:12:26 +00:00
import secrets
from flask import *
2021-09-20 14:29:13 +00:00
from flask_caching import Cache
2021-08-19 15:51:17 +00:00
from flask_limiter import Limiter
2021-07-21 01:12:26 +00:00
from flask_compress import Compress
2021-08-19 15:51:17 +00:00
from flask_limiter.util import get_ipaddr
2021-07-21 01:12:26 +00:00
from flaskext.markdown import Markdown
2021-09-14 13:53:01 +00:00
from sqlalchemy.ext.declarative import declarative_base
2021-09-20 19:43:50 +00:00
from sqlalchemy.orm import sessionmaker, scoped_session
2021-07-21 01:12:26 +00:00
from sqlalchemy import *
import gevent
from werkzeug.middleware.proxy_fix import ProxyFix
2021-09-20 14:27:38 +00:00
import redis
2021-07-21 01:12:26 +00:00
2021-09-17 11:14:45 +00:00
app = Flask(__name__, template_folder='./templates')
2021-07-21 01:12:26 +00:00
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=3)
app.url_map.strict_slashes = False
2021-09-14 13:50:04 +00:00
app.jinja_env.cache = {}
2021-09-21 19:06:02 +00:00
import faulthandler
faulthandler.enable()
2021-07-21 01:12:26 +00:00
2021-08-04 16:00:57 +00:00
app.config["SITE_NAME"]=environ.get("SITE_NAME").strip()
2021-08-05 14:43:54 +00:00
app.config["COINS_NAME"]=environ.get("COINS_NAME").strip()
2021-08-26 11:03:07 +00:00
app.config["GUMROAD_LINK"]=environ.get("GUMROAD_LINK", "https://marsey1.gumroad.com/l/tfcvri").strip()
2021-07-21 01:12:26 +00:00
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2021-09-15 17:07:03 +00:00
app.config['DATABASE_URL'] = environ.get("DATABASE_URL")
2021-07-21 01:12:26 +00:00
app.config['SECRET_KEY'] = environ.get('MASTER_KEY')
2021-08-05 14:41:32 +00:00
app.config["SERVER_NAME"] = environ.get("DOMAIN").strip()
2021-08-26 11:16:02 +00:00
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 86400
2021-08-04 16:00:57 +00:00
app.config["SESSION_COOKIE_NAME"] = "session_" + environ.get("SITE_NAME").strip().lower()
2021-07-25 21:59:20 +00:00
app.config["VERSION"] = "1.0.0"
2021-07-21 01:12:26 +00:00
app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024
app.config["SESSION_COOKIE_SECURE"] = bool(int(environ.get("FORCE_HTTPS", 1)))
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
app.config["PERMANENT_SESSION_LIFETIME"] = 60 * 60 * 24 * 365
app.config["SESSION_REFRESH_EACH_REQUEST"] = True
app.config["SLOGAN"] = environ.get("SLOGAN", "").strip()
app.config["DEFAULT_COLOR"] = environ.get("DEFAULT_COLOR", "ff0000").strip()
app.config["DEFAULT_THEME"] = environ.get("DEFAULT_THEME", "light").strip() + "_" + environ.get("DEFAULT_COLOR", "ff0000").strip()
2021-07-21 01:12:26 +00:00
app.config["FORCE_HTTPS"] = int(environ.get("FORCE_HTTPS", 1)) if ("localhost" not in app.config["SERVER_NAME"] and "127.0.0.1" not in app.config["SERVER_NAME"]) else 0
2021-07-25 21:59:20 +00:00
app.config["UserAgent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
2021-07-21 01:12:26 +00:00
app.config["HCAPTCHA_SITEKEY"] = environ.get("HCAPTCHA_SITEKEY","").strip()
2021-08-04 16:00:57 +00:00
app.config["HCAPTCHA_SECRET"] = environ.get("HCAPTCHA_SECRET","").strip()
2021-08-11 15:15:51 +00:00
app.config["SPAM_SIMILARITY_THRESHOLD"] = float(environ.get("SPAM_SIMILARITY_THRESHOLD", 0.5))
app.config["SPAM_SIMILAR_COUNT_THRESHOLD"] = int(environ.get("SPAM_SIMILAR_COUNT_THRESHOLD", 5))
app.config["SPAM_URL_SIMILARITY_THRESHOLD"] = float(environ.get("SPAM_URL_SIMILARITY_THRESHOLD", 0.5))
app.config["COMMENT_SPAM_SIMILAR_THRESHOLD"] = float(environ.get("COMMENT_SPAM_SIMILAR_THRESHOLD", 0.5))
app.config["COMMENT_SPAM_COUNT_THRESHOLD"] = int(environ.get("COMMENT_SPAM_COUNT_THRESHOLD", 0.5))
2021-09-05 16:50:17 +00:00
app.config["VIDEO_COIN_REQUIREMENT"] = int(environ.get("VIDEO_COIN_REQUIREMENT", 0))
2021-08-11 15:15:51 +00:00
app.config["READ_ONLY"]=bool(int(environ.get("READ_ONLY", "0")))
2021-07-21 01:12:26 +00:00
app.config["BOT_DISABLE"]=bool(int(environ.get("BOT_DISABLE", False)))
app.config["RATELIMIT_KEY_PREFIX"] = "flask_limiting_"
app.config["RATELIMIT_ENABLED"] = True
app.config["RATELIMIT_DEFAULTS_DEDUCT_WHEN"]=lambda:True
app.config["RATELIMIT_DEFAULTS_EXEMPT_WHEN"]=lambda:False
app.config["RATELIMIT_HEADERS_ENABLED"]=True
2021-09-20 14:27:38 +00:00
app.config["CACHE_TYPE"] = "filesystem"
2021-09-20 14:29:46 +00:00
app.config["CACHE_DIR"] = "cache"
2021-09-21 18:06:41 +00:00
app.config["RATELIMIT_STORAGE_URL"] = environ.get("REDIS_URL", "redis://127.0.0.1")
2021-07-21 01:12:26 +00:00
2021-09-21 18:06:41 +00:00
r=redis.Redis(host=environ.get("REDIS_URL", "redis://127.0.0.1"), decode_responses=True, ssl_cert_reqs=None)
2021-09-14 13:50:04 +00:00
2021-09-20 14:29:13 +00:00
cache = Cache(app)
2021-09-20 14:29:46 +00:00
Markdown(app)
2021-09-14 13:50:04 +00:00
Compress(app)
2021-07-26 14:44:52 +00:00
2021-08-19 15:51:17 +00:00
limiter = Limiter(
2021-07-21 01:12:26 +00:00
app,
2021-08-19 15:51:17 +00:00
key_func=get_ipaddr,
2021-09-04 01:28:50 +00:00
default_limits=["100/minute"],
2021-07-21 01:12:26 +00:00
headers_enabled=True,
strategy="fixed-window"
)
2021-09-14 13:53:01 +00:00
Base = declarative_base()
2021-09-20 19:46:09 +00:00
engine = create_engine(app.config['DATABASE_URL'])
2021-09-21 21:27:32 +00:00
db_session = scoped_session(sessionmaker(bind=engine, autoflush=False))
2021-07-21 01:12:26 +00:00
2021-09-16 13:18:09 +00:00
2021-07-21 01:12:26 +00:00
@app.before_request
def before_request():
2021-09-16 13:18:09 +00:00
if request.method.lower() != "get" and app.config["READ_ONLY"]: return {"error":f"{app.config['SITE_NAME']} is currently in read-only mode."}, 500
2021-07-21 01:12:26 +00:00
2021-09-16 13:18:09 +00:00
if app.config["BOT_DISABLE"] and request.headers.get("X-User-Type")=="Bot": abort(503)
2021-07-21 01:12:26 +00:00
2021-09-15 16:59:51 +00:00
g.db = db_session()
2021-07-21 01:12:26 +00:00
g.timestamp = int(time.time())
#do not access session for static files
if not request.path.startswith("/assets"):
session.permanent = True
2021-09-16 13:18:09 +00:00
if not session.get("session_id"): session["session_id"] = secrets.token_hex(16)
2021-09-16 13:18:09 +00:00
if app.config["FORCE_HTTPS"] and request.url.startswith("http://") and "localhost" not in app.config["SERVER_NAME"]:
2021-07-21 01:12:26 +00:00
url = request.url.replace("http://", "https://", 1)
return redirect(url, code=301)
ua=request.headers.get("User-Agent","")
2021-09-16 13:18:09 +00:00
if "CriOS/" in ua: g.system="ios/chrome"
elif "Version/" in ua: g.system="android/webview"
elif "Mobile Safari/" in ua: g.system="android/chrome"
elif "Safari/" in ua: g.system="ios/safari"
elif "Mobile/" in ua: g.system="ios/webview"
else: g.system="other/other"
2021-07-21 01:12:26 +00:00
2021-09-16 08:10:57 +00:00
@app.teardown_appcontext
2021-09-16 08:22:21 +00:00
def teardown_request(error):
2021-09-15 05:51:05 +00:00
if hasattr(g, 'db') and g.db:
2021-09-13 11:18:19 +00:00
g.db.close()
2021-09-16 08:10:57 +00:00
@app.after_request
def after_request(response):
2021-07-21 01:12:26 +00:00
response.headers.add("Strict-Transport-Security", "max-age=31536000")
response.headers.add("Referrer-Policy", "same-origin")
response.headers.add("Feature-Policy", "geolocation 'none'; midi 'none'; notifications 'none'; push 'none'; sync-xhr 'none'; microphone 'none'; camera 'none'; magnetometer 'none'; gyroscope 'none'; vibrate 'none'; fullscreen 'none'; payment 'none';")
2021-08-13 21:25:16 +00:00
response.headers.add("X-Frame-Options", "deny")
2021-09-14 13:53:01 +00:00
return response
2021-09-16 13:18:09 +00:00
2021-09-14 13:53:01 +00:00
from files.routes import *