"""The caller's address, for the throttles that are keyed by it (SEC-42, B-54).""" def client_ip(request) -> str: """The *last* hop of X-Forwarded-For: the one written by our own proxy. The first elements are whatever the client sent, so reading from the front would let a fake value per request dodge every IP-keyed control. Caddy also overwrites the header with the real peer (Caddyfile), so either half closes it; without the header (direct access) the socket peer is used.""" forwarded = request.META.get("HTTP_X_FORWARDED_FOR", "") hops = [hop.strip() for hop in forwarded.split(",") if hop.strip()] if hops: return hops[-1] return request.META.get("REMOTE_ADDR", "unknown")