freedombone-mesh-connect 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Blogging functions for mesh clients
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. MY_USERNAME='fbone'
  32. OPENVPN_SERVER_NAME="server"
  33. OPENVPN_KEY_FILENAME='client.ovpn'
  34. VPN_COUNTRY_CODE="US"
  35. VPN_AREA="Apparent Free Speech Zone"
  36. VPN_LOCATION="Freedomville"
  37. VPN_ORGANISATION="Freedombone"
  38. VPN_UNIT="Freedombone Unit"
  39. STUNNEL_PORT=3439
  40. VPN_MESH_TLS_PORT=653
  41. function vpn_generate_keys {
  42. # generate host keys
  43. if [ ! -f /etc/openvpn/dh2048.pem ]; then
  44. ${PROJECT_NAME}-dhparam -o /etc/openvpn/dh2048.pem
  45. fi
  46. if [ ! -f /etc/openvpn/dh2048.pem ]; then
  47. echo $'vpn dhparams were not generated' >> /var/log/${PROJECT_NAME}.log
  48. exit 73724523
  49. fi
  50. cp /etc/openvpn/dh2048.pem /etc/openvpn/easy-rsa/keys/dh2048.pem
  51. cd /etc/openvpn/easy-rsa || exit 246872464
  52. # shellcheck disable=SC1091
  53. . ./vars
  54. ./clean-all
  55. vpn_openssl_version='1.0.0'
  56. if [ ! -f openssl-${vpn_openssl_version}.cnf ]; then
  57. echo $"openssl-${vpn_openssl_version}.cnf was not found" >> /var/log/${PROJECT_NAME}.log
  58. exit 7392353
  59. fi
  60. cp openssl-${vpn_openssl_version}.cnf openssl.cnf
  61. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt ]; then
  62. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt
  63. fi
  64. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key ]; then
  65. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key
  66. fi
  67. if [ -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.csr ]; then
  68. rm /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.csr
  69. fi
  70. sed -i 's| --interact||g' build-key-server
  71. sed -i 's| --interact||g' build-ca
  72. ./build-ca
  73. ./build-key-server ${OPENVPN_SERVER_NAME}
  74. if [ ! -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt ]; then
  75. echo $'OpenVPN crt not found' >> /var/log/${PROJECT_NAME}.log
  76. exit 7823352
  77. fi
  78. server_cert=$(cat /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt)
  79. if [ ${#server_cert} -lt 10 ]; then
  80. cat /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.crt
  81. echo $'Server cert generation failed' >> /var/log/${PROJECT_NAME}.log
  82. exit 3284682
  83. fi
  84. if [ ! -f /etc/openvpn/easy-rsa/keys/${OPENVPN_SERVER_NAME}.key ]; then
  85. echo $'OpenVPN key not found' >> /var/log/${PROJECT_NAME}.log
  86. exit 6839436
  87. fi
  88. if [ ! -f /etc/openvpn/easy-rsa/keys/ca.key ]; then
  89. echo $'OpenVPN ca not found' >> /var/log/${PROJECT_NAME}.log
  90. exit 7935203
  91. fi
  92. cp /etc/openvpn/easy-rsa/keys/{$OPENVPN_SERVER_NAME.crt,$OPENVPN_SERVER_NAME.key,ca.crt} /etc/openvpn
  93. create_user_vpn_key ${MY_USERNAME}
  94. }
  95. function generate_stunnel_keys {
  96. echo "Creating stunnel keys" >> /var/log/${PROJECT_NAME}.log
  97. openssl req -x509 -nodes -days 3650 -sha256 \
  98. -subj "/O=$VPN_ORGANISATION/OU=$VPN_UNIT/C=$VPN_COUNTRY_CODE/ST=$VPN_AREA/L=$VPN_LOCATION/CN=$HOSTNAME" \
  99. -newkey rsa:2048 -keyout /etc/stunnel/key.pem \
  100. -out /etc/stunnel/cert.pem
  101. if [ ! -f /etc/stunnel/key.pem ]; then
  102. echo $'stunnel key not created' >> /var/log/${PROJECT_NAME}.log
  103. exit 793530
  104. fi
  105. if [ ! -f /etc/stunnel/cert.pem ]; then
  106. echo $'stunnel cert not created' >> /var/log/${PROJECT_NAME}.log
  107. exit 204587
  108. fi
  109. chmod 400 /etc/stunnel/key.pem
  110. chmod 640 /etc/stunnel/cert.pem
  111. cat /etc/stunnel/key.pem /etc/stunnel/cert.pem >> /etc/stunnel/stunnel.pem
  112. chmod 640 /etc/stunnel/stunnel.pem
  113. openssl pkcs12 -export -out /etc/stunnel/stunnel.p12 -inkey /etc/stunnel/key.pem -in /etc/stunnel/cert.pem -passout pass:
  114. if [ ! -f /etc/stunnel/stunnel.p12 ]; then
  115. echo $'stunnel pkcs12 not created' >> /var/log/${PROJECT_NAME}.log
  116. exit 639353
  117. fi
  118. chmod 640 /etc/stunnel/stunnel.p12
  119. cp /etc/stunnel/stunnel.pem /home/$MY_USERNAME/stunnel.pem
  120. cp /etc/stunnel/stunnel.p12 /home/$MY_USERNAME/stunnel.p12
  121. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/stunnel*
  122. echo "stunnel keys created" >> /var/log/${PROJECT_NAME}.log
  123. }
  124. function mesh_setup_vpn {
  125. vpn_generate_keys
  126. cp /etc/stunnel/stunnel-client.conf /home/$MY_USERNAME/stunnel-client.conf
  127. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/stunnel*
  128. generate_stunnel_keys
  129. sed -i 's|tun-mtu .*|tun-mtu 1532|g' /home/$MY_USERNAME/client.ovpn
  130. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/client.ovpn
  131. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/stunnel*
  132. # create an archive of the vpn client files
  133. cd /home/$MY_USERNAME || exit 346825628354
  134. tar -czvf vpn.tar.gz stunnel* client.ovpn
  135. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/vpn.tar.gz
  136. if [ -f vpn.tar.gz ]; then
  137. zenity --info --title $"Generate VPN client keys" --text $"\\nNew VPN client keys have been generated in the /home/fbone directory.\\n\\nYou can find it by selecting \"Places\" then \"Home Directory\" on the top menu bar. Transmit the vpn.tar.gz file to whoever is running the other mesh network so that they can connect to yours.\\n\\nThey should uncompress vpn.tar.gz to their /home/fbone directory, forward port $VPN_MESH_TLS_PORT then connect using your IP address or domain name." --width 600
  138. fi
  139. }
  140. function connect_to_mesh {
  141. connect_title=$"Connect to another mesh network"
  142. HIDDEN_SERVICE_PATH=/var/lib/tor/hidden_service_mesh/hostname
  143. if [ -f ${HIDDEN_SERVICE_PATH} ]; then
  144. connect_title=$"Connect from $(cat $HIDDEN_SERVICE_PATH) to another mesh network"
  145. fi
  146. data=$(zenity --entry --title "$connect_title" --text $'Enter the IP address or domain name of the other mesh')
  147. sel=$?
  148. case $sel in
  149. 0)
  150. ip_or_domain="$data"
  151. if [ ${#ip_or_domain} -gt 1 ]; then
  152. if [[ "$ip_or_domain" == *'.'* ]]; then
  153. connect_failed=
  154. if [ ! -f ~/client.ovpn ]; then
  155. connect_failed=1
  156. fi
  157. if [ ! -f ~/stunnel.pem ]; then
  158. connect_failed=1
  159. fi
  160. if [ ! -f ~/stunnel.p12 ]; then
  161. connect_failed=1
  162. fi
  163. if [ $connect_failed ]; then
  164. zenity --info --title $"Connect to another mesh network" --text $"\nObtain the vpn.tar.gz file from the other mesh administrator, uncompress it into the /home/fbone directory and also forwarded port $VPN_MESH_TLS_PORT from your internet router to this system." --width 400
  165. exit 1
  166. fi
  167. sed -i "s|route .*|route $ip_or_domain 255.255.255.255 net_gateway|g" ~/client.ovpn
  168. clear
  169. cd ~/ || exit 234628422874
  170. sudo stunnel stunnel-client.conf
  171. sudo openvpn client.ovpn
  172. fi
  173. fi
  174. ;;
  175. esac
  176. }
  177. data=$(zenity --list 1 $"Connect to another mesh network" 2 $"Generate VPN keys for another mesh network to connect to me" --column="id" --title $"Connect to another mesh network" --column=$"Choose an operation:" --hide-column=1 --print-column=1 --width=500 --height=100)
  178. sel=$?
  179. case $sel in
  180. 1) exit 1;;
  181. 255) exit 1;;
  182. esac
  183. case $data in
  184. 1) connect_to_mesh;;
  185. 2) mesh_setup_vpn;;
  186. esac
  187. exit 0