diff --git a/routers/ip.py b/routers/ip.py index 7361269..8ff58a3 100644 --- a/routers/ip.py +++ b/routers/ip.py @@ -153,18 +153,41 @@ class IPInfo: # ==================== Роутер ==================== router = APIRouter(prefix='/api/v1', tags=['ip']) +import logging +logging.basicConfig(level=logging.DEBUG) + @router.get('/ip') async def get_ip(request: Request): + # 1. Проверяем заголовки forwarded = request.headers.get('X-Forwarded-For') - + print(f"[DEBUG] X-Forwarded-For: {forwarded}") + print(f"[DEBUG] All headers: {dict(request.headers)}") + + # 2. Определяем IP if forwarded: - # Берем ПЕРВЫЙ IP из списка (это реальный клиент) client_ip = forwarded.split(',')[0].strip() - + else: + client_ip = request.client.host + + print(f"[DEBUG] Client IP: {client_ip}") + + # 3. Проверяем запрос к ipinfo.io async with AsyncClient() as client: - response = await client.get(f'https://ipinfo.io/{client_ip}') + url = f'https://ipinfo.io/{client_ip}' + print(f"[DEBUG] Request URL: {url}") + + response = await client.get(url) + print(f"[DEBUG] Response status: {response.status_code}") + print(f"[DEBUG] Response headers: {response.headers}") + print(f"[DEBUG] Response text (first 500 chars): {response.text[:500]}") html = response.text ipinfo = IPInfo(html) - + + print(f"[DEBUG] IPInfo is_valid: {ipinfo.is_valid}") + print(f"[DEBUG] IPInfo._data: {ipinfo._data}") + + if ipinfo._data is None: + return {"error": "Parse failed", "ip": client_ip, "raw_html_preview": html[:200]} + return ipinfo.dict() \ No newline at end of file