init_ynh-torclient 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-torclient
  4. # Required-Start: $network $remote_fs $syslog $all
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Set prerequisites for wifi torclient.
  9. # Description: Set prerequisites for wifi torclient.
  10. ### END INIT INFO
  11. # Functions
  12. ## State functions
  13. has_vpnclient_app() {
  14. [ -e /tmp/.ynh-vpnclient-started ]
  15. }
  16. has_torclient_app() {
  17. [ -e /tmp/.ynh-torclient-started ]
  18. }
  19. has_hotspot_app() {
  20. [ -e /tmp/.ynh-hotspot-started ]
  21. }
  22. is_nat_set() {
  23. iptables -nvt nat -L PREROUTING | grep REDIRECT | grep -q "${ynh_wifi_device}"
  24. }
  25. is_tor_running() {
  26. service tor status &> /dev/null
  27. }
  28. is_running() {
  29. has_hotspot_app && is_tor_running && is_nat_set
  30. }
  31. set_nat() {
  32. iptables -t nat -F
  33. iptables -t nat -X
  34. iptables -t nat -A PREROUTING -i ${ynh_wifi_device} -p udp --dport 53 -j REDIRECT --to-ports 9053
  35. iptables -t nat -A PREROUTING -i ${ynh_wifi_device} -p tcp --syn -j REDIRECT --to-ports 9040
  36. }
  37. set_forwarding() {
  38. sysctl -w net.ipv6.conf.all.forwarding=1 > /dev/null
  39. sysctl -w net.ipv4.conf.all.forwarding=1 > /dev/null
  40. }
  41. unset_nat() {
  42. internet_device=${1}
  43. iptables -t nat -D PREROUTING -i ${ynh_wifi_device} -p udp --dport 53 -j REDIRECT --to-ports 9053
  44. iptables -t nat -D PREROUTING -i ${ynh_wifi_device} -p tcp --syn -j REDIRECT --to-ports 9040
  45. }
  46. stop_tor() {
  47. service tor stop
  48. }
  49. start_tor() {
  50. service tor start
  51. }
  52. ## Tools
  53. moulinette_get() {
  54. var=${1}
  55. value=$(yunohost app setting hotspot "${var}")
  56. if [[ "${value}" =~ "An instance is already running" ]]; then
  57. echo "${value}" >&2
  58. exit 1
  59. fi
  60. echo "${value}"
  61. }
  62. moulinette_vpnclient_get() {
  63. var=${1}
  64. value=$(yunohost app setting vpnclient "${var}")
  65. if [[ "${value}" =~ "An instance is already running" ]]; then
  66. echo "${value}" >&2
  67. exit 1
  68. fi
  69. echo "${value}"
  70. }
  71. moulinette_set() {
  72. var=${1}
  73. value=${2}
  74. msg=$(yunohost app setting hotspot "${var}" -v "${value}")
  75. if [ ! $? -eq 0 ]; then
  76. echo "${msg}" >&2
  77. exit 1
  78. fi
  79. }
  80. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  81. if [ ! -e /tmp/.ynh-hotspot-boot ]; then
  82. touch /tmp/.ynh-hotspot-boot
  83. service php5-fpm restart
  84. fi
  85. # Variables
  86. echo -n "Retrieving Yunohost settings... "
  87. ynh_wifi_device=$(moulinette_get wifi_device)
  88. echo "OK"
  89. # Script
  90. case "$1" in
  91. start)
  92. if is_running; then
  93. echo "Already started"
  94. else
  95. echo "[torclient] Starting..."
  96. touch /tmp/.ynh-tor-started
  97. # Run tor
  98. if ! is_tor_running; then
  99. echo "Run Tor"
  100. start_tor
  101. fi
  102. # Set ipv4 NAT
  103. if ! is_nat_set; then
  104. echo "Set NAT settings"
  105. set_nat
  106. fi
  107. fi
  108. ;;
  109. stop)
  110. if ! is_running; then
  111. echo "Already stoped"
  112. else
  113. echo "[torclient] Stopping..."
  114. rm /tmp/.ynh-tor-started
  115. if is_nat_set; then
  116. echo "Unset NAT"
  117. unset_nat
  118. fi
  119. if is_tor_running; then
  120. echo "Stop Tor"
  121. stop_tor
  122. fi
  123. if has_vpnclient_app; then
  124. service ynh-vpnclient start
  125. fi
  126. fi
  127. ;;
  128. status)
  129. exitcode=0
  130. if is_tor_running; then
  131. echo "[OK] Tor is running"
  132. else
  133. echo "[ERR] Tor is not running"
  134. exitcode=1
  135. fi
  136. if is_nat_set; then
  137. echo "[OK] IPv4 nat rules set"
  138. else
  139. echo "[ERR] No IPv4 nat rules not set"
  140. exitcode=1
  141. fi
  142. exit ${exitcode}
  143. ;;
  144. *)
  145. echo "Usage: $0 {start|stop|status}"
  146. exit 1
  147. ;;
  148. esac
  149. exit 0