From 0a78c524faf81c3458166e6a02a3fe0fcd5aebaa Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Thu, 28 Oct 2021 21:31:24 -0600 Subject: [PATCH] Expand 'my ip' to work for proxied requests Adds a check for the HTTP_X_FORWARDED_FOR header, and uses the value from the request if found. --- app/routes.py | 4 ++-- app/utils/misc.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/routes.py b/app/routes.py index 224905e..b6e6f45 100644 --- a/app/routes.py +++ b/app/routes.py @@ -13,7 +13,7 @@ from app import app from app.models.config import Config from app.request import Request, TorError from app.utils.bangs import resolve_bang -from app.utils.misc import read_config_bool +from app.utils.misc import read_config_bool, get_client_ip from app.utils.results import add_ip_card from app.utils.results import bold_search_terms from app.utils.search import * @@ -257,7 +257,7 @@ def search(): # Feature to display IP address if search_util.check_kw_ip(): html_soup = bsoup(str(response), 'html.parser') - response = add_ip_card(html_soup, request.remote_addr) + response = add_ip_card(html_soup, get_client_ip(request)) return render_template( 'display.html', diff --git a/app/utils/misc.py b/app/utils/misc.py index 01e6935..6ce029a 100644 --- a/app/utils/misc.py +++ b/app/utils/misc.py @@ -1,3 +1,4 @@ +from flask import Request import hashlib import os @@ -15,3 +16,10 @@ def read_config_bool(var: str) -> bool: if val.isdigit(): return bool(int(val)) return False + + +def get_client_ip(r: Request) -> str: + if r.environ.get('HTTP_X_FORWARDED_FOR') is None: + return r.environ['REMOTE_ADDR'] + else: + return r.environ['HTTP_X_FORWARDED_FOR']