Browse Source

Ready for Jessie (+ some improvements)

Julien VAUBOURG 10 years ago
parent
commit
7b6af705fc
7 changed files with 111 additions and 68 deletions
  1. 10 3
      README.md
  2. 54 33
      conf/ynh-torclient
  3. 2 4
      conf/ynh-torclient.service
  4. 16 12
      scripts/install
  5. 5 7
      scripts/remove
  6. 16 8
      sources/controller.php
  7. 8 1
      sources/views/settings.html.php

+ 10 - 3
README.md

@@ -1,9 +1,16 @@
 # Tor Client
+## Overview
+
+**Warning: work in progress**
 
 Tor Client app for [YunoHost](http://yunohost.org/).
 
-* Install a Tor connection on your self-hosted server.
-* Useful for hosting your server behind a filtered (and/or non-neutral) internet access.
-* You should install [Hotspot app for YunoHost](https://github.com/jvaubourg/hotspot_ynh) before in order to broadcast your Tor access by Wifi.
+Install a Tor tunnel, for broadcasting it through a wifi hotspot.
 
 This YunoHost app is a part of the "[La Brique Internet](http://labriqueinter.net)" project but can be used independently.
+
+## Prerequisites
+
+* Debian Jessie
+* YunoHost >= 2.2.0
+* [Hotspot app for YunoHost](https://github.com/jvaubourg/hotspot_ynh)

+ 54 - 33
conf/ynh-torclient

@@ -33,7 +33,7 @@ is_nat_set() {
 }
 
 is_tor_running() {
-  systemctl is-active tor --quiet &> /dev/null
+  systemctl is-active tor &> /dev/null
 }
 
 is_running() {
@@ -58,7 +58,7 @@ unset_nat() {
 }
 
 stop_tor() {
-  systemctl stop tor --quiet &> /dev/null
+  systemctl stop tor
 }
 
 start_tor() {
@@ -66,20 +66,24 @@ start_tor() {
 
   sed "s|<TPL:TOR_NETWORK>|${ynh_wifi_prefix}|g" -i /etc/tor/torrc
 
-  systemctl start tor --quiet
+  systemctl start tor
 }
 
 ## Tools
 
 moulinette_get() {
   var=${1}
+  gotcha=0
 
-  value=$(yunohost app setting torclient "${var}")
+  while [ "${gotcha}" -eq 0 ]; do
+    value=$(yunohost app setting torclient "${var}")
 
-  if [[ "${value}" =~ "An instance is already running" ]]; then
-    echo "${value}" >&2
-    exit 1
-  fi
+    if [[ "${value}" =~ "An instance is already running" ]]; then
+      sleep $(($((RANDOM%5)) + 1))
+    else
+      gotcha=1
+    fi
+  done
 
   echo "${value}"
 }
@@ -98,13 +102,17 @@ moulinette_set() {
 
 moulinette_hotspot_get() {
   var=${1}
+  gotcha=0
 
-  value=$(yunohost app setting hotspot "${var}")
+  while [ "${gotcha}" -eq 0 ]; do
+    value=$(yunohost app setting hotspot "${var}")
 
-  if [[ "${value}" =~ "An instance is already running" ]]; then
-    echo "${value}" >&2
-    exit 1
-  fi
+    if [[ "${value}" =~ "An instance is already running" ]]; then
+      sleep $(($((RANDOM%5)) + 1))
+    else
+      gotcha=1
+    fi
+  done
 
   echo "${value}"
 }
@@ -179,33 +187,46 @@ do_status() {
   exit ${exitcode}
 }
 
-# Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
-if [ ! -e /tmp/.ynh-torclient-boot ]; then
-  touch /tmp/.ynh-torclient-boot
-  systemctl restart php5-fpm --quiet
-fi
+if [ "$1" != restart ]; then
 
-# Variables
+  # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
+  if [ ! -e /tmp/.ynh-torclient-boot ]; then
+    touch /tmp/.ynh-torclient-boot
+    systemctl restart php5-fpm
+  fi
 
-echo -n "Retrieving Yunohost settings... "
+  ynh_wifi_device_id=$(moulinette_get wifi_device_id)
 
-ynh_service_enabled=$(moulinette_get service_enabled)
-ynh_wifi_device_id=$(moulinette_get wifi_device_id)
+  if [[ ! "${1}" =~ stop ]]; then
+    exitcode=0
 
-if [ "${ynh_wifi_device_id}" -eq 0 ]; then
-  ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
-else
-  ynh_wifi_device="hotspot${ynh_wifi_device_id}"
-fi
+    if [ "${ynh_wifi_device_id}" -eq -1 ]; then
+      echo "[WARN] You need to select an associated wifi hotspot (you can do it through the web admin)"
+      exitcode=1
+    fi
 
-echo OK
+    [ "${exitcode}" -ne 0 ] && exit ${exitcode}
+  fi
 
-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]}
+  # Variables
+  
+  echo -n "Retrieving Yunohost settings... "
+  
+  ynh_service_enabled=$(moulinette_get service_enabled)
+  
+  if [ "${ynh_wifi_device_id}" -eq 0 ]; then
+    ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
+  else
+    ynh_wifi_device="hotspot${ynh_wifi_device_id}"
+  fi
 
-#echo "Torclient will be active on $ynh_wifi_device device and $ynh_wifi_ssid SSID with $ynh_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]}
+fi
 
 case "$1" in
   start)

+ 2 - 4
conf/ynh-torclient.service

@@ -1,15 +1,13 @@
 [Unit]
 Description=YunoHost TOR Client.
-Requires=network.target
-After=network.target
+Requires=ynh-hotspot.service
+After=ynh-hotspot.service
 
 [Service]
 Type=oneshot
 ExecStart=/usr/local/bin/ynh-torclient start
-ExecRestart=/usr/local/bin/ynh-torclient restart
 ExecStop=/usr/local/bin/ynh-torclient stop
 RemainAfterExit=yes
 
 [Install]
 WantedBy=multi-user.target
-Alias=ynh-torclient.service

+ 16 - 12
scripts/install

@@ -44,7 +44,7 @@ DEBIAN_FRONTEND=noninteractive sudo apt-get --assume-yes --force-yes install ${p
 
 # Save arguments
 sudo yunohost app setting torclient service_enabled -v 0
-sudo yunohost app setting torclient wifi_device_id -v 0
+sudo yunohost app setting torclient wifi_device_id -v -1
 
 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"
@@ -76,25 +76,29 @@ sudo sed "s|<TPL:NGINX_LOCATION>|${url_path}|g" -i /var/www/torclient/config.php
 
 # Copy init script
 sudo install -o root -g root -m 0755 ../conf/ynh-torclient /usr/local/bin/
-sudo install -o root -g root -m 0755 ../conf/ynh-torclient.service /lib/systemd/system/ynh-torclient.service
+sudo install -o root -g root -m 0644 ../conf/ynh-torclient.service /etc/systemd/system/
 
 # Allow Tor ports in firewall
 sudo yunohost firewall allow --no-upnp UDP 9053
 sudo yunohost firewall allow --no-upnp TCP 9040
 
 # Set default inits
-#php-fpm is already installed by yunohost hotspot app
-sudo systemctl restart php5-fpm --quiet
-sudo systemctl reload nginx --quiet
+# The boot order of these services are important, so they are disabled by default
+# and the ynh-torclient service handles them.
+sudo systemctl disable tor
+sudo systemctl stop tor
 
-# Update SSO for vpnadmin
-sudo yunohost app ssowatconf
+sudo systemctl enable php5-fpm
+sudo systemctl restart php5-fpm
 
-# Set default inits
-sudo systemctl stop tor --quiet &> /dev/null
-sudo systemctl disable tor --quiet
+sudo systemctl reload nginx
+
+sudo systemctl enable ynh-torclient
+sudo systemctl start ynh-torclient
+
+# Update SSO for torclient
+sudo yunohost app ssowatconf
 
-sudo systemctl reenable ynh-torclient --quiet
-sudo systemctl start  ynh-torclient --quiet
+echo "WARNING: Tor Client is not started because you need to define an associated wifi hotspot through the web admin" >&2
 
 exit 0

+ 5 - 7
scripts/remove

@@ -22,10 +22,9 @@
 domain=$(sudo yunohost app setting torclient domain)
 
 # The End
-sudo systemctl stop ynh-torclient --quiet
-sudo systemctl disable ynh-torclient --quiet
-sudo yunohost service remove ynh-torclient
-sudo rm -f /lib/systemd/system/ynh-torclient.service /usr/local/bin/ynh-torclient
+sudo systemctl stop ynh-torclient
+sudo systemctl disable ynh-torclient
+sudo rm -f /etc/systemd/system/ynh-torclient.service /usr/local/bin/ynh-torclient
 sudo rm -f /tmp/.ynh-torclient-*
 
 # Update firewall for DHCP
@@ -38,9 +37,8 @@ sudo rm -f /etc/nginx/conf.d/${domain}.d/torclient.conf
 sudo rm -f /etc/php5/fpm/pool.d/torclient.conf
 
 # Restart services
-sudo yunohost service stop php5-fpm
-sudo yunohost service start php5-fpm
-sudo systemctl reload nginx --quiet
+sudo systemctl restart php5-fpm
+sudo systemctl reload nginx
 
 # Remove sources
 sudo rm -rf /var/www/torclient/

+ 16 - 8
sources/controller.php

@@ -13,23 +13,23 @@ function moulinette_set($var, $value) {
 }
 
 function stop_service() {
-  exec('sudo systemctl stop ynh-torclient --quiet');
+  exec('sudo systemctl stop ynh-torclient');
 }
 
 function start_service() {
-  exec('sudo systemctl start ynh-torclient --quiet', $output, $retcode);
+  exec('sudo systemctl start ynh-torclient', $output, $retcode);
 
   return $retcode;
 }
 
 function service_status() {
-  exec('sudo systemctl is-active ynh-torclient --quiet', $output);
+  exec('sudo ynh-torclient status', $output);
 
   return $output;
 }
 
 function service_faststatus() {
-  exec('sudo systemctl is-active ynh-torclient --quiet', $output, $retcode);
+  exec('sudo systemctl is-active ynh-torclient', $output, $retcode);
 
   return $retcode;
 }
@@ -51,10 +51,6 @@ dispatch('/', function() {
     $wifi_ssid_list .= "<li $active data-device-id='$i'><a href='javascript:;'>".htmlentities($ssids[$i]).'</a></li>';
   }
 
-  if(empty($wifi_ssid)) {
-    $wifi_ssid = '<em>'.T_("None").'</em>';
-  }
-
   set('faststatus', service_faststatus() == 0);
   set('service_enabled', moulinette_get('service_enabled'));
   set('wifi_device_id', $wifi_device_id);
@@ -67,6 +63,18 @@ dispatch('/', function() {
 dispatch_put('/settings', function() {
   $service_enabled = isset($_POST['service_enabled']) ? 1 : 0;
 
+  if($service_enabled == 1) {
+    try {
+      if($_POST['wifi_device_id'] == -1) {
+        throw new Exception(T_('You need to select an associated hotspot'));
+      }
+
+    } catch(Exception $e) {
+      flash('error', $e->getMessage().' ('.T_('configuration not updated').').');
+      goto redirect;
+    }
+  }
+
   stop_service();
 
   moulinette_set('service_enabled', $service_enabled);

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

@@ -36,11 +36,18 @@
           </div>
 
           <div class="form-group">
+            <?php if($wifi_device_id == -1): ?>
+              <div class="alert alert-dismissible alert-warning fade in" style="margin: 2px 16px 17px" 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> <?= T_("You need to select an associated hotspot.") ?>
+              </div>
+            <?php endif; ?>
+
             <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_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>
+                  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><?= empty($wifi_ssid) ? '<em>'.T_("None").'</em>' : $wifi_ssid ?> <span class="caret"></span></button>
                   <ul class="dropdown-menu dropdown-menu-left" id="deviceidlist" role="menu">
                     <?= $wifi_ssid_list ?>
                   </ul>