From 6988bbebf823c0ae58a4e2257cac91c250384161 Mon Sep 17 00:00:00 2001 From: Treemo Date: Wed, 10 Jun 2015 15:41:27 +0200 Subject: [PATCH] Web: get real ip (on proxy use) --- circuits/web/tools.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/circuits/web/tools.py b/circuits/web/tools.py index 62ab45b6e..dffced97d 100644 --- a/circuits/web/tools.py +++ b/circuits/web/tools.py @@ -13,6 +13,8 @@ from email.utils import formatdate 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") @@ -449,3 +451,14 @@ def gzip(response, level=4, mime_types=("text/html", "text/plain",)): return httperror( response.request, response, 406, description="identity, gzip" ) + + +class BehindProxy(BaseComponent): + http_index = ['X-Real-IP', 'X-Forwarded-For'] + + @handler('request', priority=1) + def _on_request(self, req, *args): + for index in self.http_index: + real_ip = req.headers.get(index) + req.remote = Host(real_ip, '', real_ip) + return \ No newline at end of file