Skip to content

Commit

Permalink
Update old-style CidrMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
pchristos committed Oct 6, 2017
1 parent c7a1ec8 commit 9ea6c90
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions vpn-proxy/app/middleware/cidr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ class CidrMiddleware(object):
"""

def process_request(self, request):
def __init__(self, get_response):
# One-time configuration & initialization, when the web server starts.
self.get_response = get_response

# Get the HTTP host from the request headers.
host = request.META['REMOTE_ADDR']

host = netaddr.IPAddress(host)
cidrs = [netaddr.IPNetwork(cidr) for cidr in settings.SOURCE_CIDRS]
self.cidrs = [
netaddr.IPNetwork(cidr) for cidr in settings.SOURCE_CIDRS
]

for cidr in cidrs:
if host in cidr:
return
def __call__(self, request):
# Get the source IP address from the request headers.
host = request.META['REMOTE_ADDR']
for cidr in self.cidrs:
if netaddr.IPAddress(host) in cidr:
return self.get_response(request)
log.critical('Connection attempt from unauthorized source %s', host)
raise Http404
raise Http404()
2 changes: 1 addition & 1 deletion vpn-proxy/project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
'app',
]

MIDDLEWARE_CLASSES = [
MIDDLEWARE = [
'app.middleware.cidr.CidrMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down

0 comments on commit 9ea6c90

Please sign in to comment.