From 5577aaf332baf96ff81e612c66bbef62bbfdee1d Mon Sep 17 00:00:00 2001 From: lanefu Date: Sun, 12 Sep 2021 15:36:06 -0400 Subject: [PATCH] Obtain request scheme via proxy header if present (#18) and pass it via response header for testing and debugging purpose. Signed-off-by: MichaIng Co-authored-by: MichaIng --- app/main.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 53de618..b1bdf96 100644 --- a/app/main.py +++ b/app/main.py @@ -47,12 +47,18 @@ app = Flask(__name__) # return mirror def get_ip(): - """ returns client IP by parsing proxy headers - if they don't exist, return the actual client IP address """ + """ returns client IP by parsing proxy header + if it doesn't exist, returns the actual client IP address """ return request.environ.get('HTTP_X_FORWARDED_FOR', request.environ.get('REMOTE_ADDR'), ) +def get_scheme(): + """ returns request scheme by parsing proxy header + if it doesn't exist, returns the actual request scheme """ + return request.environ.get('HTTP_X_FORWARDED_PROTO', + request.scheme, + ) def get_region(client_ip, reader=geolite_reader, continents=mirror.continents): """ this is where we geoip and return region code """ @@ -91,7 +97,7 @@ def get_redirect(path, client_ip, mirror_class=mirror, dl_map=DL_MAP): allow schemes from 3 (ftp) to 5 (https) character length """ mirror_url = mirror_class.next(region) if mirror_url.find('://', 3, 8) == -1: - mirror_url = request.scheme + '://' + mirror_url + mirror_url = get_scheme() + '://' + mirror_url if mirror_class.mode == "dl_map" and len(split_path) == 2: key = "{}/{}".format(split_path[0], split_path[1]) @@ -109,6 +115,7 @@ def status(): """ return health check status """ resp = Response("OK") resp.headers['X-Client-IP'] = get_ip() + resp.headers['X-Request-Scheme'] = get_scheme() return resp