aboutsummaryrefslogtreecommitdiff
path: root/jb/views/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'jb/views/utils.py')
-rw-r--r--jb/views/utils.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/jb/views/utils.py b/jb/views/utils.py
new file mode 100644
index 0000000..39db5d2
--- /dev/null
+++ b/jb/views/utils.py
@@ -0,0 +1,15 @@
+from fastapi import Request
+
+
+def get_client_ip(request: Request) -> str:
+ """
+ Using a testclient, the ip returned is 'testclient'. If so, instead, grab
+ the ip from the headers
+ """
+ ip = request.headers.get("X-Forwarded-For")
+ if not ip:
+ ip = request.client.host
+ elif ip == "testclient" or ip.startswith("10."):
+ forwarded = request.headers.get("X-Forwarded-For")
+ ip = forwarded.split(",")[0].strip() if forwarded else request.client.host
+ return ip