139 lines
4.6 KiB
Python
139 lines
4.6 KiB
Python
from flask import Flask, request, render_template_string
|
||
import requests
|
||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||
|
||
app = Flask(__name__)
|
||
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1)
|
||
|
||
HTML_TEMPLATE = """<!doctype html>
|
||
<html lang="it">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<title>myip – IP & RIPE Lookup</title>
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap" rel="stylesheet">
|
||
|
||
<link rel="icon" type="image/x-icon" href="/static/favicon.png">
|
||
<link rel="stylesheet" href="/static/myip.css">
|
||
</head>
|
||
<body>
|
||
<div class="card">
|
||
{% if show_logo %}
|
||
<img src="/static/logo.png" class="logo" alt="Logo">
|
||
{% endif %}
|
||
<div class="badge">
|
||
<span class="badge-dot"></span>
|
||
<span>IP & RIPE Lookup</span>
|
||
</div>
|
||
<h1 class="title">Il tuo IP pubblico è:</h1>
|
||
|
||
<div class="ip-wrapper">
|
||
<span id="ip-value" class="ip">{{ ip }}</span>
|
||
<button id="copy-btn" class="copy-btn" type="button">
|
||
<span class="copy-icon">📋</span>
|
||
<span class="copy-label">Copia IP</span>
|
||
</button>
|
||
</div>
|
||
|
||
<div class="divider"></div>
|
||
|
||
{% if ripe %}
|
||
<div class="ripe-title">Informazioni RIPE</div>
|
||
{% if ripe.holder %}
|
||
<div class="ripe-item"><strong>Provider:</strong> {{ ripe.holder }}</div>
|
||
{% endif %}
|
||
{% if ripe.prefix %}
|
||
<div class="ripe-item"><strong>Prefisso:</strong> {{ ripe.prefix }}</div>
|
||
{% endif %}
|
||
{% if ripe.asns %}
|
||
<div class="ripe-item"><strong>ASN:</strong> {{ ripe.asns | join(', ') }}</div>
|
||
{% endif %}
|
||
<div class="ripe-muted">Dati ottenuti tramite RIPEstat Data API.</div>
|
||
{% else %}
|
||
<div class="ripe-title">Informazioni RIPE</div>
|
||
<div class="ripe-muted">
|
||
Non è stato possibile recuperare i dettagli RIPE per questo indirizzo.
|
||
</div>
|
||
{% endif %}
|
||
</div>
|
||
|
||
<script>
|
||
document.addEventListener("DOMContentLoaded", function () {
|
||
var card = document.querySelector(".card");
|
||
if (card) card.classList.add("card-visible");
|
||
|
||
var btn = document.getElementById("copy-btn");
|
||
var ipSpan = document.getElementById("ip-value");
|
||
if (!btn || !ipSpan) return;
|
||
|
||
btn.addEventListener("click", function () {
|
||
var ip = ipSpan.textContent.trim();
|
||
if (!ip) return;
|
||
|
||
navigator.clipboard.writeText(ip).then(function () {
|
||
var original = btn.querySelector(".copy-label").textContent;
|
||
btn.classList.add("copy-btn-success");
|
||
btn.querySelector(".copy-label").textContent = "Copiato!";
|
||
setTimeout(function () {
|
||
btn.classList.remove("copy-btn-success");
|
||
btn.querySelector(".copy-label").textContent = original;
|
||
}, 1500);
|
||
}).catch(console.error);
|
||
});
|
||
});
|
||
</script>
|
||
</body>
|
||
</html>
|
||
"""
|
||
|
||
RIPESTAT_BASE = "https://stat.ripe.net/data"
|
||
|
||
def get_client_ip():
|
||
xff = request.headers.get("X-Forwarded-For", "")
|
||
if xff:
|
||
parts = [p.strip() for p in xff.split(",") if p.strip()]
|
||
if parts:
|
||
return parts[0]
|
||
xri = request.headers.get("X-Real-IP")
|
||
if xri:
|
||
return xri.strip()
|
||
return request.remote_addr or "Sconosciuto"
|
||
|
||
def fetch_ripe_info(ip: str):
|
||
try:
|
||
ni_resp = requests.get(f"{RIPESTAT_BASE}/network-info/data.json", params={"resource": ip}, timeout=2)
|
||
ni_resp.raise_for_status()
|
||
ni_data = ni_resp.json().get("data", {})
|
||
|
||
prefix = ni_data.get("prefix")
|
||
asns = ni_data.get("asns") or []
|
||
holder = None
|
||
|
||
if asns:
|
||
first_asn = asns[0]
|
||
ao_resp = requests.get(f"{RIPESTAT_BASE}/as-overview/data.json", params={"resource": first_asn}, timeout=2)
|
||
ao_resp.raise_for_status()
|
||
holder = ao_resp.json().get("data", {}).get("holder")
|
||
|
||
class R: pass
|
||
r = R()
|
||
r.prefix = prefix
|
||
r.asns = [f"AS{x}" for x in asns] if asns else []
|
||
r.holder = holder
|
||
|
||
return r if any([r.prefix, r.asns, r.holder]) else None
|
||
except:
|
||
return None
|
||
|
||
@app.route("/")
|
||
def index():
|
||
ip = get_client_ip()
|
||
ripe = fetch_ripe_info(ip) if ip != "Sconosciuto" else None
|
||
return render_template_string(HTML_TEMPLATE, ip=ip, ripe=ripe, show_logo=True)
|
||
|
||
if __name__ == "__main__":
|
||
app.run(host="0.0.0.0", port=8000)
|