0.4.3: derive VNC connect address from the VM's bridge IP
This commit is contained in:
1 parent
3c2ee78944
commit
b9ad09e717
4 files changed
+41
-6
No files matched your search
+38
-3
@@ -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('');
|
||||
|
||||
Reference in new issue
Block a user