varaverk: fix ext IP and Tailscale IP in network card

- External IP: switch from DDNS domain lookup to curl ifconfig.me with 5-min
  cache; DDNS approach broke because host detection uses HOST1_NAME but conf
  has HOST1, so domain was never resolved
- Tailscale IP: switch from hardcoded tailscale0 iface to `tailscale ip -4`;
  interface name varies (tailscale0, tailscale1) across kernels/installs
This commit is contained in:
Gmer4Lfe
2026-05-24 11:07:15 -04:00
parent b2e937526f
commit 4a22f3af66
@@ -202,34 +202,26 @@ function vv_network_stats(): array {
$speedMbps = (int)@file_get_contents("/sys/class/net/$iface/speed");
// Local IP — use the NIC defined in HOST*_SYS_WATCHDOG_NIC, fall back to primary iface
$vars = vv_conf_vars();
$host = vv_detect_host();
$prefix = $host !== 'unknown' ? strtoupper($host) . '_' : 'HOST1_';
$nic = $vars[$prefix . 'SYS_WATCHDOG_NIC'] ?? $iface;
// Local IP — use primary iface
$localIp = trim(shell_exec(
"ip -4 addr show " . escapeshellarg($nic) . " 2>/dev/null | awk '/inet /{print \$2}' | cut -d/ -f1 | head -1"
"ip -4 addr show " . escapeshellarg($iface) . " 2>/dev/null | awk '/inet /{print \$2}' | cut -d/ -f1 | head -1"
) ?: '');
// External IP — resolve DDNS domain from conf (no external HTTP; DDNS keeps it current)
$ddnsDomain = $vars[$prefix . 'NETWORK_WATCHDOG_DDNS_DOMAIN'] ?? '';
// External IP — curl ifconfig.me, cached 5 min so we don't hammer it
$extIpCache = '/tmp/vv_ext_ip.cache';
$extIp = '';
if ($ddnsDomain) {
$extIpCache = '/tmp/vv_ext_ip.cache';
if (file_exists($extIpCache) && (time() - filemtime($extIpCache)) < 300) {
$extIp = trim(file_get_contents($extIpCache) ?: '');
} else {
$resolved = trim(shell_exec(
"dig +short " . escapeshellarg($ddnsDomain) . " @1.1.1.1 2>/dev/null | grep -Eo '[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+' | head -1"
) ?: '');
if ($resolved) { $extIp = $resolved; file_put_contents($extIpCache, $extIp); }
if (file_exists($extIpCache) && (time() - filemtime($extIpCache)) < 300) {
$extIp = trim(file_get_contents($extIpCache) ?: '');
} else {
$fetched = trim(shell_exec('curl -sf --max-time 4 https://ifconfig.me 2>/dev/null') ?: '');
if (preg_match('/^\d+\.\d+\.\d+\.\d+$/', $fetched)) {
$extIp = $fetched;
file_put_contents($extIpCache, $extIp);
}
}
// Tailscale IP — read from tailscale0 interface
$tsIp = trim(shell_exec(
"ip -4 addr show tailscale0 2>/dev/null | awk '/inet /{print \$2}' | cut -d/ -f1 | head -1"
) ?: '');
// Tailscale IP — use `tailscale ip` CLI (interface name varies: tailscale0, tailscale1, etc.)
$tsIp = trim(shell_exec('tailscale ip -4 2>/dev/null | head -1') ?: '');
return [
'available' => true,