Browse Source

Add service status on the web admin

Julien VAUBOURG 10 years ago
parent
commit
c856d861cd

+ 0 - 1
TODO

@@ -1,3 +1,2 @@
 * Translate PHP interface in French
 * Support VPN without certificates (only login) -- need tests
-* Add service status on the interface

+ 33 - 19
conf/init_ynh-vpnclient

@@ -55,7 +55,7 @@ is_openvpn_running() {
 
 is_running() {
   ((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
-  && ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app)\
+  && ((! has_hotspot_app && has_ip6delegatedprefix && is_ip6addr_set) || has_hotspot_app || ! has_ip6delegatedprefix)\
   && is_openvpn_running
 }
 
@@ -157,19 +157,29 @@ moulinette_set() {
   fi
 }
 
+# Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
+if [ ! -e /tmp/.ynh-vpnclient-boot ]; then
+  touch /tmp/.ynh-vpnclient-boot
+  service php5-fpm restart
+fi
+
 # Check configuration consistency
 
 if [[ ! "${1}" =~ stop ]]; then
+  exitcode=0
+
   if [ ! -e /etc/openvpn/keys/ca-server.crt ]; then
-    echo "DISABLED SERVICE: You need a CA server (you can add it through the web admin)" >&2
-    exit 1
+    echo "[WARN] You need a CA server (you can add it through the web admin)"
+    exitcode=1
   fi
 
   empty=$(find /etc/openvpn/keys/ -empty -name credentials &> /dev/null | wc -l)
   if [ "${empty}" -gt 0 -a ! -e /etc/openvpn/keys/user.key ]; then
-    echo "DISABLED SERVICE: You need either a client certificate, either a username, or both (you can add one through the web admin)" >&2
-    exit 1
+    echo "[WARN] You need either a client certificate, either a username, or both (you can add one through the web admin)"
+    exitcode=1
   fi
+
+  [ "${exitcode}" -ne 0 ] && exit ${exitcode}
 fi
 
 # Variables
@@ -255,12 +265,6 @@ case "${1}" in
         service ynh-hotspot start
       fi
     fi
-
-    # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
-    if [ ! -e /tmp/.ynh-vpnclient-boot ]; then
-      touch /tmp/.ynh-vpnclient-boot
-      service php5-fpm restart
-    fi
   ;;
   stop)
     echo "[vpnclient] Stopping..."
@@ -289,33 +293,43 @@ case "${1}" in
     exitcode=0
 
     if has_ip6delegatedprefix; then
+      echo "[INFO] IPv6 delegated prefix found"
+
       if ! has_hotspot_app; then
+        echo "[INFO] No Hotspot app detected"
+
         if is_ip6addr_set; then
-          echo "IPv6 address is correctly set"
+          echo "[OK] IPv6 address correctly set"
         else
-          echo "IPv6 address is NOT set"
+          echo "[ERR] No IPv6 address set"
           exitcode=1
         fi
       else
-        echo "Hotspot app detected"
+        echo "[INFO] Hotspot app detected"
+        echo "[INFO] No IPv6 address to set"
       fi
+    else
+      echo "[INFO] No IPv6 delegated prefix found"
     fi
 
     if has_nativeip6; then
+      echo "[INFO] Native IPv6 detected"
+
       if is_serverip6route_set "${new_server_ip6}"; then
-        echo "IPv6 server route is correctly set"
+        echo "[OK] IPv6 server route correctly set"
       else
-        echo "IPv6 server route is NOT set"
+        echo "[ERR] No IPv6 server route set"
         exitcode=1
       fi
     else
-      echo "No native IPv6 detected"
+      echo "[INFO] No native IPv6 detected"
+      echo "[INFO] No IPv6 server route to set"
     fi
 
     if is_openvpn_running; then
-      echo "Openvpn is running"
+      echo "[OK] Openvpn is running"
     else
-      echo "Openvpn is NOT running"
+      echo "[ERR] Openvpn is not running"
       exitcode=1
     fi
 

+ 2 - 2
conf/phpfpm_vpnadmin.conf

@@ -165,7 +165,7 @@ request_terminate_timeout = 600s
 ; dumped to the 'slowlog' file. A value of '0s' means 'off'.
 ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
 ; Default Value: 0
-request_slowlog_timeout = 5s
+request_slowlog_timeout = 0
 
 ; The log file for slow requests
 ; Default Value: not set
@@ -202,7 +202,7 @@ chdir = <TPL:NGINX_REALPATH>
 ; Note: on highloaded environement, this can cause some delay in the page
 ; process time (several ms).
 ; Default Value: no
-catch_workers_output = yes
+catch_workers_output = no
 
 ; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
 ; the current environment.

BIN
screenshot.png


+ 1 - 6
scripts/install

@@ -144,15 +144,10 @@ sudo yunohost service disable openvpn
 
 sudo yunohost service add php5-fpm
 sudo yunohost service enable php5-fpm
-sudo yunohost service stop php5-fpm
-sudo yunohost service start php5-fpm
 
 sudo yunohost service add ynh-vpnclient
 sudo yunohost service enable ynh-vpnclient
-
-if [ ! -z "${crt_server_ca_path}" ]; then
-  sudo service ynh-vpnclient start
-fi
+sudo service ynh-vpnclient start
 
 sudo service nginx reload
 

+ 1 - 1
scripts/remove

@@ -7,7 +7,7 @@ domain=$(sudo yunohost app setting vpnclient domain)
 sudo service ynh-vpnclient stop
 sudo yunohost service remove ynh-vpnclient
 sudo rm -f /etc/init.d/ynh-vpnclient
-sudo rm -f /tmp/.ynh-vpnclient-boot
+sudo rm -f /tmp/.ynh-vpnclient-*
 
 # Remove confs
 sudo rm -f /etc/openvpn/client.conf{.tpl,}

+ 35 - 0
sources/controller.php

@@ -18,6 +18,18 @@ function start_service() {
   return $retcode;
 }
 
+function service_status() {
+  exec('sudo service ynh-vpnclient status', $output);
+
+  return $output;
+}
+
+function service_faststatus() {
+  exec('ip link show tun0', $output, $retcode);
+
+  return $retcode;
+}
+
 function ipv6_expanded($ip) {
   exec('ipv6_expanded '.escapeshellarg($ip), $output);
 
@@ -43,6 +55,7 @@ dispatch('/', function() {
   set('crt_client_exists', file_exists('/etc/openvpn/keys/user.crt'));
   set('crt_client_key_exists', file_exists('/etc/openvpn/keys/user.key'));
   set('crt_server_ca_exists', file_exists('/etc/openvpn/keys/ca-server.crt'));
+  set('faststatus', service_faststatus() == 0);
 
   return render('settings.html.php');
 });
@@ -149,6 +162,28 @@ dispatch_put('/settings', function() {
   redirect_to('/');
 });
 
+dispatch('/status', function() {
+  $status_lines = service_status();
+  $status_list = '';
+
+  foreach($status_lines AS $status_line) {
+    if(preg_match('/^\[INFO\]/', $status_line)) {
+      $status_list .= "<li class='status-info'>${status_line}</li>";
+    }
+    elseif(preg_match('/^\[OK\]/', $status_line)) {
+      $status_list .= "<li class='status-success'>${status_line}</li>";
+    }
+    elseif(preg_match('/^\[WARN\]/', $status_line)) {
+      $status_list .= "<li class='status-warning'>${status_line}</li>";
+    }
+    elseif(preg_match('/^\[ERR\]/', $status_line)) {
+      $status_list .= "<li class='status-danger'>${status_line}</li>";
+    }
+  }
+
+  echo $status_list;
+});
+
 dispatch('/lang/:locale', function($locale = 'en') {
   switch ($locale) {
     case 'fr':

+ 37 - 0
sources/public/css/style.css

@@ -1,3 +1,40 @@
+li.status-info {
+  color: #5BC0DE;
+}
+
+li.status-warning {
+  color: #D9534F;
+}
+
+li.status-danger {
+  color: #D9534F;
+}
+
+li.status-success {
+  color: #5CB85C;
+}
+
+img#status-loading {
+  display: none;
+  margin-right: 5px;
+}
+
+img#save-loading {
+  display: none;
+  margin-left: 5px;
+}
+
+div#status {
+  display: none;
+  margin-top: 10px;
+}
+
+div#status ul {
+  list-style-type: none;
+  padding: 0;
+  margin: 0;
+}
+
 a.btn-danger span {
   color: #eee;
 }

BIN
sources/public/img/loading.gif


+ 21 - 0
sources/public/js/custom.js

@@ -39,4 +39,25 @@ $(document).ready(function() {
 
     $(choosertxtid).val($(this).val());
   });
+
+  $('#save').click(function() {
+    $(this).prop('disabled', true);
+    $('#save-loading').show();
+  });
+
+  $('#status .close').click(function() {
+    $(this).parent().hide();
+  });
+
+  $('#statusbtn').click(function() {
+    $('#status-loading').show();
+
+    $.ajax({
+      url: '?/status',
+    }).done(function(data) {
+      $('#status-loading').hide();
+      $('#status-text').html('<ul>' + data + '</ul>');
+      $('#status').show('slow');
+    });
+  });
 });

