From b35ce1134471fa0acf50fa82a733043beb809632 Mon Sep 17 00:00:00 2001 From: SimonFair <39065407+SimonFair@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:06:11 +0100 Subject: [PATCH] Change get VM IP to a function. --- .../dynamix.vm.manager/include/VMajax.php | 40 +++++++------------ .../include/libvirt_helpers.php | 26 ++++++++++++ 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/emhttp/plugins/dynamix.vm.manager/include/VMajax.php b/emhttp/plugins/dynamix.vm.manager/include/VMajax.php index e589f1c7d..f8ac42c14 100644 --- a/emhttp/plugins/dynamix.vm.manager/include/VMajax.php +++ b/emhttp/plugins/dynamix.vm.manager/include/VMajax.php @@ -128,6 +128,20 @@ case 'domain-start-consoleRV': $arrResponse['vvfile'] = $vvfile; break; +case 'domain-consoleRDP': + requireLibvirt(); + $dom = $lv->get_domain_by_name($domName); + $rdpvarray = array() ; + $myIP=get_vm_ip($dom); + if ($myIP == NULL) {$arrResponse['error'] = "No IP, guest agent not installed?"; break; } + $rdparray[] = "full address:s: $myIP\n"; + #$rdparray[] = "administrative session:1\n"; + if (!is_dir("/mnt/user/system/remoteviewer")) mkdir("/mnt/user/system/remoteviewer") ; + $rdpfile = "/mnt/user/system/remoteviewer/rv"._var($_SERVER,'HTTP_HOST').".$port.rdp" ; + file_put_contents($rdpfile,$rdparray) ; + $arrResponse['vvfile'] = $rdpfile; + break; + case 'domain-consoleRV': requireLibvirt(); $dom = $lv->get_domain_by_name($domName); @@ -151,30 +165,7 @@ case 'domain-openWebUI': requireLibvirt(); $dom = $lv->get_domain_by_name($domName); $WebUI = unscript(_var($_REQUEST,'vmrcurl')); - $gastate = getgastate($dom); - if ($gastate == "connected") { - $ip = $lv->domain_interface_addresses($dom, 1); - #$arrResponse['other'] = "Connected $WebUI"; - $gastate = getgastate($dom); - if ($gastate == "connected") { - $myIP=null; - $ip = $lv->domain_interface_addresses($dom, 1); - if ($ip != false) { - $duplicates = []; // hide duplicate interface names - foreach ($ip as $arrIP) { - $ipname = $arrIP["name"]; - if (preg_match('/^(lo|Loopback)/',$ipname)) continue; // omit loopback interface - $iplist = $arrIP["addrs"]; - foreach ($iplist as $arraddr) { - $myIP= $arraddr["addr"]; - if (preg_match('/^f[c-f]/',$ipaddrval)) continue; // omit ipv6 private addresses - if (!in_array($ipnamemac,$duplicates)) $duplicates[] = $ipnamemac; else $ipnamemac = ""; - break 2; - } - } - } - } - } + $myIP = get_vm_ip($dom); if (strpos($WebUI,"[IP]") && $myIP == NULL) $arrResponse['error'] = "No IP, guest agent not installed?"; $WebUI = preg_replace("%\[IP\]%", $myIP, $WebUI); $vmnamehypen = str_replace(" ","-",$domName); @@ -183,7 +174,6 @@ case 'domain-openWebUI': $ConfigPort = $matches[1] ?? ''; $WebUI = preg_replace("%\[PORT:\d+\]%", $ConfigPort, $WebUI); } - $arrResponse['vmrcurl'] = $WebUI; break; diff --git a/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php b/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php index a9901f231..e8f31708e 100644 --- a/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php +++ b/emhttp/plugins/dynamix.vm.manager/include/libvirt_helpers.php @@ -2763,4 +2763,30 @@ function qemu_log($vm,$m) { file_put_contents("/var/log/libvirt/qemu/$vm.log",$m."\n",FILE_APPEND); } +function get_vm_ip($dom) { + global $lv; + $myIP=null; + $gastate = getgastate($dom); + if ($gastate == "connected") { + $ip = $lv->domain_interface_addresses($dom, 1); + $gastate = getgastate($dom); + if ($gastate == "connected") { + $ip = $lv->domain_interface_addresses($dom, 1); + if ($ip != false) { + foreach ($ip as $arrIP) { + $ipname = $arrIP["name"]; + if (preg_match('/^(lo|Loopback)/',$ipname)) continue; // omit loopback interface + $iplist = $arrIP["addrs"]; + foreach ($iplist as $arraddr) { + $myIP= $arraddr["addr"]; + if (preg_match('/^f[c-f]/',$myIP)) continue; // omit ipv6 private addresses + break 2; + } + } + } + } + } + return $myIP; +} + ?>