debug
This commit is contained in:
+25
-2
@@ -153,18 +153,41 @@ class IPInfo:
|
|||||||
# ==================== Роутер ====================
|
# ==================== Роутер ====================
|
||||||
router = APIRouter(prefix='/api/v1', tags=['ip'])
|
router = APIRouter(prefix='/api/v1', tags=['ip'])
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
@router.get('/ip')
|
@router.get('/ip')
|
||||||
async def get_ip(request: Request):
|
async def get_ip(request: Request):
|
||||||
|
# 1. Проверяем заголовки
|
||||||
forwarded = request.headers.get('X-Forwarded-For')
|
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:
|
if forwarded:
|
||||||
# Берем ПЕРВЫЙ IP из списка (это реальный клиент)
|
|
||||||
client_ip = forwarded.split(',')[0].strip()
|
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:
|
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
|
html = response.text
|
||||||
ipinfo = IPInfo(html)
|
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()
|
return ipinfo.dict()
|
||||||
Reference in New Issue
Block a user