ynh-torclient 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/bin/bash
  2. # Tor Client app for YunoHost
  3. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  4. # Contribute at https://github.com/labriqueinternet/torclient_ynh
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. # Functions
  19. ## State functions
  20. has_torclient_app() {
  21. [ -e /tmp/.ynh-torclient-started ]
  22. }
  23. has_hotspot_app() {
  24. [ -e /tmp/.ynh-hotspot-started ]
  25. }
  26. is_nat_set() {
  27. iptables -nvt nat -L PREROUTING | grep REDIRECT | grep -q "${ynh_wifi_device}"
  28. }
  29. is_tor_running() {
  30. systemctl is-active tor &> /dev/null
  31. }
  32. is_running() {
  33. has_hotspot_app && is_tor_running && is_nat_set
  34. }
  35. set_nat() {
  36. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j REDIRECT --to-ports 9053
  37. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
  38. }
  39. set_forwarding() {
  40. sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
  41. sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
  42. }
  43. unset_nat() {
  44. internet_device=${1}
  45. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j REDIRECT --to-ports 9053
  46. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp ! --dport 53 --syn -j REDIRECT --to-ports 9040
  47. }
  48. stop_tor() {
  49. systemctl stop tor
  50. }
  51. start_tor() {
  52. cp /etc/tor/torrc{.tpl,}
  53. sed "s|<TPL:TOR_NETWORK>|${ynh_wifi_prefix}|g" -i /etc/tor/torrc
  54. systemctl start tor
  55. }
  56. ## Tools
  57. ynh_setting_get() {
  58. app=${1}
  59. setting=${2}
  60. grep "^${setting}:" "/etc/yunohost/apps/${app}/settings.yml" | sed s/^[^:]\\+:\\s*[\"\']\\?// | sed s/\\s*[\"\']\$//
  61. }
  62. ynh_setting_set() {
  63. app=${1}
  64. setting=${2}
  65. value=${3}
  66. yunohost app setting "${app}" "${setting}" -v "${value}"
  67. }
  68. do_start() {
  69. if is_running; then
  70. echo "Already started"
  71. elif [ "${ynh_service_enabled}" -eq 0 ]; then
  72. echo "Disabled service"
  73. elif ! has_hotspot_app; then
  74. echo "[ERR] Hotspot is not running"
  75. else
  76. echo "[torclient] Starting..."
  77. touch /tmp/.ynh-torclient-started
  78. # Run tor
  79. if ! is_tor_running; then
  80. echo "Run Tor"
  81. start_tor
  82. fi
  83. # Set ipv4 NAT
  84. if ! is_nat_set; then
  85. echo "Set NAT settings"
  86. set_nat
  87. fi
  88. fi
  89. }
  90. do_stop() {
  91. echo "[torclient] Stopping..."
  92. rm -f /tmp/.ynh-torclient-started
  93. if is_nat_set; then
  94. echo "Unset NAT"
  95. unset_nat
  96. fi
  97. if is_tor_running; then
  98. echo "Stop Tor"
  99. stop_tor
  100. fi
  101. }
  102. do_status() {
  103. exitcode=0
  104. if [ "${ynh_service_enabled}" -eq 0 ]; then
  105. echo "[ERR] Tor Client Service disabled"
  106. exitcode=1
  107. fi
  108. if ! has_hotspot_app; then
  109. echo "[ERR] Hotspot is not running"
  110. exitcode=1
  111. fi
  112. if is_tor_running; then
  113. echo "[OK] Tor is running"
  114. else
  115. echo "[ERR] Tor is not running"
  116. exitcode=1
  117. fi
  118. if is_nat_set; then
  119. echo "[OK] IPv4 nat rules set"
  120. else
  121. echo "[ERR] No IPv4 nat rules set"
  122. exitcode=1
  123. fi
  124. exit ${exitcode}
  125. }
  126. if [ "$1" != restart ]; then
  127. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  128. if [ ! -e /tmp/.ynh-torclient-boot ]; then
  129. touch /tmp/.ynh-torclient-boot
  130. systemctl restart php5-fpm
  131. fi
  132. ynh_wifi_device_id=$(ynh_setting_get torclient wifi_device_id)
  133. if [[ ! "${1}" =~ stop ]]; then
  134. exitcode=0
  135. if [ "${ynh_wifi_device_id}" -eq -1 ]; then
  136. echo "[WARN] You need to select an associated wifi hotspot (you can do it through the web admin)"
  137. exitcode=1
  138. fi
  139. [ "${exitcode}" -ne 0 ] && exit ${exitcode}
  140. fi
  141. # Variables
  142. echo -n "Retrieving Yunohost settings... "
  143. ynh_service_enabled=$(ynh_setting_get torclient service_enabled)
  144. if [ "${ynh_wifi_device_id}" -eq 0 ]; then
  145. ynh_wifi_device=$(ynh_setting_get hotspot wifi_device)
  146. else
  147. ynh_wifi_device="hotspot${ynh_wifi_device_id}"
  148. fi
  149. echo OK
  150. IFS='|' read -a ynh_wifi_ssid <<< "$(ynh_setting_get hotspot wifi_ssid)"
  151. IFS='|' read -a ynh_wifi_prefix <<< "$(ynh_setting_get hotspot ip4_nat_prefix)"
  152. ynh_wifi_prefix=${ynh_wifi_prefix[$ynh_wifi_device_id]}
  153. ynh_wifi_ssid=${ynh_wifi_ssid[$ynh_wifi_device_id]}
  154. fi
  155. case "$1" in
  156. start)
  157. do_start
  158. ;;
  159. stop)
  160. do_stop
  161. ;;
  162. restart)
  163. do_stop
  164. do_start
  165. ;;
  166. status)
  167. do_status
  168. ;;
  169. *)
  170. echo "Usage: $0 {start|stop|restart|status}"
  171. exit 1
  172. ;;
  173. esac
  174. exit 0