From 5dc98f28812cb58127552a44a39fa63a49d352c0 Mon Sep 17 00:00:00 2001 From: austnv Date: Sun, 14 Jun 2026 16:29:01 +0300 Subject: [PATCH] add logging --- main.py | 3 +++ routers/ip.py | 37 +++++++++---------------------------- 2 files changed, 12 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index 174ad51..97b6db5 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,11 @@ from fastapi import FastAPI import tomllib +import logging from routers import ip +logging.basicConfig(level=logging.INFO) + def get_version_from_pyproject(): with open("pyproject.toml", "rb") as f: data = tomllib.load(f) diff --git a/routers/ip.py b/routers/ip.py index 8ff58a3..b037a59 100644 --- a/routers/ip.py +++ b/routers/ip.py @@ -6,6 +6,10 @@ from fastapi import APIRouter, Request from httpx import AsyncClient +import logging + +logging.basicConfig(level=logging.INFO) + # ==================== Pydantic модели ==================== @@ -153,41 +157,18 @@ 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: - 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]}") + response = await client.get(f'https://ipinfo.io/{client_ip}') 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