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 authored and prologic committed Jun 12, 2015
1 parent 40e4e7e commit 09234df
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 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,20 @@ 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
"""
if headers:
self.headers = headers

@handler('request', priority=1)
def _on_request(self, req, *_):
ip = [v for v in map(req.headers.get, self.headers) if v]
req.remote = ip and Host(ip[0], "", ip[0]) or req.remote

0 comments on commit 09234df

Please sign in to comment.