rDrama/files/__main__.py

136 lines
4.2 KiB
Python
Raw Normal View History

2022-05-04 23:09:46 +00:00
import gevent.monkey
gevent.monkey.patch_all()
from os import environ, path
import secrets
from flask import *
from flask_caching import Cache
from flask_limiter import Limiter
from flask_compress import Compress
from flask_mail import Mail
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import *
import gevent
import redis
import time
from sys import stdout, argv
import faulthandler
import json
2022-08-05 17:22:17 +00:00
import random
2022-05-04 23:09:46 +00:00
app = Flask(__name__, template_folder='templates')
app.url_map.strict_slashes = False
app.jinja_env.cache = {}
app.jinja_env.auto_reload = True
2022-07-09 10:38:45 +00:00
app.jinja_env.add_extension('jinja2.ext.do')
2022-05-04 23:09:46 +00:00
faulthandler.enable()
app.config['SECRET_KEY'] = environ.get('MASTER_KEY')
app.config["SERVER_NAME"] = environ.get("DOMAIN").strip()
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3153600
app.config["SESSION_COOKIE_NAME"] = "session_" + environ.get("SITE_NAME").strip().lower()
2022-05-07 06:04:14 +00:00
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
2022-05-04 23:09:46 +00:00
app.config["SESSION_COOKIE_SECURE"] = True
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
2022-05-27 21:28:10 +00:00
app.config["PERMANENT_SESSION_LIFETIME"] = 60 * 60 * 24 * 365
2022-05-19 17:58:18 +00:00
app.config['SESSION_REFRESH_EACH_REQUEST'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URL'] = environ.get("DATABASE_URL", "postgresql://postgres@localhost:5432")
2022-05-04 23:09:46 +00:00
app.config["CACHE_TYPE"] = "RedisCache"
app.config["CACHE_REDIS_URL"] = environ.get("REDIS_URL", "redis://localhost")
2022-05-04 23:09:46 +00:00
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
2022-08-05 17:22:17 +00:00
if environ.get("MAIL_USERNAME2") and random.random() < 0.5:
app.config['MAIL_USERNAME'] = environ.get("MAIL_USERNAME2", "").strip()
app.config['MAIL_PASSWORD'] = environ.get("MAIL_PASSWORD2", "").strip()
else:
app.config['MAIL_USERNAME'] = environ.get("MAIL_USERNAME", "").strip()
app.config['MAIL_PASSWORD'] = environ.get("MAIL_PASSWORD", "").strip()
app.config['SETTINGS'] = {}
2022-05-04 23:09:46 +00:00
r=redis.Redis(host=environ.get("REDIS_URL", "redis://localhost"), decode_responses=True, ssl_cert_reqs=None)
def get_CF():
with app.app_context():
return request.headers.get('CF-Connecting-IP')
limiter = Limiter(
app,
key_func=get_CF,
default_limits=["3/second;30/minute;200/hour;1000/day"],
application_limits=["10/second;200/minute;5000/hour;10000/day"],
storage_uri=environ.get("REDIS_URL", "redis://localhost")
)
Base = declarative_base()
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URL'])
2022-05-04 23:09:46 +00:00
db_session = scoped_session(sessionmaker(bind=engine, autoflush=False))
2022-05-04 23:09:46 +00:00
cache = Cache(app)
Compress(app)
mail = Mail(app)
if not path.isfile(f'/site_settings.json'):
with open('/site_settings.json', 'w', encoding='utf_8') as f:
f.write(
'{"Bots": true, "Fart mode": false, "Read-only mode": false, ' + \
'"Signups": true, "login_required": false}')
2022-05-04 23:09:46 +00:00
@app.before_request
def before_request():
2022-05-26 20:49:36 +00:00
2022-05-26 20:53:24 +00:00
g.agent = request.headers.get("User-Agent")
2022-05-26 21:16:49 +00:00
if not g.agent: return 'Please use a "User-Agent" header!', 403
2022-05-26 20:53:24 +00:00
ua = g.agent.lower()
2022-05-26 20:49:36 +00:00
with open('/site_settings.json', 'r', encoding='utf_8') as f:
app.config['SETTINGS'] = json.load(f)
2022-05-04 23:09:46 +00:00
2022-07-13 18:14:37 +00:00
if SITE != app.config["SERVER_NAME"]: return {"error":"Unauthorized host provided."}, 401
2022-05-04 23:09:46 +00:00
if request.headers.get("CF-Worker"): return {"error":"Cloudflare workers are not allowed to access this website."}, 401
if not app.config['SETTINGS']['Bots'] and request.headers.get("Authorization"): abort(503)
g.db = db_session()
2022-05-29 01:55:36 +00:00
g.webview = '; wv) ' in ua
g.inferior_browser = 'iphone' in ua or 'ipad' in ua or 'ipod' in ua or 'mac os' in ua or ' firefox/' in ua
2022-05-04 23:09:46 +00:00
2022-08-01 18:03:29 +00:00
request.path = request.path.rstrip('/')
request.full_path = request.full_path.rstrip('?').rstrip('/')
2022-07-08 18:06:54 +00:00
2022-05-04 23:09:46 +00:00
@app.after_request
def after_request(response):
response.headers.add("Strict-Transport-Security", "max-age=31536000")
response.headers.add("X-Frame-Options", "deny")
2022-06-27 05:24:11 +00:00
if response.status_code < 400:
g.db.commit()
g.db.close()
del g.db
2022-05-04 23:09:46 +00:00
return response
@app.teardown_appcontext
def teardown_request(error):
if hasattr(g, 'db') and g.db:
2022-06-27 05:24:11 +00:00
g.db.rollback()
g.db.close()
2022-06-27 05:24:11 +00:00
del g.db
stdout.flush()
2022-05-04 23:09:46 +00:00
if app.config["SERVER_NAME"] == 'localhost':
from files.routes import *
2022-08-26 15:37:06 +00:00
from files.routes.chat import *
2022-05-04 23:09:46 +00:00
elif "load_chat" in argv:
from files.routes.chat import *
else:
2022-05-29 01:55:36 +00:00
from files.routes import *
2022-07-17 19:17:46 +00:00
stdout.flush()