init_ynh-piratebox 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-piratebox
  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 piratebox.
  9. # Description: Set prerequisites for wifi piratebox.
  10. ### END INIT INFO
  11. # PirateBox app for YunoHost
  12. # Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
  13. # Contribute at https://github.com/jvaubourg/piratebox_ynh
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # Functions
  28. ## State functions
  29. has_hotspot_app() {
  30. [ -e /tmp/.ynh-hotspot-started ]
  31. }
  32. is_nat4_dns_set() {
  33. iptables -nvt nat -L PREROUTING | grep 'udp dpt:53' | grep -q "${ynh_wifi_device}" \
  34. && iptables -nvt nat -L POSTROUTING | grep MASQUERADE | grep -q "${ynh_wifi_device}"
  35. }
  36. is_nat4_web_set() {
  37. iptables -nvt nat -L PREROUTING | grep 'tcp dpt:80' | grep -q "${ynh_wifi_device}"
  38. }
  39. is_fakedns_running() {
  40. ps aux | grep -v grep | grep -q piratebox_fakedns
  41. }
  42. is_running() {
  43. has_hotspot_app \
  44. && is_nat4_dns_set && is_nat4_web_set \
  45. && is_fakedns_running
  46. }
  47. ## Setters
  48. # The IPv6 NAT features are not available available before the kernel 3.8
  49. # therefore the IPv6-compliance will be add when Debian Stable has a
  50. # compliant kernel
  51. set_nat4_dns() {
  52. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  53. iptables -t nat -A POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  54. }
  55. set_nat4_web() {
  56. iptables -t nat -A PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  57. }
  58. start_fakedns() {
  59. /usr/local/bin/piratebox_fakedns "${ynh_ip4_nat_prefix}.1" 2>&1 > /dev/null &
  60. }
  61. ## Unsetters
  62. unset_nat4_dns() {
  63. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p udp --dport 53 -j DNAT --to-destination "${ynh_ip4_nat_prefix}.1:4253"
  64. iptables -t nat -D POSTROUTING -o "${ynh_wifi_device}" -j MASQUERADE
  65. }
  66. unset_nat4_web() {
  67. iptables -t nat -D PREROUTING -i "${ynh_wifi_device}" -p tcp --dport 80 -j REDIRECT --to-port 4280
  68. }
  69. stop_fakedns() {
  70. kill $(ps aux | grep piratebox_fakedns | awk '{ print $2 }' | head -n1)
  71. }
  72. ## Tools
  73. moulinette_hotspot_get() {
  74. var=${1}
  75. value=$(yunohost app setting hotspot "${var}")
  76. if [[ "${value}" =~ "An instance is already running" ]]; then
  77. echo "${value}" >&2
  78. exit 1
  79. fi
  80. echo "${value}"
  81. }
  82. if [ "$1" != restart ]; then
  83. # Restart php5-fpm at the first start (it needs to be restarted after the slapd start)
  84. if [ ! -e /tmp/.ynh-piratebox-boot ]; then
  85. touch /tmp/.ynh-piratebox-boot
  86. service php5-fpm restart
  87. fi
  88. # Variables
  89. echo -n "Retrieving Yunohost settings... "
  90. ynh_wifi_device=$(moulinette_hotspot_get wifi_device)
  91. ynh_ip4_nat_prefix=$(moulinette_hotspot_get ip4_nat_prefix)
  92. echo "OK"
  93. fi
  94. # Script
  95. case "$1" in
  96. start)
  97. if is_running; then
  98. echo "Already started"
  99. else
  100. echo "[piratebox] Starting..."
  101. touch /tmp/.ynh-piratebox-started
  102. # Set IPv4 DNS NAT
  103. if ! is_nat4_dns_set; then
  104. echo "Set IPv4 DNS NAT"
  105. set_nat4_dns
  106. fi
  107. # Set IPv4 Web NAT
  108. if ! is_nat4_web_set; then
  109. echo "Set IPv4 Web NAT"
  110. set_nat4_web
  111. fi
  112. # Run fakedns
  113. if ! is_fakedns_running; then
  114. echo "Run fakedns"
  115. start_fakedns
  116. fi
  117. fi
  118. ;;
  119. stop)
  120. echo "[piratebox] Stopping..."
  121. rm -f /tmp/.ynh-piratebox-started
  122. if is_nat4_dns_set; then
  123. echo "Unset IPv4 DNS NAT"
  124. unset_nat4_dns
  125. fi
  126. if is_nat4_web_set; then
  127. echo "Unset IPv4 Web NAT"
  128. unset_nat4_web
  129. fi
  130. if is_fakedns_running; then
  131. echo "Stop fakedns"
  132. stop_fakedns
  133. fi
  134. ;;
  135. restart)
  136. $0 stop
  137. $0 start
  138. ;;
  139. status)
  140. exitcode=0
  141. if ! has_hotspot_app; then
  142. echo "[ERR] Hotspot is not running"
  143. exitcode=1
  144. fi
  145. if is_nat4_dns_set; then
  146. echo "[OK] IPv4 DNS NAT set"
  147. else
  148. echo "[ERR] No IPv4 DNS NAT set"
  149. exitcode=1
  150. fi
  151. if is_nat4_web_set; then
  152. echo "[OK] IPv4 Web NAT set"
  153. else
  154. echo "[ERR] No IPv4 Web NAT set"
  155. exitcode=1
  156. fi
  157. if is_fakedns_running; then
  158. echo "[OK] Fakedns is running"
  159. else
  160. echo "[ERR] Fakedns is not running"
  161. exitcode=1
  162. fi
  163. exit ${exitcode}
  164. ;;
  165. *)
  166. echo "Usage: $0 {start|stop|restart|status}"
  167. exit 1
  168. ;;
  169. esac
  170. exit 0