freedombone-mesh-connect 8.2KB

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