2017-03-25 09:38:06 +01:00
|
|
|
from titanembeds.discordrest import DiscordREST
|
2017-03-26 05:31:47 +02:00
|
|
|
from flask import request, session
|
|
|
|
from flask.ext.cache import Cache
|
2017-03-27 01:37:27 +02:00
|
|
|
from flask_limiter import Limiter
|
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-03-25 09:38:06 +01:00
|
|
|
|
|
|
|
discord_api = DiscordREST(config['bot-token'])
|
2017-03-26 05:31:47 +02:00
|
|
|
cache = Cache()
|
|
|
|
|
|
|
|
def get_client_ipaddr():
|
|
|
|
if hasattr(request.headers, "X-Real-IP"): # pythonanywhere specific
|
|
|
|
return request.headers['X-Real-IP']
|
|
|
|
else: # general
|
|
|
|
return request.remote_addr
|
|
|
|
|
|
|
|
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:
|
|
|
|
rand_str = lambda n: ''.join([random.choice(string.lowercase) for i in xrange(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()
|
|
|
|
return (path + args + sess + ip).encode('utf-8')
|
|
|
|
|
|
|
|
def make_guilds_cache_key():
|
|
|
|
sess = generate_session_key()
|
|
|
|
ip = get_client_ipaddr()
|
|
|
|
return (sess + ip + "user_guilds").encode('utf-8')
|
2017-03-27 01:37:27 +02:00
|
|
|
|
2017-03-27 04:46:06 +02:00
|
|
|
def make_guildchannels_cache_key():
|
|
|
|
guild_id = request.args.get('guild_id', "0")
|
|
|
|
sess = generate_session_key()
|
|
|
|
ip = get_client_ipaddr()
|
|
|
|
return (sess + ip + guild_id + "user_guild_channels").encode('utf-8')
|
|
|
|
|
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()
|
|
|
|
channel_id = request.args.get('channel_id', "0")
|
|
|
|
return (sess + channel_id).encode('utf-8')
|
|
|
|
|
|
|
|
def guild_ratelimit_key():
|
|
|
|
sess = generate_session_key()
|
|
|
|
guild_id = request.args.get('guild_id', "0")
|
|
|
|
return (sess + guild_id).encode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
rate_limiter = Limiter(key_func=get_client_ipaddr) # Default limit by ip address
|