2022-03-22 14:09:02 +00:00
import gevent . monkey
gevent . monkey . patch_all ( )
2022-02-05 10:17:08 +00:00
from os import environ , path
2021-10-15 14:08:27 +00:00
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
2022-01-04 21:11:29 +00:00
import time
2022-03-24 19:44:12 +00:00
from sys import stdout , argv
2022-01-14 02:33:27 +00:00
import faulthandler
2022-04-12 19:05:50 +00:00
import json
2022-02-05 10:17:08 +00:00
2021-12-23 19:14:52 +00:00
app = Flask ( __name__ , template_folder = ' templates ' )
2021-10-15 14:08:27 +00:00
app . url_map . strict_slashes = False
app . jinja_env . cache = { }
2021-10-26 22:59:34 +00:00
app . jinja_env . auto_reload = True
2021-10-15 14:08:27 +00:00
faulthandler . enable ( )
2021-12-21 22:28:31 +00:00
2022-04-25 16:38:06 +00:00
app . config [ " SITE_NAME " ] = environ . get ( " SITE_NAME " )
app . config [ " GUMROAD_LINK " ] = environ . get ( " GUMROAD_LINK " , " https://marsey1.gumroad.com/l/tfcvri " )
2021-10-15 14:08:27 +00:00
app . config [ ' SQLALCHEMY_TRACK_MODIFICATIONS ' ] = False
2022-02-11 23:32:14 +00:00
app . config [ ' DATABASE_URL ' ] = environ . get ( " DATABASE_URL " , " postgresql://postgres@localhost:5432 " )
2021-10-15 14:08:27 +00:00
app . config [ ' SECRET_KEY ' ] = environ . get ( ' MASTER_KEY ' )
2022-04-25 21:57:37 +00:00
app . config [ " SERVER_NAME " ] = environ . get ( " DOMAIN " ) . strip ( )
2022-02-22 09:15:45 +00:00
app . config [ ' SEND_FILE_MAX_AGE_DEFAULT ' ] = 3153600
2022-04-25 16:38:06 +00:00
app . config [ " SESSION_COOKIE_NAME " ] = " session_ " + environ . get ( " SITE_NAME " ) . lower ( )
2021-10-15 14:08:27 +00:00
app . config [ " VERSION " ] = " 1.0.0 "
2022-01-24 18:19:22 +00:00
app . config [ ' MAX_CONTENT_LENGTH ' ] = 8 * 1024 * 1024
2021-12-23 19:14:52 +00:00
app . config [ " SESSION_COOKIE_SECURE " ] = True
2021-10-15 14:08:27 +00:00
app . config [ " SESSION_COOKIE_SAMESITE " ] = " Lax "
app . config [ " PERMANENT_SESSION_LIFETIME " ] = 60 * 60 * 24 * 365
2022-04-25 16:38:06 +00:00
app . config [ " DEFAULT_COLOR " ] = environ . get ( " DEFAULT_COLOR " , " ff0000 " )
app . config [ " DEFAULT_THEME " ] = environ . get ( " DEFAULT_THEME " , " midnight " )
2021-12-23 19:14:52 +00:00
app . config [ " FORCE_HTTPS " ] = 1
2021-10-15 14:08:27 +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 "
2022-04-25 16:38:06 +00:00
app . config [ " HCAPTCHA_SITEKEY " ] = environ . get ( " HCAPTCHA_SITEKEY " , " " )
app . config [ " HCAPTCHA_SECRET " ] = environ . get ( " HCAPTCHA_SECRET " , " " )
2021-10-15 14:08:27 +00:00
app . config [ " SPAM_SIMILARITY_THRESHOLD " ] = float ( environ . get ( " SPAM_SIMILARITY_THRESHOLD " , 0.5 ) )
2021-12-29 06:43:20 +00:00
app . config [ " SPAM_URL_SIMILARITY_THRESHOLD " ] = float ( environ . get ( " SPAM_URL_SIMILARITY_THRESHOLD " , 0.1 ) )
app . config [ " SPAM_SIMILAR_COUNT_THRESHOLD " ] = int ( environ . get ( " SPAM_SIMILAR_COUNT_THRESHOLD " , 10 ) )
2021-10-15 14:08:27 +00:00
app . config [ " COMMENT_SPAM_SIMILAR_THRESHOLD " ] = float ( environ . get ( " COMMENT_SPAM_SIMILAR_THRESHOLD " , 0.5 ) )
2021-12-29 06:43:20 +00:00
app . config [ " COMMENT_SPAM_COUNT_THRESHOLD " ] = int ( environ . get ( " COMMENT_SPAM_COUNT_THRESHOLD " , 10 ) )
2022-01-22 19:49:12 +00:00
app . config [ " CACHE_TYPE " ] = " RedisCache "
app . config [ " CACHE_REDIS_URL " ] = environ . get ( " REDIS_URL " , " redis://localhost " )
2021-10-15 14:08:27 +00:00
app . config [ ' MAIL_SERVER ' ] = ' smtp.gmail.com '
app . config [ ' MAIL_PORT ' ] = 587
app . config [ ' MAIL_USE_TLS ' ] = True
2022-04-25 16:38:06 +00:00
app . config [ ' MAIL_USERNAME ' ] = environ . get ( " MAIL_USERNAME " , " " )
app . config [ ' MAIL_PASSWORD ' ] = environ . get ( " MAIL_PASSWORD " , " " )
app . config [ ' DESCRIPTION ' ] = environ . get ( " DESCRIPTION " , " rdrama.net caters to drama in all forms such as: Real life, videos, photos, gossip, rumors, news sites, Reddit, and Beyondâ„¢. There isn ' t drama we won ' t touch, and we want it all! " )
2022-04-12 19:05:50 +00:00
app . config [ ' SETTINGS ' ] = { }
2021-10-15 14:08:27 +00:00
2021-12-24 03:22:00 +00:00
r = redis . Redis ( host = environ . get ( " REDIS_URL " , " redis://localhost " ) , decode_responses = True , ssl_cert_reqs = None )
2021-10-15 14:08:27 +00:00
2022-01-23 23:06:34 +00:00
def get_CF ( ) :
2022-01-21 11:27:10 +00:00
with app . app_context ( ) :
return request . headers . get ( ' CF-Connecting-IP ' )
2021-10-15 14:08:27 +00:00
limiter = Limiter (
app ,
2022-01-21 11:27:10 +00:00
key_func = get_CF ,
2022-01-13 02:35:00 +00:00
default_limits = [ " 3/second;30/minute;200/hour;1000/day " ] ,
2022-02-04 04:21:47 +00:00
application_limits = [ " 10/second;200/minute;5000/hour;10000/day " ] ,
2022-01-15 06:57:59 +00:00
storage_uri = environ . get ( " REDIS_URL " , " redis://localhost " )
2021-10-15 14:08:27 +00:00
)
Base = declarative_base ( )
engine = create_engine ( app . config [ ' DATABASE_URL ' ] )
db_session = scoped_session ( sessionmaker ( bind = engine , autoflush = False ) )
cache = Cache ( app )
Compress ( app )
mail = Mail ( app )
@app.before_request
def before_request ( ) :
2022-04-12 19:05:50 +00:00
with open ( ' site_settings.json ' , ' r ' ) as f :
app . config [ ' SETTINGS ' ] = json . load ( f )
2022-04-05 16:53:32 +00:00
if request . host != app . config [ " SERVER_NAME " ] : return { " error " : " Unauthorized host provided. " } , 401
2022-04-05 16:49:05 +00:00
if request . headers . get ( " CF-Worker " ) : return { " error " : " Cloudflare workers are not allowed to access this website. " } , 401
2022-04-05 16:29:27 +00:00
2022-04-12 19:05:50 +00:00
if not app . config [ ' SETTINGS ' ] [ ' Bots ' ] and request . headers . get ( " Authorization " ) : abort ( 503 )
2021-10-15 14:08:27 +00:00
g . db = db_session ( )
2022-01-22 15:55:34 +00:00
ua = request . headers . get ( " User-Agent " , " " ) . lower ( )
if ' ; wv) ' in ua : g . webview = True
2021-12-29 08:29:22 +00:00
else : g . webview = False
2022-02-17 03:03:03 +00:00
if ' iphone ' in ua or ' ipad ' in ua or ' ipod ' in ua or ' mac os ' in ua or ' firefox/ ' in ua : g . inferior_browser = True
else : g . inferior_browser = False
2022-01-24 21:44:31 +00:00
2022-04-07 01:18:46 +00:00
g . timestamp = int ( time . time ( ) )
2022-01-22 15:55:34 +00:00
2021-10-15 14:08:27 +00:00
@app.teardown_appcontext
def teardown_request ( error ) :
if hasattr ( g , ' db ' ) and g . db :
g . db . close ( )
2022-01-14 02:33:27 +00:00
stdout . flush ( )
2021-10-15 14:08:27 +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 " )
return response
2022-04-06 23:03:18 +00:00
if " load_chat " in argv :
from files . routes . chat import *
else :
from files . routes import *