Skip to content

Commit

Permalink
use ipaddress package to test
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghengwang committed Oct 31, 2024
1 parent b4938a9 commit 27935e4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ moesifapi>=1.5.3
moesifpythonrequest>=0.3.4
readerwriterlock==1.0.9
apscheduler==3.10.4
ipaddress; python_version < '3.3'
3 changes: 2 additions & 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.9.6',
version='1.9.7',

description='Moesif Middleware for Python WSGI based platforms (Flask, Bottle & Others)',
long_description=long_description,
Expand Down Expand Up @@ -94,6 +94,7 @@
'moesifpythonrequest>=0.3.4',
'readerwriterlock>=1.0.9',
'apscheduler>=3.10.4'
'ipaddress;python_version<"3.3"',
],

# List additional groups of dependencies here (e.g. development
Expand Down

0 comments on commit 27935e4

Please sign in to comment.