Skip to content

Commit

Permalink
#188481396 : use ipaddress package to test valid v6 ipaddress (#72)
Browse files Browse the repository at this point in the history
* use ipaddress package to test

* Update setup.py

* cleanup typos etc

---------

Co-authored-by: Praveen Kumar <[email protected]>
  • Loading branch information
xinghengwang and praves77 authored Oct 31, 2024
1 parent b4938a9 commit df5843f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
13 changes: 8 additions & 5 deletions moesifwsgi/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 getClientIpFromXForwardedFor(self, value):
try:
Expand Down Expand Up @@ -98,4 +101,4 @@ def get_client_address(self, environ):

return environ['REMOTE_ADDR']
except KeyError:
return environ['REMOTE_ADDR']
return environ['REMOTE_ADDR']
2 changes: 1 addition & 1 deletion moesifwsgi/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def initialize_config(self):
response_catcher = HttpResponseCatcher(self.DEBUG)
self.api_client.http_call_back = response_catcher
Configuration.BASE_URI = self.settings.get("BASE_URI") or self.settings.get("LOCAL_MOESIF_BASEURL", "https://api.moesif.net")
Configuration.version = "moesifwsgi-python/1.9.6"
Configuration.version = "moesifwsgi-python/1.9.7"
if self.settings.get("CAPTURE_OUTGOING_REQUESTS", False):
StartCapture().start_capture_outgoing(self.settings)

Expand Down
4 changes: 2 additions & 2 deletions 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.9.6',
version='1.9.7',

description='Moesif Middleware for Python WSGI based platforms (Flask, Bottle & Others)',
long_description=long_description,
Expand All @@ -49,7 +49,7 @@
# 3 - Alpha
# 4 - Beta
# 5 - Production/Stable
'Development Status :: 4 - Beta',
'Development Status :: 5 - Production/Stable',

# Indicate who your project is intended for
'Intended Audience :: Developers',
Expand Down

0 comments on commit df5843f

Please sign in to comment.