(request: Request)
| 46 | |
| 47 | |
| 48 | def get_client_ip(request: Request) -> str: |
| 49 | client_host = request.client.host if request.client else "unknown" |
| 50 | if not _is_trusted_proxy(client_host): |
| 51 | return client_host |
| 52 | |
| 53 | forwarded_for = request.headers.get("X-Forwarded-For") |
| 54 | if forwarded_for: |
| 55 | return _get_forwarded_for_ip(forwarded_for, client_host) |
| 56 | |
| 57 | real_ip = request.headers.get("X-Real-IP") |
| 58 | if real_ip: |
| 59 | try: |
| 60 | ip_address(real_ip) |
| 61 | except ValueError: |
| 62 | return client_host |
| 63 | return real_ip |
| 64 | |
| 65 | return client_host |
| 66 | |
| 67 | |
| 68 | class IPRateLimit: |
no test coverage detected