Files
Void-Homelab/public/views/service_url.js
2026-06-08 00:56:22 +10:00

29 lines
1.3 KiB
JavaScript

// Pure helpers: choose which URL a service tile opens, based on whether the dashboard
// is being viewed on-network (LAN) or remotely. No DOM / no network access here.
const PRIVATE_RE = /^(localhost$|127\.|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/;
// True when `hostname` is NOT a private/local address — i.e. the dashboard is being
// accessed remotely (e.g. via void.hynesy.com).
export function isRemoteHost(hostname = '') {
const h = String(hostname).toLowerCase();
if (!h) return false;
if (PRIVATE_RE.test(h)) return false;
if (h.endsWith('.local') || h.endsWith('.lan')) return false;
if (!h.includes('.')) return false; // bare hostname (e.g. "ct311")
return true;
}
// For a service ({ url, external }) and whether we're remote, return the primary URL
// the tile opens, an optional alt (the other URL) for one-click fallback, and whether
// the service is LAN-only while remote.
export function pickServiceUrls(svc = {}, remote = false) {
const url = svc.url || '';
const external = svc.external || '';
let primary, alt;
if (remote) { primary = external || url; alt = external ? url : ''; }
else { primary = url; alt = external || ''; }
if (alt && alt === primary) alt = '';
return { primary, alt: alt || null, lanOnly: remote && !external };
}