understanding commited on
Commit
9f108c3
·
verified ·
1 Parent(s): 2e08224

Update bot/core/speedtest.py

Browse files
Files changed (1) hide show
  1. 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 = 5.0) -> Optional[str]:
49
  """
50
- Best-effort public IP detect.
51
- Tries ipify, then Cloudflare trace.
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 < 400:
69
- # lines like: ip=1.2.3.4
70
- for line in (r.text or "").splitlines():
71
- if line.startswith("ip="):
72
- ip = line.split("=", 1)[1].strip()
73
- return ip or None
74
  except Exception:
75
- pass
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