import os import re from helpers import format_timespan from helpers import remote_ip import ratelimit # ------------------------------------------------------------------------------------------ # ------ token transfer limiter def token_limit_exceed(handler): write_json_response(handler, {'msg': 'You have reached the max amount of tokens for {}'.format(format_timespan(config.RATE_LIMIT_TOKEN_EXPIRE))}, 403) token_account_amount_limiter = ratelimit.RateLimitType( name = "token_account_amount", amount = config.RATE_LIMIT_TOKEN_AMOUNT, expire = config.RATE_LIMIT_TOKEN_EXPIRE, identity = lambda h: h.request.uri, on_exceed = token_limit_exceed) # ------------------------------------------------------------------------------------------ # ------ common functions def write_json_response(handler, msg, code=200): handler.set_status(code) handler.set_header('Content-Type', 'application/json; charset=UTF-8') handler.write(msg) def is_valid_account_name(account_name): return len(account_name) < 13 and len(account_name) > 0 and not re.search(r'[^a-z1-5\.]', account_name)
def token_limit_exceed(handler): write_json_response(handler, {'msg': 'reach 24 hours max token amount'}, 403) def account_limit_exceed(handler): write_json_response(handler, {'msg': 'reach 24 hours max account amount'}, 403) single_get_token_call_amount = 200 ip_24h_token_amount_limiter = ratelimit.RateLimitType( name="ip_24h_token_amount", amount=6000, # 24 hours amount expire=3600 * 24, # 24 hours identity=lambda h: h.request.remote_ip, on_exceed=token_limit_exceed) account_24h_token_amount_limiter = ratelimit.RateLimitType( name="account_24h_token_amount", amount=5, # 24 hours amount expire=3600 * 24, # 24 hours identity=lambda h: h.request.arguments.keys()[0] if len(h.request.arguments.keys()) == 1 else '', on_exceed=account_limit_exceed) # ------------------------------------------------------------------------------------------ # ------ common functions
import ratelimit # ------------------------------------------------------------------------------------------ # ------ token transfer limiter def token_limit_exceed(handler): write_json_response(handler, {'msg': 'reach 24 hours max token amount'}, 403) single_get_token_call_amount = 100 ip_24h_token_amount_limiter = ratelimit.RateLimitType( name="ip_24h_token_amount", amount=1000, # 24 hours amount expire=3600 * 24, # 24 hours identity=lambda h: h.request.remote_ip, on_exceed=token_limit_exceed) # ------------------------------------------------------------------------------------------ # ------ common functions def write_json_response(handler, msg, code=200): handler.set_status(code) handler.set_header('Content-Type', 'application/json; charset=UTF-8') handler.write(msg) def is_valid_account_name(account_name): return len(account_name) < 13 and len(account_name) > 0 and not re.search(