Skip to content

Commit

Permalink
update checker for ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghengwang committed Oct 31, 2024
1 parent 6dbd557 commit 06ba07a
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions moesifdjango/client_ip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import logging
import ipaddress

logger = logging.getLogger(__name__)

Expand All @@ -10,10 +11,12 @@ def __init__(self):

@classmethod
def is_ip(cls, value):
if not value is None:
ipv4 = r"^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$"
ipv6 = r"^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i"
return re.match(ipv4, value) or re.match(ipv6, value)
# https://docs.python.org/3/library/ipaddress.html#ipaddress.ip_address
try:
ip = ipaddress.ip_address(value);
return True;
except ValueError:
return False;

def get_client_ip_from_x_forwarded_for(self, value):
try:
Expand Down

0 comments on commit 06ba07a

Please sign in to comment.