rDrama/files/__main__.py

141 lines
4.4 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
2022-09-01 19:16:21 +00:00
if environ.get("MAIL_USERNAME2") and random.random() < 0.5:
2022-09-01 20:53:25 +00:00
app.config['MAIL_USERNAME'] = environ.get("MAIL_USERNAME2", "").strip()
app.config['MAIL_PASSWORD'] = environ.get("MAIL_PASSWORD2", "").strip()
2022-09-01 19:16:21 +00:00
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-09-13 16:53:19 +00:00
if not g.agent and request.path != '/kofi':
return 'Please use a "User-Agent" header!', 403
2022-05-26 20:53:24 +00:00
2022-09-13 16:53:19 +00:00
ua = g.agent or ''
ua = ua.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-09-06 20:31:31 +00:00
if request.host != 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
2022-09-13 16:53:19 +00:00
if not app.config['SETTINGS']['Bots'] and request.headers.get("Authorization"): abort(403)
2022-05-04 23:09:46 +00:00
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('/')
if not request.path: request.path = '/'
2022-09-04 23:37:39 +00:00
request.full_path = request.full_path.rstrip('?').rstrip('/')
if not request.full_path: request.full_path = '/'
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 *
[DO NOT MERGE] Chat restructure (#360) * Create new subdirectory for chat-related stuff * Gitignore * Have new code show up on chat * Have new code show up on chat * Fix config issue * More script stuff * Create UserInput components * More chat changes * More updates to chat * Add chat:watch script * Move up state and pass down * Match up existing functionality entirely * Match up existing functionality entirely * Send a message when hitting Enter * feature based directories * First crack at emoji drawer * Leave everything in a fucked up state ugh * Leave it in a better state * Stop for the night * Decouple by abstract chat functionality to provider * Wait a minute... * Small chat restructure * Prepare for notifications * Add root context * Flash number of messages * Read this and u die * Add quote functionality * Couple tweaks * Shallowenize the features dir/ * Add activity list * Ch-ch-ch-ch-ch-changes * Enable moving drawer * Hover style on activities * UserList changes * Add emoji processing logic * Duhhhh * Scroll to top when changing query * Put the emoji in the drawer * Improve emoji drawer * Add emoji genres * Do not show activities * Add feature flag technology * Fix issue where own messages were triggering notifications * Adjust startup scripts * Responsive part 1 * Styling changes for emoji genres * More emoji drawer styling * Add QuickEmojis * Re-add classnames * Set version * Modify build script * Modify build script * Mild renaming * Lots of styling changes * Leggo.
2022-09-24 03:49:40 +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()