123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: ynh-torclient
- # Required-Start: $network $remote_fs $syslog $all
- # Required-Stop: $network $remote_fs $syslog
- # 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
- # 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 "${ynh_wifi_device}"
- }
- is_tor_running() {
- service tor status &> /dev/null
- }
- is_running() {
- has_hotspot_app && is_tor_running && is_nat_set
- }
- set_nat() {
- iptables -t nat -F
- iptables -t nat -X
- 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 --syn -j REDIRECT --to-ports 9040
- }
- set_forwarding() {
- sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
- sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
- }
- unset_nat() {
- internet_device=${1}
- 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 --syn -j REDIRECT --to-ports 9040
- }
- stop_tor() {
- service tor stop
- }
- start_tor() {
- service tor start
- }
- ## Tools
- 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_vpnclient_get() {
- var=${1}
- value=$(yunohost app setting vpnclient "${var}")
- if [[ "${value}" =~ "An instance is already running" ]]; then
- echo "${value}" >&2
- exit 1
- fi
- echo "${value}"
- }
- moulinette_set() {
- var=${1}
- value=${2}
- msg=$(yunohost app setting hotspot "${var}" -v "${value}")
- if [ ! $? -eq 0 ]; then
- echo "${msg}" >&2
- exit 1
- fi
- }
- # 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
- service php5-fpm restart
- fi
- # Variables
- echo -n "Retrieving Yunohost settings... "
- ynh_wifi_device=$(moulinette_get wifi_device)
- echo "OK"
- # Script
- case "$1" in
- start)
- if is_running; then
- echo "Already started"
- else
- echo "[torclient] Starting..."
- touch /tmp/.ynh-tor-started
- # Run tor
- if ! is_tor_running; then
- echo "Run Tor"
- start_tor
- fi
- # Set ipv4 NAT
- if ! is_nat_set; then
- echo "Set NAT settings"
- set_nat
- fi
- fi
- ;;
- 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
-
- if is_tor_running; then
- echo "Stop Tor"
- stop_tor
- fi
-
- if has_vpnclient_app; then
- service ynh-vpnclient start
- fi
- fi
- ;;
- status)
- exitcode=0
- if is_tor_running; then
- echo "[OK] Tor is running"
- else
- echo "[ERR] Tor is not running"
- exitcode=1
- fi
- if is_nat_set; then
- echo "[OK] IPv4 nat rules set"
- else
- echo "[ERR] No IPv4 nat rules not set"
- exitcode=1
- fi
- exit ${exitcode}
- ;;
- *)
- echo "Usage: $0 {start|stop|status}"
- exit 1
- ;;
- esac
- exit 0
|