Spaces:
Runtime error
Runtime error
Update bot/integrations/diag_extra.py
Browse files- bot/integrations/diag_extra.py +24 -39
bot/integrations/diag_extra.py
CHANGED
|
@@ -1,52 +1,37 @@
|
|
| 1 |
# PATH: bot/integrations/diag_extra.py
|
| 2 |
-
from __future__ import annotations
|
| 3 |
-
|
| 4 |
import socket
|
| 5 |
from urllib.parse import urlparse
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
s = (url_or_host or "").strip()
|
| 10 |
-
if not s:
|
| 11 |
-
return ""
|
| 12 |
-
# If it's a URL, extract hostname; else treat as hostname
|
| 13 |
-
if "://" in s:
|
| 14 |
-
try:
|
| 15 |
-
u = urlparse(s)
|
| 16 |
-
return (u.hostname or "").strip()
|
| 17 |
-
except Exception:
|
| 18 |
-
return ""
|
| 19 |
-
# remove port if present
|
| 20 |
-
if ":" in s:
|
| 21 |
-
return s.split(":", 1)[0].strip()
|
| 22 |
-
return s
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def dns_check(url_or_host: str, max_ips: int = 5) -> str:
|
| 26 |
"""
|
| 27 |
-
Returns
|
| 28 |
-
- OK
|
| 29 |
-
- ERR:<
|
| 30 |
-
Works in HF container (best-effort).
|
| 31 |
"""
|
| 32 |
-
host =
|
| 33 |
if not host:
|
| 34 |
-
return "ERR:empty_host"
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
infos = socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)
|
| 39 |
ips = []
|
| 40 |
-
for
|
| 41 |
-
|
| 42 |
-
if
|
| 43 |
-
ips.append(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if not ips:
|
| 47 |
-
return f"ERR:no_ip:{host}"
|
| 48 |
-
return f"OK:{host} -> {', '.join(ips)}"
|
| 49 |
-
except socket.gaierror as e:
|
| 50 |
-
return f"ERR:gaierror:{e}"
|
| 51 |
except Exception as e:
|
| 52 |
return f"ERR:{type(e).__name__}:{e}"
|
|
|
|
| 1 |
# PATH: bot/integrations/diag_extra.py
|
|
|
|
|
|
|
| 2 |
import socket
|
| 3 |
from urllib.parse import urlparse
|
| 4 |
|
| 5 |
+
def _host_from_url(url: str) -> str:
|
| 6 |
+
try:
|
| 7 |
+
u = urlparse(url)
|
| 8 |
+
if u.hostname:
|
| 9 |
+
return u.hostname
|
| 10 |
+
except Exception:
|
| 11 |
+
pass
|
| 12 |
+
# fallback: if someone passed "domain.com"
|
| 13 |
+
url = (url or "").strip()
|
| 14 |
+
url = url.replace("https://", "").replace("http://", "")
|
| 15 |
+
return url.split("/")[0].strip()
|
| 16 |
|
| 17 |
+
def dns_check(url_or_host: str, port: int = 443) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
+
Returns:
|
| 20 |
+
- OK:<host> -> ip1, ip2, ...
|
| 21 |
+
- ERR:<ExceptionName>:<message>
|
|
|
|
| 22 |
"""
|
| 23 |
+
host = _host_from_url(url_or_host)
|
| 24 |
if not host:
|
| 25 |
+
return "ERR:ValueError:empty_host"
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
|
|
|
|
| 29 |
ips = []
|
| 30 |
+
for it in infos:
|
| 31 |
+
addr = it[4][0]
|
| 32 |
+
if addr not in ips:
|
| 33 |
+
ips.append(addr)
|
| 34 |
+
ips = ips[:8] # keep short
|
| 35 |
+
return f"OK:{host} -> " + ", ".join(ips)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"ERR:{type(e).__name__}:{e}"
|