Parse the network configuration only once

thanks @ghostwheel42
master
Florent Daigniere 3 years ago
parent 98742268e6
commit 19b784b198

@ -152,6 +152,7 @@ class ConfigManager(dict):
self.config['SESSION_COOKIE_HTTPONLY'] = True self.config['SESSION_COOKIE_HTTPONLY'] = True
self.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=int(self.config['SESSION_LIFETIME'])) self.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=int(self.config['SESSION_LIFETIME']))
hostnames = [host.strip() for host in self.config['HOSTNAMES'].split(',')] hostnames = [host.strip() for host in self.config['HOSTNAMES'].split(',')]
self.config['AUTH_RATELIMIT_EXEMPTION'] = set(ipaddress.ip_network(net.strip(), False) for net in config['AUTH_RATELIMIT_EXEMPTION'].split(','))
self.config['HOSTNAMES'] = ','.join(hostnames) self.config['HOSTNAMES'] = ','.join(hostnames)
self.config['HOSTNAME'] = hostnames[0] self.config['HOSTNAME'] = hostnames[0]
# update the app config itself # update the app config itself

@ -80,10 +80,8 @@ def extract_network_from_ip(ip):
return str(n.supernet(prefixlen_diff=(128-int(app.config["AUTH_RATELIMIT_IP_V6_MASK"]))).network_address) return str(n.supernet(prefixlen_diff=(128-int(app.config["AUTH_RATELIMIT_IP_V6_MASK"]))).network_address)
def is_exempt_from_ratelimits(ip): def is_exempt_from_ratelimits(ip):
for range in [net.strip() for net in app.config['AUTH_RATELIMIT_EXEMPTION'].split(',')]: ip = ipaddress.ip_address(ip)
if ipaddress.ip_address(ip) in ipaddress.ip_network(ip, False): return any(ip in cidr for cidr in app.config['AUTH_RATELIMIT_EXEMPTION'])
return False
return True
# Application translation # Application translation
babel = flask_babel.Babel() babel = flask_babel.Babel()

Loading…
Cancel
Save