Julien VAUBOURG 10 years ago
parent
commit
51903e3678

+ 66 - 81
conf/init_ynh-torclient

@@ -12,19 +12,16 @@
 # Functions
 ## State functions
 
-has_vpnclient_app() {
-  [ -e /tmp/.ynh-vpnclient-started ]
-}
-
 has_torclient_app() {
   [ -e /tmp/.ynh-torclient-started ]
 }
+
 has_hotspot_app() {
   [ -e /tmp/.ynh-hotspot-started ]
 }
 
 is_nat_set() {
-  iptables -nvt nat -L PREROUTING | grep REDIRECT | grep -q "${torclient_wifi_device_old}"
+  iptables -nvt nat -L PREROUTING | grep REDIRECT | grep -q "${ynh_wifi_device}"
 }
 
 is_tor_running() {
@@ -36,8 +33,8 @@ is_running() {
 }
 
 set_nat() {
-  iptables -t nat -A PREROUTING -i ${torclient_wifi_device} -p udp --dport 53 -j REDIRECT --to-ports 9053
-  iptables -t nat -A PREROUTING -i ${torclient_wifi_device} -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
+  iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j REDIRECT --to-ports 9053
+  iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
 }
 
 set_forwarding() {
@@ -48,17 +45,19 @@ set_forwarding() {
 unset_nat() {
   internet_device=${1}
 
-  iptables -t nat -D PREROUTING -i ${torclient_wifi_device_old} -p udp --dport 53 -j REDIRECT --to-ports 9053
-  iptables -t nat -D PREROUTING -i ${torclient_wifi_device_old} -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
+  iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j REDIRECT --to-ports 9053
+  iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
 }
 
 stop_tor() {
-  service tor stop
+  service tor stop &> /dev/null
 }
 
 start_tor() {
-  cp /etc/tor/torrc.tpl /etc/tor/torrc
-  sed "s|<TPL:TOR_NETWORK>|${torclient_wifi_prefix}|g" -i /etc/tor/torrc
+  cp /etc/tor/torrc{.tpl,}
+
+  sed "s|<TPL:TOR_NETWORK>|${ynh_wifi_prefix}|g" -i /etc/tor/torrc
+
   service tor start
 }
 
@@ -67,19 +66,6 @@ start_tor() {
 moulinette_get() {
   var=${1}
 
-  value=$(yunohost app setting hotspot "${var}")
-
-  if [[ "${value}" =~ "An instance is already running" ]]; then
-    echo "${value}" >&2
-    exit 1
-  fi
-
-  echo "${value}"
-} 
-
-moulinette_torclient_get() {
-  var=${1}
-
   value=$(yunohost app setting torclient "${var}")
 
   if [[ "${value}" =~ "An instance is already running" ]]; then
@@ -102,12 +88,29 @@ moulinette_set() {
   fi
 }
 
+moulinette_hotspot_get() {
+  var=${1}
+
+  value=$(yunohost app setting hotspot "${var}")
+
+  if [[ "${value}" =~ "An instance is already running" ]]; then
+    echo "${value}" >&2
+    exit 1
+  fi
+
+  echo "${value}"
+} 
+
 do_start() {
   if is_running; then
     echo "Already started"
+  elif [ "${ynh_service_enabled}" -eq 0 ]; then
+    echo "Disabled service"
+  elif ! has_hotspot_app; then
+    echo "[ERR] Hotspot is not running"
   else
     echo "[torclient] Starting..."
-    touch /tmp/.ynh-tor-started
+    touch /tmp/.ynh-torclient-started
 
     # Run tor
     if ! is_tor_running; then
@@ -121,35 +124,36 @@ do_start() {
       set_nat
     fi
   fi
-  moulinette_set wifi_num_old $torclient_wifi_num
 }
 
 do_stop() {
-  if ! is_running; then
-    echo "Already stoped"
-  else
-    echo "[torclient] Stopping..."
-    rm /tmp/.ynh-tor-started
-  
-    if is_nat_set; then
-      echo "Unset NAT"
-      unset_nat
-    fi
+  echo "[torclient] Stopping..."
+  rm /tmp/.ynh-torclient-started
 
-    if is_tor_running; then
-      echo "Stop Tor"
-      stop_tor
-    fi
-  
-    if has_vpnclient_app; then
-      service ynh-vpnclient start
-    fi
+  if is_nat_set; then
+    echo "Unset NAT"
+    unset_nat
+  fi
+
+  if is_tor_running; then
+    echo "Stop Tor"
+    stop_tor
   fi
 }
 
 do_status() {
   exitcode=0
 
+  if [ "${ynh_service_enabled}" -eq 0 ]; then
+    echo "[ERR] Tor Client Service disabled"
+    exitcode=1
+  fi
+
+  if ! has_hotspot_app; then
+    echo "[ERR] Hotspot is not running"
+    exitcode=1
+  fi
+
   if is_tor_running; then
     echo "[OK] Tor is running"
   else
@@ -160,7 +164,7 @@ do_status() {
   if is_nat_set; then
     echo "[OK] IPv4 nat rules set"
   else
-    echo "[ERR] No IPv4 nat rules not set"
+    echo "[ERR] No IPv4 nat rules set"
     exitcode=1
   fi
 
@@ -168,50 +172,32 @@ do_status() {
 }
 
 # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
-if [ ! -e /tmp/.ynh-hotspot-boot ]; then
-  touch /tmp/.ynh-hotspot-boot
+if [ ! -e /tmp/.ynh-torclient-boot ]; then
+  touch /tmp/.ynh-torclient-boot
   service php5-fpm restart
 fi
 
 # Variables
 
-echo "Retrieving Yunohost settings... "
+echo -n "Retrieving Yunohost settings... "
 
-torclient_wifi_num=$(moulinette_torclient_get wifi_num)
-if [ "$torclient_wifi_num" == "-1" ] ; then
-  echo "[ERR] SSID is not set for torclient"
-  exit 1
-fi
-if [ ${torclient_wifi_num} -eq 0 ]; then
-  torclient_wifi_device=$(moulinette_get wifi_device)
-else
-  torclient_wifi_device="hotspot${torclient_wifi_num}"
-fi
-if [ ! $torclient_wifi_device ] ; then
-  echo "[ERR] Device not found for torclient"
-  exit 1
-fi
+ynh_service_enabled=$(moulinette_get service_enabled)
+ynh_wifi_device_id=$(moulinette_get wifi_device_id)
 
-torclient_wifi_num_old=$(moulinette_torclient_get wifi_num_old)
-if [ ${torclient_wifi_num_old} -eq 0 ]; then
-  torclient_wifi_device_old=$(moulinette_get wifi_device)
+if [ "${ynh_wifi_device_id}" -eq 0 ]; then
+  ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
 else
-  torclient_wifi_device_old="hotspot${torclient_wifi_num_old}"
-fi
-if [ ! $torclient_wifi_device_old ] ; then
-  echo "[ERR] Old device not found for torclient"
-  exit 1
-fi
-if [ "$torclient_wifi_num_old" == "-1" ] && [ "$1" == "stop" ] ; then
-  echo "Torclient never started"
-  exit 1
+  ynh_wifi_device="hotspot${ynh_wifi_device_id}"
 fi
 
-IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_get wifi_ssid)"
-IFS='|' read -a ynh_wifi_prefix <<< "$(moulinette_get ip4_nat_prefix)"
-torclient_wifi_prefix=${ynh_wifi_prefix[$torclient_wifi_num]}
-torclient_wifi_ssid=${ynh_wifi_ssid[$torclient_wifi_num]}
-echo "Torclient will be active on $torclient_wifi_device device and $torclient_wifi_ssid SSID with $torclient_wifi_prefix prefix"
+echo OK
+
+IFS='|' read -a ynh_wifi_ssid <<< "$(moulinette_hotspot_get wifi_ssid)"
+IFS='|' read -a ynh_wifi_prefix <<< "$(moulinette_hotspot_get ip4_nat_prefix)"
+ynh_wifi_prefix=${ynh_wifi_prefix[$ynh_wifi_device_id]}
+ynh_wifi_ssid=${ynh_wifi_ssid[$ynh_wifi_device_id]}
+
+#echo "Torclient will be active on $ynh_wifi_device device and $ynh_wifi_ssid SSID with $ynh_wifi_prefix prefix"
 
 case "$1" in
   start)
@@ -233,5 +219,4 @@ case "$1" in
   ;;
 esac
 
-
 exit 0

+ 5 - 7
scripts/install

@@ -43,7 +43,8 @@ sudo apt-get update
 DEBIAN_FRONTEND=noninteractive sudo apt-get --assume-yes --force-yes install ${packages}
 
 # Save arguments
-#sudo yunohost app setting torclient server_name -v "${server_name}"
+sudo yunohost app setting torclient service_enabled -v 0
+sudo yunohost app setting torclient wifi_device_id -v 0
 
 sudo install -o root -g root -m 0644 ../conf/torrc /etc/tor/torrc.tpl
 sudo install -b -o root -g root -m 0644 ../conf/nginx_torclient.conf "/etc/nginx/conf.d/${domain}.d/torclient.conf"
@@ -89,14 +90,11 @@ sudo service nginx reload
 # Update SSO for vpnadmin
 sudo yunohost app ssowatconf
 
-# Stop tor client
-sudo service tor stop
+# Set default inits
+sudo service tor stop &> /dev/null
 sudo insserv -r tor
-#sudo service ynh-torclient start
-sudo yunohost app setting torclient wifi_num -v -1
-sudo yunohost app setting torclient wifi_num_old -v -1
 
-# Start tor client at boot 
 sudo insserv ynh-torclient
+sudo service torclient start
 
 exit 0

+ 0 - 4
scripts/remove

@@ -47,8 +47,4 @@ sudo rm -rf /var/www/torclient/
 sudo rm -rf /etc/apt/sources.list.d/torproject.list 
 sudo apt-get update
 
-# Remove packets
-# The yunohost policy is currently to not uninstall packets (dependency problems)
-#sudo apt-get --assume-yes --force-yes remove tor torsocks
-
 exit 0

+ 36 - 41
sources/controller.php

@@ -1,6 +1,6 @@
 <?php
 
-function moulinette_get_hotspot($var) {
+function moulinette_hotspot_get($var) {
   return htmlspecialchars(exec('sudo yunohost app setting hotspot '.escapeshellarg($var)));
 }
 
@@ -16,8 +16,8 @@ function stop_service() {
   exec('sudo service ynh-torclient stop');
 }
 
-function restart_service() {
-  exec('sudo service ynh-torclient restart', $output, $retcode);
+function start_service() {
+  exec('sudo service ynh-torclient start', $output, $retcode);
 
   return $retcode;
 }
@@ -34,61 +34,56 @@ function service_faststatus() {
   return $retcode;
 }
 
-function getArray($str) {
-  return explode('|', $str);
-}
-
 dispatch('/', function() {
+  $ssids = explode('|', moulinette_hotspot_get('wifi_ssid'));
+  $wifi_device_id = moulinette_get('wifi_device_id');
+  $wifi_ssid_list = '';
+  $wifi_ssid = '';
 
-  $wifi_ssid_list='';
-  $ssids = getArray(moulinette_get_hotspot('wifi_ssid'));
-  $multissid = moulinette_get_hotspot('multissid');
-  $wifi_num = moulinette_get('wifi_num');
+  for($i = 0; $i < count($ssids); $i++) {
+    $active = '';
+
+    if($i == $wifi_device_id) {
+      $active = 'class="active"';
+      $wifi_ssid = htmlentities($ssids[$i]);
+    }
 
-  for($i = 0; $i < $multissid; $i++) {
-    $active = ($i == $wifi_num) ? 'class="active"' : '';
-    $wifi_ssid_list .= "<li $active><a href='#'>$ssids[$i]</a></li>\n";
+    $wifi_ssid_list .= "<li $active data-device-id='$i'><a href='javascript:;'>".htmlentities($ssids[$i]).'</a></li>';
   }
-  
-  if($wifi_num == -1) {
-    $ssid="";
-  } else {
-    $ssid=$ssids[$wifi_num];
+
+  if(empty($wifi_ssid)) {
+    $wifi_ssid = '<em>'.T_("None").'</em>';
   }
-  set('wifi_ssid', $ssid);
-  set('status', service_faststatus() == 0);
+
+  set('faststatus', service_faststatus() == 0);
+  set('service_enabled', moulinette_get('service_enabled'));
+  set('wifi_device_id', $wifi_device_id);
+  set('wifi_ssid', $wifi_ssid);
   set('wifi_ssid_list', $wifi_ssid_list);
 
   return render('settings.html.php');
 });
 
 dispatch_put('/settings', function() {
+  $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
 
-  $status = isset($_POST['status']) ? 1 : 0;
-  $ssids = getArray(moulinette_get_hotspot('wifi_ssid'));
-  $wifi_ssid = $_POST['wifi_ssid'];
-  $interfaces = getArray(moulinette_get_hotspot('wifi_ssid'));
-  $multissid = moulinette_get_hotspot('multissid');
+  stop_service();
 
-  for($i = 0; $i < $multissid; $i++) {
-    if($ssids[$i] == $wifi_ssid) {
-      moulinette_set('wifi_num', $i);
-    }
-  }
+  moulinette_set('service_enabled', $service_enabled);
 
-  moulinette_set('status', $status);
-  moulinette_set('wifi_ssid', $wifi_ssid);
+  if($service_enabled == 1) {
+    moulinette_set('wifi_device_id', $_POST['wifi_device_id']);
 
-  if($status == 1) {
-    $retcode = restart_service();
-  } else {
-    $retcode = stop_service();
-  }
+     $retcode = start_service();
+
+    if($retcode == 0) {
+      flash('success', T_('Configuration updated and service successfully reloaded'));
+    } else {
+      flash('error', T_('Configuration updated but service reload failed'));
+    }
 
-  if($retcode == 0) {
-    flash('success', T_('Configuration updated and service successfully reloaded'));
   } else {
-    flash('error', T_('Configuration updated but service reload failed'));
+      flash('success', T_('Service successfully disabled'));
   }
 
   redirect:

+ 4 - 4
sources/public/js/custom.js

@@ -18,14 +18,14 @@ $(document).ready(function() {
     var items = menu.children();
     var button = menu.prev();
     var input = button.prev();
-
+  
     items.removeClass('active');
     $(this).addClass('active');
-
+  
     button.text($(this).text());
     button.append(' <span class="caret"></span>');
-
-    input.attr('value', $(this).text());
+  
+    input.val($(this).data('device-id'));
   });
 
   $('.switch').bootstrapToggle();

+ 5 - 5
sources/views/layout.html.php

@@ -6,7 +6,7 @@
 <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
-  <title>Wifi Hotspot <? echo (isset($title)) ? "| ".$title : "" ?></title>
+  <title><?= T_("Tor Client") ?> <?= isset($title) ? "| $title" : '' ?></title>
   <meta name="viewport" content="width=device-width">
   <link media="all" type="text/css" href="<?= PUBLIC_DIR ?>/bootstrap/css/bootstrap.min.css" rel="stylesheet">
   <link media="all" type="text/css" href="<?= PUBLIC_DIR ?>/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
@@ -19,22 +19,22 @@
 </head>
 <body>
   <div class="container">
-    <? if(isset($flash['error'])): ?>
+    <?php if(isset($flash['error'])): ?>
       <div class="alert alert-dismissible alert-danger fade in" style="margin-top: 20px" role="alert">
         <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
         <strong><?= T_('Error') ?>:</strong> <?= $flash['error'] ?>
       </div>
-    <? elseif(isset($flash['notice'])): ?>
+    <?php elseif(isset($flash['notice'])): ?>
       <div class="alert alert-dismissible alert-info fade in" style="margin-top: 20px" role="alert">
         <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
         <strong><?= T_('Notice') ?>:</strong> <?= $flash['notice'] ?>
       </div>
-    <? elseif(isset($flash['success'])): ?>
+    <?php elseif(isset($flash['success'])): ?>
       <div class="alert alert-dismissible alert-success fade in" style="margin-top: 20px" role="alert">
         <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
         <?= $flash['success'] ?>
       </div>
-    <? endif; ?>
+    <?php endif; ?>
 
     <?= $content?>
 

+ 10 - 8
sources/views/settings.html.php

@@ -1,5 +1,5 @@
 <h2><?= T_("Tor client configuration") ?></h2>
-<?php if($status): ?>
+<?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>
@@ -21,31 +21,33 @@
 
       <div class="panel panel-default">
         <div class="panel-heading">
-          <h3 class="panel-title"><?= T_("Wifi") ?></h3>
+          <h3 class="panel-title"><?= T_("Service") ?></h3>
         </div>
 
         <div style="padding: 14px 14px 0 10px">
   
           <div class="form-group">
-            <label for="tro_active" class="col-sm-3 control-label"><?= T_('Active') ?></label>
-            <div class="col-sm-9 input-group-btn" data-toggle="tooltip" data-title="<?= T_('set tor enable or disable') ?>">
+            <label for="service_enabled" class="col-sm-3 control-label"><?= T_('Tor Enabled') ?></label>
+            <div class="col-sm-9 input-group-btn">
               <div class="input-group">
-                <input type="checkbox" class="form-control switch" name="status" id="status" value="1" <?= $status == 1 ? 'checked="checked"' : '' ?> />
+                <input type="checkbox" class="form-control switch" name="service_enabled" id="service_enabled" value="1" <?= $service_enabled == 1 ? 'checked="checked"' : '' ?> />
               </div>
             </div>
           </div>
+
           <div class="form-group">
-            <label for="wifi_ssid" class="col-sm-3 control-label"><?= T_('Associated Hotspot') ?></label>
+            <label for="wifi_device_id" class="col-sm-3 control-label"><?= T_('Associated Hotspot') ?></label>
             <div class="col-sm-9 input-group-btn">
               <div class="input-group">
-                  <input type="text" name="wifi_ssid" id="wifi_ssid" value="<?= $wifi_ssid ?>" style="display: none" />
+                  <input type="text" name="wifi_device_id" id="wifi_device_id" value="<?= $wifi_device_id ?>" style="display: none" />
                   <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><?= $wifi_ssid ?> <span class="caret"></span></button>
-                  <ul class="dropdown-menu dropdown-menu-left" role="menu">
+                  <ul class="dropdown-menu dropdown-menu-left" id="deviceidlist" role="menu">
                     <?= $wifi_ssid_list ?>
                   </ul>
               </div>
             </div>
           </div>
+
 	</div>
       </div>
       <div class="form-group">