Spaces:
Runtime error
Runtime error
Update bot/integrations/diag_extra.py
Browse files
bot/integrations/diag_extra.py
CHANGED
|
@@ -1,14 +1,52 @@
|
|
| 1 |
# PATH: bot/integrations/diag_extra.py
|
|
|
|
|
|
|
| 2 |
import socket
|
| 3 |
from urllib.parse import urlparse
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
-
|
| 8 |
-
if not host:
|
| 9 |
-
return "bad_url"
|
| 10 |
infos = socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)
|
| 11 |
-
ips =
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
return f"ERR:{type(e).__name__}:{e}"
|
|
|
|
| 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 _to_host(url_or_host: str) -> str:
|
| 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 a short string:
|
| 28 |
+
- OK:host -> ip1,ip2
|
| 29 |
+
- ERR:<Reason>
|
| 30 |
+
Works in HF container (best-effort).
|
| 31 |
+
"""
|
| 32 |
+
host = _to_host(url_or_host)
|
| 33 |
+
if not host:
|
| 34 |
+
return "ERR:empty_host"
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
+
# getaddrinfo may return duplicates; we de-dupe
|
|
|
|
|
|
|
| 38 |
infos = socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)
|
| 39 |
+
ips = []
|
| 40 |
+
for info in infos:
|
| 41 |
+
ip = info[4][0]
|
| 42 |
+
if ip not in ips:
|
| 43 |
+
ips.append(ip)
|
| 44 |
+
if len(ips) >= max_ips:
|
| 45 |
+
break
|
| 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}"
|