aboutsummaryrefslogtreecommitdiff
path: root/jb/views/utils.py
blob: 39db5d20f84278344a9904e584b0096a6af3d643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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