2017-09-11 22:33:20 +02:00
|
|
|
from titanembeds.database import db, Guilds
|
2017-03-26 05:31:47 +02:00
|
|
|
from flask import request, session
|
2017-03-27 01:37:27 +02:00
|
|
|
from flask_limiter import Limiter
|
2017-08-19 05:52:32 +02:00
|
|
|
from flask_socketio import SocketIO
|
2017-03-25 09:38:06 +01:00
|
|
|
from config import config
|
2017-03-26 05:31:47 +02:00
|
|
|
import random
|
|
|
|
import string
|
2017-04-12 09:36:05 +02:00
|
|
|
import hashlib
|
2017-06-13 03:39:49 +02:00
|
|
|
import time
|
2017-03-25 09:38:06 +01:00
|
|
|
|
2017-04-22 06:18:14 +02:00
|
|
|
from titanembeds.discordrest import DiscordREST
|
|
|
|
|
2017-03-25 09:38:06 +01:00
|
|
|
discord_api = DiscordREST(config['bot-token'])
|
2017-03-26 05:31:47 +02:00
|
|
|
|
|
|
|
def get_client_ipaddr():
|
2017-09-13 23:48:31 +02:00
|
|
|
ip = request.remote_addr
|
2017-08-19 02:33:50 +02:00
|
|
|
return hashlib.sha512((config['app-secret'] + ip).encode('utf-8')).hexdigest()[:15]
|
2017-03-26 05:31:47 +02:00
|
|
|
|
|
|
|
def generate_session_key():
|
2017-03-27 01:37:27 +02:00
|
|
|
sess = session.get("sessionunique", None)
|
2017-03-26 05:31:47 +02:00
|
|
|
if not sess:
|
2017-08-19 02:33:50 +02:00
|
|
|
rand_str = lambda n: ''.join([random.choice(string.ascii_lowercase) for i in range(n)])
|
2017-03-27 01:37:27 +02:00
|
|
|
session['sessionunique'] = rand_str(25)
|
|
|
|
sess = session['sessionunique']
|
2017-03-26 05:31:47 +02:00
|
|
|
return sess #Totally unique
|
|
|
|
|
|
|
|
def make_cache_key(*args, **kwargs):
|
|
|
|
path = request.path
|
|
|
|
args = str(hash(frozenset(request.args.items())))
|
|
|
|
ip = get_client_ipaddr()
|
|
|
|
sess = generate_session_key()
|
2017-08-19 02:33:50 +02:00
|
|
|
return (path + args + sess + ip)
|
2017-03-26 05:31:47 +02:00
|
|
|
|
2017-05-09 06:47:07 +02:00
|
|
|
def make_user_cache_key(*args, **kwargs):
|
2017-05-09 06:44:05 +02:00
|
|
|
ip = get_client_ipaddr()
|
|
|
|
sess = generate_session_key()
|
2017-08-19 02:33:50 +02:00
|
|
|
return (sess + ip)
|
2017-05-09 06:44:05 +02:00
|
|
|
|
2017-03-26 05:31:47 +02:00
|
|
|
def make_guilds_cache_key():
|
|
|
|
sess = generate_session_key()
|
|
|
|
ip = get_client_ipaddr()
|
2017-08-19 02:33:50 +02:00
|
|
|
return (sess + ip + "user_guilds")
|
2017-03-27 01:37:27 +02:00
|
|
|
|
2017-03-27 04:46:06 +02:00
|
|
|
def make_guildchannels_cache_key():
|
2017-04-08 02:02:39 +02:00
|
|
|
guild_id = request.values.get('guild_id', "0")
|
2017-03-27 04:46:06 +02:00
|
|
|
sess = generate_session_key()
|
|
|
|
ip = get_client_ipaddr()
|
2017-08-19 02:33:50 +02:00
|
|
|
return (sess + ip + guild_id + "user_guild_channels")
|
2017-03-27 04:46:06 +02:00
|
|
|
|
2017-03-27 01:37:27 +02:00
|
|
|
def channel_ratelimit_key(): # Generate a bucket with given channel & unique session key
|
|
|
|
sess = generate_session_key()
|
2017-04-08 02:02:39 +02:00
|
|
|
channel_id = request.values.get('channel_id', "0")
|
2017-08-19 02:33:50 +02:00
|
|
|
return (sess + channel_id)
|
2017-03-27 01:37:27 +02:00
|
|
|
|
|
|
|
def guild_ratelimit_key():
|
2017-04-08 02:29:37 +02:00
|
|
|
ip = get_client_ipaddr()
|
2017-04-08 02:02:39 +02:00
|
|
|
guild_id = request.values.get('guild_id', "0")
|
2017-08-19 02:33:50 +02:00
|
|
|
return (ip + guild_id)
|
2017-03-27 01:37:27 +02:00
|
|
|
|
2017-04-04 07:53:27 +02:00
|
|
|
def check_guild_existance(guild_id):
|
|
|
|
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first()
|
|
|
|
if not dbGuild:
|
|
|
|
return False
|
2017-05-07 03:36:45 +02:00
|
|
|
else:
|
|
|
|
return True
|
2017-03-27 01:37:27 +02:00
|
|
|
|
2017-06-09 06:22:33 +02:00
|
|
|
def guild_accepts_visitors(guild_id):
|
|
|
|
dbGuild = Guilds.query.filter_by(guild_id=guild_id).first()
|
|
|
|
return dbGuild.visitor_view
|
|
|
|
|
2017-04-04 07:53:27 +02:00
|
|
|
def guild_query_unauth_users_bool(guild_id):
|
|
|
|
dbGuild = db.session.query(Guilds).filter(Guilds.guild_id==guild_id).first()
|
|
|
|
return dbGuild.unauth_users
|
2017-04-08 02:02:39 +02:00
|
|
|
|
|
|
|
rate_limiter = Limiter(key_func=get_client_ipaddr) # Default limit by ip address
|
2017-08-20 21:56:54 +02:00
|
|
|
socketio = SocketIO()
|