init_ynh-vpnclient 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #!/bin/bash
  2. ### BEGIN INIT INFO
  3. # Provides: ynh-vpnclient
  4. # Required-Start: $network $remote_fs $syslog
  5. # Required-Stop: $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start VPN client.
  9. # Description: Start VPN client.
  10. ### END INIT INFO
  11. # Functions
  12. ## State functions
  13. has_nativeip6() {
  14. ip -6 route | grep -q default\ via
  15. }
  16. has_hotspot_app() {
  17. yunohost app list -f hotspot --json | grep -q '"installed": true'
  18. }
  19. is_ip6addr_set() {
  20. ip address show dev tun0 2> /dev/null | grep -q "${ynh_ip6_addr}/128"
  21. }
  22. is_serverip6route_set() {
  23. server_ip6=${1}
  24. ip -6 route | grep -q "${server_ip6}/"
  25. }
  26. is_openvpn_running() {
  27. service openvpn status client &> /dev/null
  28. }
  29. is_running() {
  30. ((has_nativeip6 && is_serverip6route_set "${new_server_ip6}") || ! has_nativeip6)\
  31. && ((! has_hotspot_app && is_ip6addr_set) || has_hotspot_app)\
  32. && is_openvpn_running
  33. }
  34. ## Setters
  35. set_ip6addr() {
  36. ip address add "${ynh_ip6_addr}/128" dev tun0
  37. }
  38. set_serverip6route() {
  39. server_ip6=${1}
  40. ip6_gw=${2}
  41. wired_device=${3}
  42. ip route add "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  43. }
  44. start_openvpn() {
  45. ip6_gw=${1}
  46. server_ip6=${2}
  47. if [ ! -z "${ip6_gw}" -a ! -z "${server_ip6}" ]; then
  48. proto=udp6
  49. [ "${ynh_server_proto}" == tcp ] && proto=tcp6-client
  50. else
  51. proto=udp
  52. [ "${ynh_server_proto}" == tcp ] && proto=tcp-client
  53. fi
  54. cp /etc/openvpn/client.conf{.tpl,}
  55. sed "s|<TPL:SERVER_NAME>|${ynh_server_name}|g" -i /etc/openvpn/client.conf
  56. sed "s|<TPL:SERVER_PORT>|${ynh_server_port}|g" -i /etc/openvpn/client.conf
  57. sed "s|<TPL:PROTO>|${proto}|g" -i /etc/openvpn/client.conf
  58. if [[ "${proto}" =~ udp ]]; then
  59. sed 's|^<TPL:UDP_COMMENT>||' -i /etc/openvpn/client.conf
  60. else
  61. sed 's|^<TPL:UDP_COMMENT>|;|' -i /etc/openvpn/client.conf
  62. fi
  63. service openvpn start client
  64. }
  65. ## Unsetters
  66. unset_ip6addr() {
  67. ip address delete "${ynh_ip6_addr}/128" dev tun0
  68. }
  69. unset_serverip6route() {
  70. server_ip6=${1}
  71. ip6_gw=${2}
  72. wired_device=${3}
  73. ip route delete "${server_ip6}/128" via "${ip6_gw}" dev "${wired_device}"
  74. }
  75. stop_openvpn() {
  76. service openvpn stop
  77. }
  78. ## Tools
  79. moulinette_get() {
  80. var=${1}
  81. value=$(yunohost app setting vpnclient "${var}")
  82. if [[ "${value}" =~ "An instance is already running" ]]; then
  83. echo "${value}" >&2
  84. exit 1
  85. fi
  86. echo "${value}"
  87. }
  88. moulinette_set() {
  89. var=${1}
  90. value=${2}
  91. msg=$(yunohost app setting vpnclient "${var}" -v "${value}")
  92. if [ ! $? -eq 0 ]; then
  93. echo "${msg}" >&2
  94. exit 1
  95. fi
  96. }
  97. # Variables
  98. echo -n "Retrieving Yunohost settings... "
  99. ynh_server_name=$(moulinette_get server_name)
  100. ynh_server_port=$(moulinette_get server_port)
  101. ynh_server_proto=$(moulinette_get server_proto)
  102. ynh_ip6_addr=$(moulinette_get ip6_addr)
  103. old_ip6_gw=$(moulinette_get ip6_gw)
  104. old_wired_device=$(moulinette_get wired_device)
  105. old_server_ip6=$(moulinette_get server_ip6)
  106. new_ip6_gw=$(ip -6 route | grep default\ via | awk '{ print $3 }')
  107. new_wired_device=$(ip route | awk '/default via/ { print $NF; }')
  108. new_server_ip6=$(host "${ynh_server_name}" | awk '/IPv6/ { print $NF; }')
  109. if [ -z "${new_server_ip6}" ]; then
  110. new_server_ip6=$(host "${ynh_server_name}" 80.67.188.188 | awk '/IPv6/ { print $NF; }')
  111. fi
  112. echo "OK"
  113. # Script
  114. case "${1}" in
  115. start)
  116. if is_running; then
  117. echo "Already started"
  118. else
  119. echo "Starting..."
  120. # Run openvpn
  121. if ! is_openvpn_running; then
  122. echo "Run openvpn"
  123. start_openvpn "${new_ip6_gw}" "${new_server_ip6}"
  124. if [ ! $? -eq 0 ]; then
  125. exit 1
  126. fi
  127. i=0
  128. false || while [ $? -ne 0 ]; do
  129. (( i++ ))
  130. [ $i -gt 15 ] && exit 1
  131. sleep 1
  132. ip link show dev tun0 &> /dev/null
  133. done && sleep 2
  134. fi
  135. # Check old state of the server ipv6 route
  136. if [ ! -z "${old_server_ip6}" -a ! -z "${new_ip6_gw}" -a ! -z "${old_wired_device}"\
  137. -a \( "${new_server_ip6}" != "${old_server_ip6}" -o "${new_ip6_gw}" != "${old_ip6_gw}"\
  138. -o "${new_wired_device}" != "${old_wired_device}" \) ]\
  139. && is_serverip6route_set "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"; then
  140. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  141. fi
  142. # Set the new server ipv6 route
  143. if has_nativeip6 && ! is_serverip6route_set "${new_server_ip6}"; then
  144. echo "Set IPv6 server route"
  145. set_serverip6route "${new_server_ip6}" "${new_ip6_gw}" "${new_wired_device}"
  146. fi
  147. # Set the ipv6 address
  148. if ! has_hotspot_app && ! is_ip6addr_set; then
  149. echo "Set IPv6 address"
  150. set_ip6addr
  151. fi
  152. fi
  153. # Update dynamic settings
  154. moulinette_set server_ip6 "${new_server_ip6}"
  155. moulinette_set ip6_gw "${new_ip6_gw}"
  156. moulinette_set wired_device "${new_wired_device}"
  157. ;;
  158. stop)
  159. echo "Stopping..."
  160. if ! has_hotspot_app && is_ip6addr_set; then
  161. echo "Unset IPv6 address"
  162. unset_ip6addr
  163. fi
  164. if is_serverip6route_set "${old_server_ip6}"; then
  165. echo "Unset IPv6 server route"
  166. unset_serverip6route "${old_server_ip6}" "${old_ip6_gw}" "${old_wired_device}"
  167. fi
  168. if is_openvpn_running; then
  169. echo "Stop openvpn"
  170. stop_openvpn
  171. fi
  172. ;;
  173. status)
  174. exitcode=0
  175. if ! has_hotspot_app; then
  176. if is_ip6addr_set; then
  177. echo "IPv6 address is correctly set"
  178. else
  179. echo "IPv6 address is NOT set"
  180. exitcode=1
  181. fi
  182. else
  183. echo "Hotspot app detected"
  184. fi
  185. if has_nativeip6; then
  186. if is_serverip6route_set "${new_server_ip6}"; then
  187. echo "IPv6 server route is correctly set"
  188. else
  189. echo "IPv6 server route is NOT set"
  190. exitcode=1
  191. fi
  192. else
  193. echo "No native IPv6 detected"
  194. fi
  195. if is_openvpn_running; then
  196. echo "Openvpn is running"
  197. else
  198. echo "Openvpn is NOT running"
  199. exitcode=1
  200. fi
  201. exit ${exitcode}
  202. ;;
  203. *)
  204. echo "Usage: $0 {start|stop|status}"
  205. exit 1
  206. ;;
  207. esac
  208. exit 0