Spaces:
Runtime error
Runtime error
Update bot/core/speedtest.py
Browse files- bot/core/speedtest.py +10 -23
bot/core/speedtest.py
CHANGED
|
@@ -45,35 +45,22 @@ async def ping_ms(host: str = "1.1.1.1", port: int = 443, timeout: float = 3.0)
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
|
| 48 |
-
async def public_ip(timeout: float =
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
-
|
| 52 |
"""
|
| 53 |
-
try:
|
| 54 |
-
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as c:
|
| 55 |
-
r = await c.get("https://api.ipify.org", params={"format": "json"})
|
| 56 |
-
if r.status_code < 400:
|
| 57 |
-
j = r.json()
|
| 58 |
-
ip = str(j.get("ip") or "").strip()
|
| 59 |
-
if ip:
|
| 60 |
-
return ip
|
| 61 |
-
except Exception:
|
| 62 |
-
pass
|
| 63 |
-
|
| 64 |
-
# fallback: Cloudflare trace
|
| 65 |
try:
|
| 66 |
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as c:
|
| 67 |
r = await c.get("https://1.1.1.1/cdn-cgi/trace")
|
| 68 |
-
if r.status_code
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
except Exception:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
return None
|
| 78 |
|
| 79 |
|
|
|
|
| 45 |
return None
|
| 46 |
|
| 47 |
|
| 48 |
+
async def public_ip(timeout: float = 4.0) -> Optional[str]:
|
| 49 |
"""
|
| 50 |
+
Gets public IP using Cloudflare trace.
|
| 51 |
+
Returns string IP or None.
|
| 52 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
try:
|
| 54 |
async with httpx.AsyncClient(timeout=timeout, follow_redirects=True) as c:
|
| 55 |
r = await c.get("https://1.1.1.1/cdn-cgi/trace")
|
| 56 |
+
if r.status_code >= 400:
|
| 57 |
+
return None
|
| 58 |
+
# trace format: key=value lines
|
| 59 |
+
for line in (r.text or "").splitlines():
|
| 60 |
+
if line.startswith("ip="):
|
| 61 |
+
return line.split("=", 1)[1].strip() or None
|
| 62 |
except Exception:
|
| 63 |
+
return None
|
|
|
|
| 64 |
return None
|
| 65 |
|
| 66 |
|