mirror of
https://github.com/jazzband/django-defender.git
synced 2026-05-05 06:04:49 +00:00
took marcus's advice and used the built in django validator
This commit is contained in:
parent
2f6afbdb6e
commit
fd4f58a20c
2 changed files with 10 additions and 9 deletions
|
|
@ -362,6 +362,10 @@ class AccessAttemptTest(DefenderTestCase):
|
|||
utils.is_valid_ip('2001:db8:85a3:0:0:8a2e:370:7334'), True)
|
||||
self.assertEquals(
|
||||
utils.is_valid_ip('2001:db8:85a3::8a2e:370:7334'), True)
|
||||
self.assertEquals(
|
||||
utils.is_valid_ip('::ffff:192.0.2.128'), True)
|
||||
self.assertEquals(
|
||||
utils.is_valid_ip('::ffff:8.8.8.8'), True)
|
||||
|
||||
def test_parse_redis_url(self):
|
||||
""" test the parse_redis_url method """
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import logging
|
||||
import socket
|
||||
import re
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.core.validators import validate_ipv46_address
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from .connection import get_redis_connection
|
||||
from . import config
|
||||
|
|
@ -21,14 +21,11 @@ def is_valid_ip(ip_address):
|
|||
if not ip_address:
|
||||
return False
|
||||
ip_address = ip_address.strip()
|
||||
ipv4_re = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
|
||||
ipv6_re = r'^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|::(?:[0-9A-Fa-f]{1,4}:){5}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1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0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)$'
|
||||
ipv4_regex = re.compile(ipv4_re)
|
||||
ipv6_regex = re.compile(ipv6_re)
|
||||
|
||||
if ipv4_regex.match(ip_address) or ipv6_regex.match(ip_address):
|
||||
try:
|
||||
validate_ipv46_address(ip_address)
|
||||
return True
|
||||
return False
|
||||
except ValidationError:
|
||||
return False
|
||||
|
||||
|
||||
def get_ip_address_from_request(request):
|
||||
|
|
|
|||
Loading…
Reference in a new issue