ASN lookup : ajout fallback Team Cymru whois (port 43)
3e service de résolution après ip-api.com et ipinfo.io. Cymru whois utilise TCP port 43 (pas HTTPS), ce qui fonctionne même quand les sorties HTTPS sont bloquées côté serveur. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d272b3e8b7
commit
94f75dc59c
1 changed files with 24 additions and 1 deletions
25
app/app.py
25
app/app.py
|
|
@ -722,7 +722,7 @@ def _lookup_ip_asn(ip: str) -> dict:
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Fallback : ipinfo.io si toujours pas d'AS
|
# Fallback 1 : ipinfo.io
|
||||||
if not result.get("asn"):
|
if not result.get("asn"):
|
||||||
try:
|
try:
|
||||||
req = urllib.request.Request(
|
req = urllib.request.Request(
|
||||||
|
|
@ -736,6 +736,29 @@ def _lookup_ip_asn(ip: str) -> dict:
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Fallback 2 : Team Cymru whois (port 43, TCP — fonctionne même si HTTPS bloqué)
|
||||||
|
if not result.get("asn"):
|
||||||
|
try:
|
||||||
|
import socket as _socket
|
||||||
|
s = _socket.create_connection(("whois.cymru.com", 43), timeout=8)
|
||||||
|
s.sendall(f"verbose\n{ip}\n".encode())
|
||||||
|
data = b""
|
||||||
|
while True:
|
||||||
|
chunk = s.recv(4096)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
data += chunk
|
||||||
|
s.close()
|
||||||
|
for line in data.decode(errors="replace").splitlines():
|
||||||
|
if "|" not in line or line.startswith("AS"):
|
||||||
|
continue
|
||||||
|
parts = [p.strip() for p in line.split("|")]
|
||||||
|
if len(parts) >= 7 and parts[0] not in ("", "NA"):
|
||||||
|
result = {"asn": parts[0], "name": parts[6], "country": parts[3]}
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
# Mise en cache seulement si on a trouvé un AS
|
# Mise en cache seulement si on a trouvé un AS
|
||||||
if result.get("asn"):
|
if result.get("asn"):
|
||||||
with _pg() as conn:
|
with _pg() as conn:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue