Parcourir la source

Merge pull request #7 from labriqueinternet/jessie

Debian jessie support
Julien Vaubourg il y a 10 ans
Parent
commit
286a2e8c2f
7 fichiers modifiés avec 151 ajouts et 84 suppressions
  1. 10 3
      README.md
  2. 72 43
      conf/init_ynh-torclient
  3. 13 0
      conf/ynh-torclient.service
  4. 22 18
      scripts/install
  5. 10 11
      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)

+ 72 - 43
conf/init_ynh-torclient

@@ -1,13 +1,21 @@
 #!/bin/bash
-### BEGIN INIT INFO
-# Provides: ynh-torclient
-# Required-Start:    $network $remote_fs $syslog ynh-hotspot
-# Required-Stop:     $network $remote_fs $syslog ynh-hotspot
-# Default-Start:     2 3 4 5
-# Default-Stop:      0 1 6
-# Short-Description: Set prerequisites for wifi torclient.
-# Description:       Set prerequisites for wifi torclient.
-### END INIT INFO
+
+# TOR Client app for YunoHost
+# Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
+# Contribute at https://github.com/jvaubourg/torclient_ynh
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Functions
 ## State functions
@@ -25,7 +33,7 @@ is_nat_set() {
 }
 
 is_tor_running() {
-  service tor status &> /dev/null
+  systemctl is-active tor &> /dev/null
 }
 
 is_running() {
@@ -50,7 +58,7 @@ unset_nat() {
 }
 
 stop_tor() {
-  service tor stop &> /dev/null
+  systemctl stop tor
 }
 
 start_tor() {
@@ -58,20 +66,24 @@ start_tor() {
 
   sed "s|<TPL:TOR_NETWORK>|${ynh_wifi_prefix}|g" -i /etc/tor/torrc
 
-  service tor start
+  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}"
 }
@@ -90,16 +102,20 @@ 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}"
-} 
+}
 
 do_start() {
   if is_running; then
@@ -171,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
-  service php5-fpm restart
-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)

+ 13 - 0
conf/ynh-torclient.service

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

+ 22 - 18
scripts/install

@@ -1,20 +1,20 @@
 #!/bin/bash
 
-# Tor Client app for YunoHost 
+# Tor Client app for YunoHost
 # Copyright (C) 2015 Emile Morel <emile@bleuchtang.fr>
 # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
 # Contribute at https://github.com/bleuchtang/torclient_ynh
-# 
+#
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -29,7 +29,7 @@ if [ ! $? -eq 0 ]; then
 fi
 
 if [ ! -e /tmp/.ynh-hotspot-started ]; then
-  echo "hotspot not started and/or not installed" 
+  echo "hotspot not started and/or not installed"
 fi
 
 echo 'deb http://deb.torproject.org/torproject.org wheezy main' | sudo tee "/etc/apt/sources.list.d/torproject.list"
@@ -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"
@@ -75,26 +75,30 @@ sudo sed 's|^;\?\s*max_execution_time.\+|max_execution_time = 600|' -i /etc/php5
 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/init_ynh-torclient /etc/init.d/ynh-torclient
+sudo install -o root -g root -m 0755 ../conf/ynh-torclient /usr/local/bin/
+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 service php5-fpm stop
-sudo service php5-fpm start
-sudo service nginx reload
+# 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 service tor stop &> /dev/null
-sudo insserv -r tor
+sudo systemctl reload nginx
+
+sudo systemctl enable ynh-torclient
+sudo systemctl start ynh-torclient
+
+# Update SSO for torclient
+sudo yunohost app ssowatconf
 
-sudo insserv ynh-torclient
-sudo service ynh-torclient start
+echo "WARNING: Tor Client is not started because you need to define an associated wifi hotspot through the web admin" >&2
 
 exit 0

+ 10 - 11
scripts/remove

@@ -1,20 +1,20 @@
 #!/bin/bash
 
-# Tor Client app for YunoHost 
+# Tor Client app for YunoHost
 # Copyright (C) 2015 Emile Morel <emile@bleuchtang.fr>
 # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
 # Contribute at https://github.com/bleuchtang/torclient_ynh
-# 
+#
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU Affero General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
@@ -22,9 +22,9 @@
 domain=$(sudo yunohost app setting torclient domain)
 
 # The End
-sudo service ynh-torclient stop
-sudo yunohost service remove ynh-torclient
-sudo rm -f /etc/init.d/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
@@ -37,14 +37,13 @@ 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 service nginx reload
+sudo systemctl restart php5-fpm
+sudo systemctl reload nginx
 
 # Remove sources
 sudo rm -rf /var/www/torclient/
 
-sudo rm -rf /etc/apt/sources.list.d/torproject.list 
+sudo rm -rf /etc/apt/sources.list.d/torproject.list
 sudo apt-get update
 
 exit 0

+ 16 - 8
sources/controller.php

@@ -13,23 +13,23 @@ function moulinette_set($var, $value) {
 }
 
 function stop_service() {
-  exec('sudo service ynh-torclient stop');
+  exec('sudo systemctl stop ynh-torclient');
 }
 
 function start_service() {
-  exec('sudo service ynh-torclient start', $output, $retcode);
+  exec('sudo systemctl start ynh-torclient', $output, $retcode);
 
   return $retcode;
 }
 
 function service_status() {
-  exec('sudo service ynh-torclient status', $output);
+  exec('sudo ynh-torclient status', $output);
 
   return $output;
 }
 
 function service_faststatus() {
-  exec('sudo service ynh-torclient status', $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>