Skip to content

Commit

Permalink
Web: get real ip (on proxy use)
Browse files Browse the repository at this point in the history
  • Loading branch information
treemo committed Jun 12, 2015
1 parent 16d52a1 commit 9aed186
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions circuits/web/tools.py
Expand Up @@ -14,6 +14,9 @@
from datetime import datetime, timedelta
from email.generator import _make_boundary

from circuits import BaseComponent, handler
from circuits.web.wrappers import Host

mimetypes.init()
mimetypes.add_type("image/x-dwg", ".dwg")
mimetypes.add_type("image/x-icon", ".ico")
Expand Down Expand Up @@ -449,3 +452,24 @@ def gzip(response, level=4, mime_types=("text/html", "text/plain",)):
return httperror(
response.request, response, 406, description="identity, gzip"
)


class ReverseProxy(BaseComponent):
headers = ('X-Real-IP', 'X-Forwarded-For')

def __init__(self, headers=None):
""" Component for have the original client IP when a reverse proxy is used
:param headers: List of HTTP headers to read the original client IP
"""
super(ReverseProxy, self).__init__()

if headers:
self.headers = headers

@handler('request', priority=1)
def _on_request(self, req, *_):
for index in self.headers:
real_ip = req.headers.get(index)
req.remote = Host(real_ip, '', real_ip)
return

0 comments on commit 9aed186

Please sign in to comment.