From 91c5c7d51f8bf2de5cde3b937333c8a69a5ee1c5 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Thu, 2 Aug 2018 14:41:21 +0200
Subject: [PATCH] Multiple fix by removing timeouts as much as possible

---
 wol/scripts/wakeonlanservice.sh | 20 +++++---------------
 wol/www/boot.php                | 23 ++++++++++++++---------
 wol/www/index.php               |  7 +++++--
 3 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/wol/scripts/wakeonlanservice.sh b/wol/scripts/wakeonlanservice.sh
index f1d9b48..0474f77 100755
--- a/wol/scripts/wakeonlanservice.sh
+++ b/wol/scripts/wakeonlanservice.sh
@@ -24,27 +24,17 @@ getip(){
 IP=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1)
 broadcast=$(ip a | awk '/inet.*eth1/ {print $4}' | cut -d / -f 1)
 subnet=$(ip a | awk '/inet.*containers/ {print $2}' | cut -d / -f 1 | cut -d . -f 1-3)
-LIMIT=20
 while true
 do
     MAC=`netcat -l $IP 9999`
+    echo "$MAC"
     if [ $(echo "$MAC" | egrep -c "^([0-9a-f]{2}:?){6}$") -eq 1 ]
     then
         wakeonlan -i $broadcast "$MAC"
-        bootedip=""
-        timeout=0
-        sleep 25
-        while [ -z "$bootedip" ]
-        do
-            bootedip=`getip $MAC`
-            sleep 5
-            timeout=$(($timeout +1))
-            if [ $timeout -ge $LIMIT ]
-            then
-                $bootedip="ERROR"
-                break
-            fi
-        done
+        sleep 5
+        bootedip=`getip $MAC`
+        echo "Sending ip"
         echo "$bootedip" | netcat $subnet.51 9999
+        echo "Done"
     fi
 done
diff --git a/wol/www/boot.php b/wol/www/boot.php
index 8890058..2360eb5 100644
--- a/wol/www/boot.php
+++ b/wol/www/boot.php
@@ -23,13 +23,18 @@
         echo "<p>Adresse MAC invalide : ".$args."</p>";
     }else{
         $cmd = "../scripts/sendmac.sh";
-        echo "<p> Démarrage de la machine ".$args." terminé !</p>";
-        echo "<p>Pour vous y connecter, téléchargez ";
-        system("cp www/config.rdp.sample www/config.rdp");
-        system("echo \"full address:s:\"".$_SERVER["HTTP_HOST"]." >> config.rdp");
-        echo '<a href="config.rdp';
-        echo '">ce fichier</a> et lancez le.<br />';
-        echo "Cela ouvrira XRDP, il vous suffira alors d'indiquer l'adresse IP <span style='color:blue;font-weight:bold'>".shell_exec("$cmd $args")."</span>";
-        echo "et le mot de passe adéquat";
-        echo "</p>";
+        $ip=shell_exec("$cmd $args");
+        if(!empty(trim($ip))){
+            echo "<p> Démarrage de la machine ".$args." terminé !</p>";
+            echo "<p>Pour vous y connecter, téléchargez ";
+            system("cp www/config.rdp.sample www/config.rdp");
+            system("echo \"full address:s:\"".$_SERVER["HTTP_HOST"]." >> config.rdp");
+            echo '<a href="config.rdp';
+            echo '">ce fichier</a> et lancez le.<br />';
+            echo "Cela ouvrira XRDP, il vous suffira alors d'indiquer l'adresse IP <span style='color:blue;font-weight:bold'>".$ip."</span>";
+            echo "et le mot de passe adéquat";
+            echo "</p>";
+        }else{
+            header('HTTP/1.1 500 Internal Server Error');
+        }
     }
diff --git a/wol/www/index.php b/wol/www/index.php
index 473227d..c0ee2cf 100644
--- a/wol/www/index.php
+++ b/wol/www/index.php
@@ -41,10 +41,13 @@
             type: "POST",
                 data: '<?php echo "mac=$mac"?>',
                 url: 'boot.php',
-                timeout: 60000,
+                timeout: 30000,
                 error: function(reponse){
                     if(retry > 0){
+                        $("#result").html("Veuillez patienter encore un peu (max "+retry*4+" sec)") ;
                         sendpost(retry-1);
+                    }else{
+                        $("#result").html("Erreur : IP non trouvée après 120 secondes") ;
                     }
                 },
                 success: function(response) {
@@ -52,7 +55,7 @@
                 }
         });
         }
-        sendpost(1);
+        sendpost(24);
         </script>
 <?php
         break;
-- 
GitLab