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.
main
Ben Busby 2021-10-28 21:31:24 -06:00
parent 26b560da1d
commit 0a78c524fa
No known key found for this signature in database
GPG Key ID: 339B7B7EB5333D14
2 changed files with 10 additions and 2 deletions

View File

@ -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',

View File

@ -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']