From b9ad09e7177ca75f13c3166384601787989fe599 Mon Sep 17 00:00:00 2001 From: Claw Date: Tue, 17 Nov 2026 20:50:26 +0100 Subject: [PATCH] 0.4.3: derive VNC connect address from the VM's bridge IP --- bin/vmm-lib.php | 41 +++++++++++++++++++++++++++++++++++--- gui/ext/vmmngr/vmmngr.conf | 2 +- gui/vmmngr_vm.php | 2 +- version.txt | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/bin/vmm-lib.php b/bin/vmm-lib.php index b8c9a99..6ad5db5 100644 --- a/bin/vmm-lib.php +++ b/bin/vmm-lib.php @@ -830,23 +830,58 @@ function vmm_delete_data_file(string $dir, string $name): array * Turn a bhyve VNC bind address ("0.0.0.0:5901") into something a human can * actually connect to. 0.0.0.0 is a listen address, not a destination. */ -function vmm_vnc_connect(string $bind): string +function vmm_vnc_connect(string $bind, string $host = ''): string { $bind = trim($bind); if ($bind === '' || $bind === '-') { return ''; } - [$host, $port] = array_pad(explode(':', $bind, 2), 2, ''); + [$bindHost, $port] = array_pad(explode(':', $bind, 2), 2, ''); $port = (int)$port; if ($port <= 0) { return ''; } - if ($host === '0.0.0.0' || $host === '' || $host === '*') { + if ($bindHost !== '0.0.0.0' && $bindHost !== '' && $bindHost !== '*') { + return $bindHost . ':' . $port; + } + if ($host === '') { $host = trim((string)@shell_exec('hostname -f 2>/dev/null')) ?: (gethostname() ?: 'localhost'); } return $host . ':' . $port; } +/** + * IP of the bridge that carries this VM's traffic (the address a client on + * that network actually reaches). Falls back to the host name. + */ +function vmm_vnc_host_for_vm(string $name): string +{ + $switchName = ''; + $info = vmm_info($name); + foreach (($info['sections']['network-interface'] ?? []) as $nic) { + if (!empty($nic['data']['virtual-switch'])) { + $switchName = (string)$nic['data']['virtual-switch']; + break; + } + } + $iface = ''; + foreach (vmm_switches() as $sw) { + if ($switchName === '' || (string)($sw['name'] ?? '') === $switchName) { + $iface = (string)($sw['iface'] ?? ''); + if ($iface !== '' && $iface !== 'n/a') { + break; + } + } + } + if ($iface !== '' && $iface !== 'n/a') { + $out = trim((string)@shell_exec('/sbin/ifconfig ' . escapeshellarg($iface) . ' inet 2>/dev/null')); + if (preg_match('/inet (\d+\.\d+\.\d+\.\d+)/', $out, $m)) { + return $m[1]; + } + } + return trim((string)@shell_exec('hostname -f 2>/dev/null')) ?: (gethostname() ?: 'localhost'); +} + function vmm_vm_conf_path(string $name): string { $dir = vmm_data_dir(''); diff --git a/gui/ext/vmmngr/vmmngr.conf b/gui/ext/vmmngr/vmmngr.conf index fae2e02..3e4bc7a 100644 --- a/gui/ext/vmmngr/vmmngr.conf +++ b/gui/ext/vmmngr/vmmngr.conf @@ -1,6 +1,6 @@ { "appname": "VM Manager", - "version": "0.4.2", + "version": "0.4.3", "rootfolder": "/mnt/Daten/extensions/vmmngr", "vm_source": "auto", "bridges": ["ix0bridge"], diff --git a/gui/vmmngr_vm.php b/gui/vmmngr_vm.php index 9fb0a21..ad392cb 100644 --- a/gui/vmmngr_vm.php +++ b/gui/vmmngr_vm.php @@ -140,7 +140,7 @@ vmm_nav('dashboard'); $vncBind = (string)$b['data']['vnc']; } } - $vncTarget = vmm_vnc_connect($vncBind); + $vncTarget = $vncBind !== '' ? vmm_vnc_connect($vncBind, vmm_vnc_host_for_vm($name)) : ''; ?> diff --git a/version.txt b/version.txt index 2b7c5ae..17b2ccd 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.4.2 +0.4.3