Files
TailScan/templates/index.html
T

460 lines
18 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TailScan</title>
<style>
:root {
--bg: #0f1117;
--surface: #1a1d27;
--surface2: #222636;
--border: #2d3148;
--text: #e2e8f0;
--muted: #7c85a2;
--accent: #5b6ee8;
--accent-hover: #4a5cd6;
--green: #22c55e;
--red: #ef4444;
--yellow: #f59e0b;
--exit-badge: #7c3aed;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; }
/* Top bar */
.topbar {
display: flex; align-items: center; gap: 12px;
padding: 0 20px; height: 52px;
background: var(--surface); border-bottom: 1px solid var(--border);
flex-shrink: 0;
}
.topbar .logo { font-weight: 700; font-size: 16px; color: var(--accent); letter-spacing: 0.5px; }
.topbar .spacer { flex: 1; }
.topbar .exit-status {
display: flex; align-items: center; gap: 8px;
font-size: 13px; color: var(--muted);
}
.topbar .exit-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--red); }
.topbar .exit-status .dot.connected { background: var(--green); }
.topbar .exit-name { color: var(--text); font-weight: 500; }
.btn {
padding: 6px 14px; border-radius: 6px; border: none; cursor: pointer;
font-size: 13px; font-weight: 500; transition: background 0.15s;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); border-color: var(--muted); }
.btn-danger { background: transparent; color: var(--red); border: 1px solid var(--border); }
.btn-danger:hover { background: rgba(239,68,68,0.1); }
/* Layout */
.layout { display: flex; flex: 1; overflow: hidden; }
/* Sidebar */
.sidebar {
width: 280px; flex-shrink: 0;
background: var(--surface); border-right: 1px solid var(--border);
display: flex; flex-direction: column; overflow: hidden;
}
.sidebar-header {
padding: 14px 16px 10px;
border-bottom: 1px solid var(--border);
display: flex; align-items: center; justify-content: space-between;
}
.sidebar-header h2 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
.sidebar-list { overflow-y: auto; flex: 1; padding: 8px; }
.peer-item {
display: flex; align-items: center; gap: 10px;
padding: 8px 10px; border-radius: 8px; cursor: pointer;
transition: background 0.12s; margin-bottom: 2px;
}
.peer-item:hover { background: var(--surface2); }
.peer-item.active-exit { background: rgba(91,110,232,0.15); border: 1px solid rgba(91,110,232,0.3); }
.peer-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: var(--red); }
.peer-dot.online { background: var(--green); }
.peer-info { flex: 1; min-width: 0; }
.peer-name { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-ip { font-size: 11px; color: var(--muted); font-family: monospace; }
.peer-badges { display: flex; gap: 4px; margin-top: 2px; flex-wrap: wrap; }
.badge {
font-size: 10px; padding: 1px 6px; border-radius: 10px; font-weight: 600;
}
.badge-exit { background: rgba(124,58,237,0.2); color: #a78bfa; border: 1px solid rgba(124,58,237,0.3); }
.badge-self { background: rgba(34,197,94,0.15); color: #4ade80; border: 1px solid rgba(34,197,94,0.3); }
.badge-os { background: var(--surface2); color: var(--muted); border: 1px solid var(--border); }
.section-label {
font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.7px;
color: var(--muted); padding: 8px 10px 4px; margin-top: 4px;
}
/* Main content */
.main { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 20px; }
/* Cards */
.card {
background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
padding: 20px;
}
.card-title { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.7px; color: var(--muted); margin-bottom: 16px; }
/* Geo info */
.geo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.geo-item label { font-size: 11px; color: var(--muted); display: block; margin-bottom: 3px; }
.geo-item .value { font-size: 14px; font-weight: 500; }
.geo-item .value.ip { font-family: monospace; font-size: 15px; color: var(--accent); }
/* Device table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
text-align: left; padding: 8px 12px;
font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.7px;
color: var(--muted); border-bottom: 1px solid var(--border);
}
td { padding: 10px 12px; border-bottom: 1px solid rgba(45,49,72,0.5); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--surface2); }
.status-up { color: var(--green); font-weight: 600; }
.ping { font-family: monospace; }
.ip-cell { font-family: monospace; color: var(--accent); }
.mac-cell { font-family: monospace; font-size: 11px; color: var(--muted); }
/* Empty / loading states */
.empty-state {
text-align: center; padding: 48px 24px; color: var(--muted);
}
.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; }
.spinner {
display: inline-block; width: 20px; height: 20px;
border: 2px solid var(--border); border-top-color: var(--accent);
border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
/* Refresh bar */
.refresh-bar {
display: flex; align-items: center; gap: 10px;
font-size: 12px; color: var(--muted);
}
.refresh-bar .countdown { font-weight: 600; color: var(--text); min-width: 20px; }
/* Disconnect prompt */
#no-exit-msg {
display: flex; flex-direction: column; align-items: center; justify-content: center;
flex: 1; text-align: center; gap: 12px; color: var(--muted);
}
#no-exit-msg h2 { font-size: 18px; color: var(--text); }
#no-exit-msg p { font-size: 14px; max-width: 320px; }
</style>
</head>
<body>
<!-- Top bar -->
<div class="topbar">
<span class="logo">🔍 TailScan</span>
<div class="exit-status">
<div class="dot" id="exit-dot"></div>
<span id="exit-label">Not connected to exit node</span>
</div>
<div class="spacer"></div>
<button class="btn btn-danger" id="disconnect-btn" style="display:none" onclick="disconnectExit()">Disconnect</button>
<a href="/logout"><button class="btn btn-ghost">Logout</button></a>
</div>
<!-- Layout -->
<div class="layout">
<!-- Sidebar -->
<div class="sidebar">
<div class="sidebar-header">
<h2>Tailnet Devices</h2>
<button class="btn btn-ghost" onclick="loadPeers()" style="padding:4px 8px; font-size:11px;">↺</button>
</div>
<div class="sidebar-list" id="peer-list">
<div style="padding:20px; text-align:center; color:var(--muted);"><div class="spinner"></div></div>
</div>
</div>
<!-- Main -->
<div class="main" id="main-content">
<div id="no-exit-msg">
<div style="font-size:56px;">🛰️</div>
<h2>Select an Exit Node</h2>
<p>Click an exit node on the left to connect and scan its LAN.</p>
</div>
</div>
</div>
<script>
let currentExitNode = null;
let refreshInterval = null;
let countdownInterval = null;
let countdown = 30;
// ── Peers ────────────────────────────────────────────────────────────────────
async function loadPeers() {
const list = document.getElementById('peer-list');
list.innerHTML = '<div style="padding:20px;text-align:center;color:var(--muted)"><div class="spinner"></div></div>';
try {
const r = await fetch('/api/peers');
const d = await r.json();
currentExitNode = d.current_exit_node;
updateTopbar(currentExitNode, d.peers);
renderPeers(d.peers, d.current_exit_node);
} catch(e) {
list.innerHTML = '<div style="padding:16px;color:var(--red);font-size:13px;">Failed to load peers</div>';
}
}
function renderPeers(peers, activeExit) {
const list = document.getElementById('peer-list');
const exitNodes = peers.filter(p => p.exit_node_option && !p.self);
const others = peers.filter(p => !p.exit_node_option || p.self);
let html = '';
if (exitNodes.length) {
html += '<div class="section-label">Exit Nodes</div>';
for (const p of exitNodes) {
const isActive = p.exit_node || p.ip === activeExit;
html += peerHTML(p, isActive, true);
}
}
if (others.length) {
html += '<div class="section-label">Devices</div>';
for (const p of others) {
html += peerHTML(p, false, false);
}
}
list.innerHTML = html || '<div style="padding:16px;color:var(--muted);font-size:13px;">No devices found</div>';
}
function peerHTML(p, isActive, isExitNode) {
const onclick = isExitNode && !p.self ? `onclick="connectExit('${p.ip}', '${p.hostname}')"` : '';
const cursor = isExitNode && !p.self ? '' : 'cursor:default;';
return `
<div class="peer-item ${isActive ? 'active-exit' : ''}" ${onclick} style="${cursor}">
<div class="peer-dot ${p.online ? 'online' : ''}"></div>
<div class="peer-info">
<div class="peer-name">${escHtml(p.hostname)}</div>
<div class="peer-ip">${p.ip}</div>
<div class="peer-badges">
${p.self ? '<span class="badge badge-self">this device</span>' : ''}
${isExitNode ? '<span class="badge badge-exit">exit node</span>' : ''}
${p.os ? `<span class="badge badge-os">${escHtml(p.os)}</span>` : ''}
</div>
</div>
</div>`;
}
// ── Exit node control ────────────────────────────────────────────────────────
async function connectExit(ip, hostname) {
showLoading(`Connecting to ${hostname}...`);
try {
await fetch('/api/exit-node/set', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ip})
});
currentExitNode = ip;
updateTopbar(ip, null, hostname);
await loadPeers();
startAutoRefresh();
await runScan();
} catch(e) {
showError('Failed to connect to exit node.');
}
}
async function disconnectExit() {
stopAutoRefresh();
await fetch('/api/exit-node/disconnect', {method: 'POST'});
currentExitNode = null;
updateTopbar(null);
await loadPeers();
document.getElementById('main-content').innerHTML = `
<div id="no-exit-msg">
<div style="font-size:56px;">🛰️</div>
<h2>Select an Exit Node</h2>
<p>Click an exit node on the left to connect and scan its LAN.</p>
</div>`;
}
// ── Scan ─────────────────────────────────────────────────────────────────────
async function runScan() {
const main = document.getElementById('main-content');
main.innerHTML = `
<div style="text-align:center;padding:60px;color:var(--muted)">
<div class="spinner" style="width:32px;height:32px;margin:0 auto 16px"></div>
<p>Scanning LAN...</p>
</div>`;
try {
const r = await fetch('/api/scan');
const d = await r.json();
renderScan(d);
resetCountdown();
} catch(e) {
showError('Scan failed.');
}
}
function renderScan(d) {
const main = document.getElementById('main-content');
// Don't reset UI to "not connected" just because scan says so — trust sidebar state
if (!d.connected && !currentExitNode) {
main.innerHTML = `
<div id="no-exit-msg">
<div style="font-size:56px;">🛰️</div>
<h2>Not Connected</h2>
<p>Select an exit node from the sidebar.</p>
</div>`;
return;
}
// If we think we're connected but scan disagrees, keep showing results anyway
if (!d.connected) d.connected = true;
const geo = d.geo || {};
const devices = d.devices || [];
const hasError = devices.length === 1 && devices[0].error;
main.innerHTML = `
<!-- Refresh bar -->
<div class="refresh-bar">
<span>Auto-refresh in <span class="countdown" id="countdown">30</span>s</span>
<button class="btn btn-ghost" onclick="runScan()" style="padding:4px 10px;font-size:12px;">Scan now</button>
</div>
<!-- WAN / Geo -->
<div class="card">
<div class="card-title">WAN & Location</div>
<div class="geo-grid">
<div class="geo-item"><label>WAN IP</label><div class="value ip">${escHtml(geo.ip || '—')}</div></div>
<div class="geo-item"><label>City</label><div class="value">${escHtml(geo.city || '—')}</div></div>
<div class="geo-item"><label>Region</label><div class="value">${escHtml(geo.region || '—')}</div></div>
<div class="geo-item"><label>Country</label><div class="value">${escHtml(geo.country || '—')}</div></div>
<div class="geo-item"><label>Latitude</label><div class="value">${geo.lat ?? '—'}</div></div>
<div class="geo-item"><label>Longitude</label><div class="value">${geo.lon ?? '—'}</div></div>
<div class="geo-item"><label>ISP</label><div class="value">${escHtml(geo.isp || '—')}</div></div>
<div class="geo-item"><label>Timezone</label><div class="value">${escHtml(geo.timezone || '—')}</div></div>
</div>
</div>
<!-- LAN devices -->
<div class="card">
<div class="card-title">LAN Devices — ${escHtml(d.subnet || 'subnet unknown')} (${devices.length} found)</div>
${hasError
? `<div style="color:var(--red);font-size:13px;">${escHtml(devices[0].error)}</div>`
: devices.length === 0
? `<div class="empty-state"><div class="icon">📭</div><p>No devices found on subnet ${escHtml(d.subnet || '')}</p></div>`
: `<div class="table-wrap"><table>
<thead><tr>
<th>IP</th><th>Hostname</th><th>Ping</th><th>MAC</th><th>Vendor</th><th>Status</th>
</tr></thead>
<tbody>
${devices.map(dev => `
<tr>
<td class="ip-cell">${escHtml(dev.ip)}</td>
<td>${escHtml(dev.hostname)}</td>
<td class="ping">${dev.ping_ms != null ? dev.ping_ms + ' ms' : '—'}</td>
<td class="mac-cell">${escHtml(dev.mac)}</td>
<td style="color:var(--muted);font-size:12px;">${escHtml(dev.vendor)}</td>
<td class="status-up">●&nbsp;up</td>
</tr>`).join('')}
</tbody>
</table></div>`
}
</div>`;
}
// ── Auto-refresh ─────────────────────────────────────────────────────────────
function startAutoRefresh() {
stopAutoRefresh();
countdown = 30;
countdownInterval = setInterval(() => {
countdown--;
const el = document.getElementById('countdown');
if (el) el.textContent = countdown;
if (countdown <= 0) runScan();
}, 1000);
}
function stopAutoRefresh() {
if (countdownInterval) { clearInterval(countdownInterval); countdownInterval = null; }
}
function resetCountdown() {
countdown = 30;
const el = document.getElementById('countdown');
if (el) el.textContent = 30;
startAutoRefresh();
}
// ── Top bar ──────────────────────────────────────────────────────────────────
function updateTopbar(exitIp, peers, hostname) {
const dot = document.getElementById('exit-dot');
const label = document.getElementById('exit-label');
const btn = document.getElementById('disconnect-btn');
if (exitIp) {
dot.classList.add('connected');
// Try to find hostname from peers
let name = hostname;
if (!name && peers) {
const match = peers.find(p => p.ip === exitIp);
if (match) name = match.hostname;
}
label.textContent = `Exit: ${name || exitIp}`;
btn.style.display = '';
} else {
dot.classList.remove('connected');
label.textContent = 'Not connected to exit node';
btn.style.display = 'none';
}
}
// ── Helpers ──────────────────────────────────────────────────────────────────
function escHtml(s) {
return String(s).replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');
}
function showLoading(msg) {
document.getElementById('main-content').innerHTML = `
<div style="text-align:center;padding:60px;color:var(--muted)">
<div class="spinner" style="width:32px;height:32px;margin:0 auto 16px"></div>
<p>${escHtml(msg)}</p>
</div>`;
}
function showError(msg) {
document.getElementById('main-content').innerHTML = `
<div style="text-align:center;padding:60px;color:var(--red)">
<div style="font-size:40px;margin-bottom:12px;">⚠️</div>
<p>${escHtml(msg)}</p>
</div>`;
}
// ── Init ─────────────────────────────────────────────────────────────────────
(async () => {
await loadPeers();
if (currentExitNode) {
startAutoRefresh();
await runScan();
}
})();
</script>
</body>
</html>