vnc and nginx generator for wss support

This commit is contained in:
nativemad 2018-10-24 17:53:40 +02:00
parent 102e47026d
commit db5d2307ba
2 changed files with 39 additions and 11 deletions

25
nlvmi
View File

@ -4,13 +4,6 @@
source /etc/nlvmi/nlvmi.conf source /etc/nlvmi/nlvmi.conf
if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi if [ $LOGLEVEL -gt "1" ]; then echo "configuration loaded"; fi
#checking directories
#echo $VMDIRECTORY | while read line; do IFS='|' read -r -a array
# for dir in "${array[@]}"; do
# if [ ! -d $dir ]; then echo "VMDIRECTORY wrong, $dir does not exist"; exit 1; fi
# done
#done <<< "$line"
#check if we are root and wrap if we are not #check if we are root and wrap if we are not
U=`/usr/bin/whoami` U=`/usr/bin/whoami`
if [ $U != "root" ]; then if [ $U != "root" ]; then
@ -177,7 +170,7 @@ function vmstart {
if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up" >>$TMPF; fi if [ ! -z ${array[26]} ]; then TAPDEV3="-netdev tap,ifname=${array[26]},script=no,id=net2"; echo "tunctl -t ${array[26]} -u ${array[2]} && ifconfig ${array[26]} up" >>$TMPF; fi
if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi if [ ! -z ${array[27]} ]; then MACADDR3="-net nic,macaddr=${array[27]},model=virtio,netdev=net2"; fi
if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "/sbin/brctl addif ${array[28]} ${array[26]}" >>$TMPF; fi if [ ! -z ${array[28]} ]; then BRDEV3="${array[28]}"; echo "/sbin/brctl addif ${array[28]} ${array[26]}" >>$TMPF; fi
if [ ! -z ${array[30]} ]; then WEBSOCK=",websocket=${array[30]}"; else WEBSOCK=""; fi if [ ! -z ${array[30]} ]; then WPORT=$((${array[30]} + 5700)); WEBSOCK=",websocket=$WPORT"; else WEBSOCK=""; fi
if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi if [ ! -z ${array[31]} ]; then VNCPASS=",password"; fi
if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi if [ ! -z ${array[29]} ]; then VNCPORT="-vnc :${array[29]}$WEBSOCK$VNCPASS"; fi
COMMAND=(su - ${array[2]} -s /bin/bash -c \"${array[4]} -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -daemonize $DRIVE1$FORMAT1 $DRIVE2$FORMAT2 $DRIVE3$FORMAT3 $CDROM $MACADDR1 $TAPDEV1 $MACADDR2 $TAPDEV2 $MACADDR3 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait\") COMMAND=(su - ${array[2]} -s /bin/bash -c \"${array[4]} -name ${array[1]} -enable-kvm ${array[10]} $CPUTYPE $MEMORY $SMP -device virtio-rng-pci -boot ${array[11]} $USBDEV $KB -daemonize $DRIVE1$FORMAT1 $DRIVE2$FORMAT2 $DRIVE3$FORMAT3 $CDROM $MACADDR1 $TAPDEV1 $MACADDR2 $TAPDEV2 $MACADDR3 $TAPDEV3 $VNCPORT $CUSTOM -pidfile $RUNDIRECTORY/${array[1]}.pid -monitor unix:$RUNDIRECTORY/${array[1]}.mon,server,nowait\")
@ -205,6 +198,17 @@ function vmstart {
rm $TMPF rm $TMPF
fi fi
fi fi
if [ -n $WPORT ] && [ -n $NGINXCONFIGPATH ]; then
echo "localtion /${array[1]}/ {" >$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_pass http://${array[3]}:$WPORT/;" >>$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_http_version 1.1;" >>$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_set_header Upgrade $http_upgrade;" >>$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_set_header Connection \"Upgrade\";" >>$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_read_timeout 61s;" >>$NGINXCONFIGPATH/${array[1]}.conf
echo " proxy_buffering off;" >>$NGINXCONFIGPATH/${array[1]}.conf
echo "}" >>$NGINXCONFIGPATH/${array[1]}.conf
if [ -n $NGINXRESTARTCOMMAND ]; then $NGINXRESTARTCOMMAND; fi
fi
done done
export VMFOUND="yes" export VMFOUND="yes"
} }
@ -307,6 +311,11 @@ if [ $# -gt 0 ]; then
else else
su nlvmi -c "ssh $2 \"rm /home/nlvmi/start*-$3.tmp; rm /home/nlvmi/start*-$3.tmp.sha512\"" su nlvmi -c "ssh $2 \"rm /home/nlvmi/start*-$3.tmp; rm /home/nlvmi/start*-$3.tmp.sha512\""
fi fi
elif [ $1 == "delnginx" ]; then
if [ $2 == "local" ]; then
rm $NGINXCONFIGPATH/$3.conf
if [ -n $NGINXRESTARTCOMMAND ]; then $NGINXRESTARTCOMMAND; fi
fi
elif [ $1 == "start" ]; then elif [ $1 == "start" ]; then
if [ -z $2 ]; then echo "start needs a vmname!"; exit 1; fi if [ -z $2 ]; then echo "start needs a vmname!"; exit 1; fi
VMFOUND="no" VMFOUND="no"

View File

@ -2,10 +2,10 @@
function clean($str) { function clean($str) {
$search = array('&' , '"' , "'" , '<' , '>' ); $search = array('&' , '"' , "'" , '<' , '>' );
$replace = array('&amp;', '&quot;', '&#39;', '&lt;', '&gt;' ); $replace = array('&amp;', '&quot;', '&#39;', '&lt;', '&gt;' );
$str = str_replace($search, $replace, $str); $str = str_replace($search, $replace, $str);
return $str; return $str;
} }
if(isset($_REQUEST['logout'])){ if(isset($_REQUEST['logout'])){
session_start(); session_start();
session_unset(); session_unset();
@ -13,6 +13,7 @@ if(isset($_REQUEST['logout'])){
} else { } else {
session_start(); session_start();
} }
$mode=""; $mode="";
function checkvm($vmname) { function checkvm($vmname) {
@ -22,6 +23,7 @@ function checkvm($vmname) {
} }
return 0; return 0;
} }
function serverdepropdown($server, $val, $what){ function serverdepropdown($server, $val, $what){
if ($what == "cdrom"){ if ($what == "cdrom"){
$dir = "isodirectory"; $dir = "isodirectory";
@ -44,6 +46,7 @@ function serverdepropdown($server, $val, $what){
} }
echo "$enddiv</div></div></div><input type=hidden id=".$what."buttonh name=$what value=\"$val\">"; echo "$enddiv</div></div></div><input type=hidden id=".$what."buttonh name=$what value=\"$val\">";
} }
function formatbuttons($label, $val){ function formatbuttons($label, $val){
echo "<div class=row><div class=col>$label</div><div class=col><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=".$label."button$label data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>"; echo "<div class=row><div class=col>$label</div><div class=col><div class=dropdown><button class=\"btn btn-secondary dropdown-toggle\" type=button id=".$label."button$label data-toggle=dropdown aria-haspopup=true aria-expanded=false>$val</button>";
echo "<div class=dropdown-menu aria-labelledby=formatbutton$label>"; echo "<div class=dropdown-menu aria-labelledby=formatbutton$label>";
@ -170,6 +173,9 @@ if (isset($_SESSION['username'])){
$shellout = shell_exec("/usr/bin/nlvmi delautostart $row[connectstring] $vmname"); $shellout = shell_exec("/usr/bin/nlvmi delautostart $row[connectstring] $vmname");
} }
} }
if (isset($config['NGINXCONFIGPATH'])){
$shellout = shell_exec("/usr/bin/nlvmi delnginx local $vmname");
}
$sqld="DELETE FROM vms WHERE id='$vmid'"; $sqld="DELETE FROM vms WHERE id='$vmid'";
if ($db_handle->query($sqld)){ if ($db_handle->query($sqld)){
echo "VM $vmname deleted!"; echo "VM $vmname deleted!";
@ -676,9 +682,22 @@ if (isset($_SESSION['username'])){
echo "<a href=# onclick=\"post('?', {'$button': '$row[vmname]'});\"class=\"btn $buttonc\">$button</a> "; echo "<a href=# onclick=\"post('?', {'$button': '$row[vmname]'});\"class=\"btn $buttonc\">$button</a> ";
echo "<button class=\"btn btn-danger\" data-delete-text=\"Delete VM $row[vmname]!\" data-delete-id=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\" data-delete-what=vm>Delete VM</button> "; echo "<button class=\"btn btn-danger\" data-delete-text=\"Delete VM $row[vmname]!\" data-delete-id=\"$row[id]\" data-delete-name=\"$row[vmname]\" data-toggle=\"modal\" data-target=\"#confirm-delete\" data-delete-what=vm>Delete VM</button> ";
if ($row['websocket']!=""){ if ($row['websocket']!=""){
$server = gethostname(); $server = $_SERVER['HTTP_HOST'];
$websocketport = $row['websocket'] + 5700;
$wpath="";
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])){
if($_SERVER['HTTP_X_FORWARDED_PROTO']=="https"){
$wpath="$row[vmname]/";
$websocketport="443";
}
} elseif (isset($_SERVER['REQUEST_SCHEME'])){
if ($_SERVER['REQUEST_SCHEME']=="https"){
$wpath="$row[vmname]/";
$websocketport="443";
}
}
if (preg_match('/stop/', $button)) { if (preg_match('/stop/', $button)) {
echo "<a target=_blank href=novnc/vnc.html?path=pussyvm&host=$server class=\"btn btn-success\">VNC</a>"; echo "<a target=_blank href=novnc/vnc.html?host=$server&port=$websocketport&path=$wpath class=\"btn btn-success\">VNC</a>";
} }
} }
echo "</div></div><hr>"; echo "</div></div><hr>";