Skip to content

Commit

Permalink
#188481396 : Use ipaddress package to test valid IPV6 addresses (#36)
Browse files Browse the repository at this point in the history
* Use ipaddress package to test valid IPV6, bump ver

* Standardize return type
  • Loading branch information
dylanfrankcom authored Nov 1, 2024
1 parent db2e0c1 commit f580c35
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
16 changes: 6 additions & 10 deletions moesifasgi/client_ip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

import logging
import ipaddress

logger = logging.getLogger(__name__)

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

@classmethod
def is_ip(cls, value):
# https://docs.python.org/3/library/ipaddress.html#ipaddress.ip_address
try:
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)
except:
# Ignore any error
pass

return None
ip = ipaddress.ip_address(value)
return True
except ValueError:
return False

def getClientIpFromXForwardedFor(self, value, debug = False):
try:
Expand Down
4 changes: 2 additions & 2 deletions moesifasgi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MoesifMiddleware(BaseHTTPMiddleware):
"""ASGI Middleware for recording of request-response"""
def __init__(self, settings=None, *args, **kwargs):
super().__init__(*args, **kwargs)

if settings is None:
raise Exception('Moesif Application ID is required in settings')
self.settings = settings
Expand Down Expand Up @@ -88,7 +88,7 @@ def schedule_config_job(self):

def initialize_config(self):
Configuration.BASE_URI = self.settings.get("BASE_URI", "https://api.moesif.net")
Configuration.version = 'moesifasgi-python/1.0.9'
Configuration.version = 'moesifasgi-python/1.0.10'
self.LOG_BODY = self.settings.get("LOG_BODY", True)

self.app_config = AppConfig()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='1.0.9',
version='1.0.10',

description='Moesif Middleware for Python ASGI based platforms (FastAPI & Others)',
long_description=long_description,
Expand Down

0 comments on commit f580c35

Please sign in to comment.