+ 13 - 1
sources/views/settings.html.php

@@ -1,4 +1,16 @@
 <h2><?= T_("VPN Client Configuration") ?></h2>
+<?php if($faststatus): ?>
+  <span class="label label-success" data-toggle="tooltip" data-title="<?= T_('This is a fast status. Click on More details to show the complete status.') ?>"><?= T_('Running') ?></span>
+<?php else: ?>
+  <span class="label label-danger" data-toggle="tooltip" data-title="<?= T_('This is a fast status. Click on More details to show the complete status.') ?>"><?= T_('Not Running') ?></span>
+<?php endif; ?>
+
+ &nbsp; <img src="public/img/loading.gif" id="status-loading" alt="Loading..." /><a href="#" id="statusbtn" data-toggle="tooltip" data-title="<?= T_('Loading complete status may take a few minutes. Be patient.') ?>"><?= T_('More details') ?></a>
+
+<div id="status" class="alert alert-dismissible alert-info fade in" style="margin-top: 10px" role="alert">
+  <button type="button" class="close"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
+  <div id="status-text"></div>
+</div>
 
 <hr />
 
@@ -142,7 +154,7 @@
 
       <div class="form-group">
         <div style="text-align: center">
-          <button type="submit" class="btn btn-default" data-toggle="tooltip" data-title="<?= T_('Reloading may take a few minutes. Be patient.') ?>"><?= T_('Save and reload') ?></button>
+          <button type="submit" class="btn btn-default" data-toggle="tooltip" id="save" data-title="<?= T_('Reloading may take a few minutes. Be patient.') ?>"><?= T_('Save and reload') ?></button> <img src="public/img/loading.gif" id="save-loading" alt="Loading..." />
         </div>
       </div>
     </form>