suspicious_policy: fix Request IP lookup

This commit is contained in:
Langhammer, Jens 2019-07-22 15:46:41 +02:00
parent 9296c41650
commit 62abe3f256
1 changed files with 13 additions and 5 deletions

View File

@ -11,13 +11,21 @@ from passbook.suspicious_policy.models import IPScore, UserScore
LOGGER = getLogger(__name__) LOGGER = getLogger(__name__)
def get_remote_ip(request):
"""Small wrapper of get_client_ip to catch errors"""
try:
remote_ip, _ = get_client_ip(request)
if remote_ip:
return remote_ip
if 'ip' in request:
return request['ip']
except (AttributeError, ValueError):
pass
return '255.255.255.255'
def update_score(request, username, amount): def update_score(request, username, amount):
"""Update score for IP and User""" """Update score for IP and User"""
remote_ip = '255.255.255.255' remote_ip = get_remote_ip(request)
if 'ip' in request:
remote_ip = request['ip']
elif request:
remote_ip, _ = get_client_ip(request)
ip_score, _ = IPScore.objects.update_or_create(ip=remote_ip) ip_score, _ = IPScore.objects.update_or_create(ip=remote_ip)
ip_score.score += amount ip_score.score += amount
ip_score.save() ip_score.save()