freedombone-app-riot 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Riot Web user interface for Matrix
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim chat'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. RIOT_VERSION='0.9.9'
  34. RIOT_FILENAME="vector-v${RIOT_VERSION}"
  35. RIOT_HASH='209cd3eee841c208dfb8ec1c4558667471b0c4373d87138210205b83f1a7c97b'
  36. RIOT_DOWNLOAD_URL="https://github.com/vector-im/riot-web/releases/download/v${RIOT_VERSION}"
  37. RIOT_ONION_PORT=8115
  38. RIOT_ONION_HOSTNAME=
  39. RIOT_DOMAIN_NAME=
  40. RIOT_CODE=
  41. riot_variables=(MY_USERNAME
  42. RIOT_DOMAIN_NAME
  43. MATRIX_DOMAIN_NAME
  44. SYSTEM_TYPE
  45. DDNS_PROVIDER)
  46. function remove_user_riot {
  47. echo -n ''
  48. }
  49. function add_user_riot {
  50. echo '0'
  51. }
  52. function install_interactive_riot {
  53. if [[ $ONION_ONLY != "no" ]]; then
  54. RIOT_DOMAIN_NAME='riot.local'
  55. else
  56. RIOT_DETAILS_COMPLETE=
  57. while [ ! $RIOT_DETAILS_COMPLETE ]
  58. do
  59. data=$(tempfile 2>/dev/null)
  60. trap "rm -f $data" 0 1 2 5 15
  61. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  62. dialog --backtitle $"Freedombone Configuration" \
  63. --title $"Riot Web user interface for Matrix" \
  64. --form $"\nPlease enter your details.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
  65. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  66. $"Code:" 2 1 "$(grep 'RIOT_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
  67. 2> $data
  68. else
  69. dialog --backtitle $"Freedombone Configuration" \
  70. --title $"Riot Web user interface for Matrix" \
  71. --form $"\nPlease enter your details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 2 \
  72. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  73. 2> $data
  74. fi
  75. sel=$?
  76. case $sel in
  77. 1) exit 1;;
  78. 255) exit 1;;
  79. esac
  80. RIOT_DOMAIN_NAME=$(cat $data | sed -n 1p)
  81. if [ $RIOT_DOMAIN_NAME ]; then
  82. TEST_DOMAIN_NAME=$RIOT_DOMAIN_NAME
  83. validate_domain_name
  84. if [[ $TEST_DOMAIN_NAME != $RIOT_DOMAIN_NAME ]]; then
  85. RIOT_DOMAIN_NAME=
  86. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  87. else
  88. if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
  89. RIOT_CODE=$(cat $data | sed -n 2p)
  90. validate_freedns_code "$RIOT_CODE"
  91. if [ ! $VALID_CODE ]; then
  92. RIOT_DOMAIN_NAME=
  93. fi
  94. fi
  95. fi
  96. fi
  97. if [ $RIOT_DOMAIN_NAME ]; then
  98. RIOT_DETAILS_COMPLETE="yes"
  99. fi
  100. done
  101. # save the results in the config file
  102. write_config_param "RIOT_CODE" "$RIOT_CODE"
  103. fi
  104. write_config_param "RIOT_DOMAIN_NAME" "$RIOT_DOMAIN_NAME"
  105. APP_INSTALLED=1
  106. }
  107. function reconfigure_riot {
  108. echo -n ''
  109. }
  110. function riot_download {
  111. # download
  112. if [ ! -f $INSTALL_DIR/${RIOT_FILENAME}.tar.gz ]; then
  113. wget ${RIOT_DOWNLOAD_URL}/${RIOT_FILENAME}.tar.gz -O $INSTALL_DIR/${RIOT_FILENAME}.tar.gz
  114. fi
  115. if [ ! -f $INSTALL_DIR/${RIOT_FILENAME}.tar.gz ]; then
  116. echo $'Unable to download Riot Web from releases'
  117. exit 62836
  118. fi
  119. cd $INSTALL_DIR
  120. # check the hash
  121. curr_hash=$(sha256sum ${RIOT_FILENAME}.tar.gz | awk -F ' ' '{print $1}')
  122. if [[ "$curr_hash" != "$RIOT_HASH" ]]; then
  123. echo $'Riot download hash does not match'
  124. exit 78352
  125. fi
  126. tar -xzvf ${RIOT_FILENAME}.tar.gz
  127. if [ ! -d $INSTALL_DIR/${RIOT_FILENAME} ]; then
  128. echo $'Unable to extract Riot Web tarball'
  129. exit 542826
  130. fi
  131. cp -r $INSTALL_DIR/${RIOT_FILENAME}/* /var/www/$RIOT_DOMAIN_NAME/htdocs
  132. chown -R www-data:www-data /var/www/$RIOT_DOMAIN_NAME/htdocs
  133. }
  134. function upgrade_riot {
  135. if ! grep -q 'riot version:' $COMPLETION_FILE; then
  136. return
  137. fi
  138. CURR_RIOT_VERSION=$(get_completion_param "riot version")
  139. echo "riot current version: ${CURR_RIOT_VERSION}"
  140. echo "riot app version: ${RIOT_VERSION}"
  141. if [[ "${CURR_RIOT_VERSION}" == "${RIOT_VERSION}" ]]; then
  142. return
  143. fi
  144. riot_download
  145. sed -i "s|riot version.*|riot version:$RIOT_VERSION|g" ${COMPLETION_FILE}
  146. systemctl restart nginx
  147. }
  148. function backup_local_riot {
  149. echo -n ''
  150. }
  151. function restore_local_riot {
  152. echo -n ''
  153. }
  154. function backup_remote_riot {
  155. echo -n ''
  156. }
  157. function restore_remote_riot {
  158. echo -n ''
  159. }
  160. function remove_riot {
  161. function_check remove_onion_service
  162. remove_onion_service riot ${RIOT_ONION_PORT}
  163. nginx_dissite $RIOT_DOMAIN_NAME
  164. if [ -f /etc/nginx/sites-available/$RIOT_DOMAIN_NAME ]; then
  165. rm /etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  166. fi
  167. if [ -d /var/www/$RIOT_DOMAIN_NAME ]; then
  168. rm -rf /var/www/$RIOT_DOMAIN_NAME
  169. fi
  170. remove_completion_param install_riot
  171. sed -i '/riot /d' $COMPLETION_FILE
  172. }
  173. function install_riot {
  174. # check that matrix has been installed
  175. if [ ! $MATRIX_DOMAIN_NAME ]; then
  176. exit 687292
  177. fi
  178. if [[ "$MATRIX_DOMAIN_NAME" != *'.'* ]]; then
  179. exit 256288
  180. fi
  181. if [ ! -d /var/lib/matrix ]; then
  182. exit 827334
  183. fi
  184. apt-get -yq install wget
  185. if [ ! -d /var/www/$RIOT_DOMAIN_NAME/htdocs ]; then
  186. mkdir -p /var/www/$RIOT_DOMAIN_NAME/htdocs
  187. fi
  188. if [ ! -d $INSTALL_DIR ]; then
  189. mkdir $INSTALL_DIR
  190. fi
  191. riot_download
  192. cd /var/www/$RIOT_DOMAIN_NAME/htdocs
  193. cp config.sample.json config.json
  194. sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" config.json
  195. sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" config.json
  196. sed -i "s|\"integrations_ui_url\":.*|\"integrations_ui_url\": \"https://${MATRIX_DOMAIN_NAME}/\",|g" config.json
  197. sed -i "s|\"integrations_rest_url\":.*|\"integrations_rest_url\": \"https://${MATRIX_DOMAIN_NAME}/api\",|g" config.json
  198. sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"https://${MATRIX_DOMAIN_NAME}/bugs\",|g" config.json
  199. sed -i "/\"servers\":/a \"matrix.freedombone.net\"," config.json
  200. RIOT_ONION_HOSTNAME=$(add_onion_service riot 80 ${RIOT_ONION_PORT})
  201. riot_nginx_site=/etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  202. if [[ $ONION_ONLY == "no" ]]; then
  203. function_check nginx_http_redirect
  204. nginx_http_redirect $RIOT_DOMAIN_NAME
  205. echo 'server {' >> $riot_nginx_site
  206. echo ' listen 443 ssl;' >> $riot_nginx_site
  207. echo ' listen [::]:443 ssl;' >> $riot_nginx_site
  208. echo " server_name $RIOT_DOMAIN_NAME;" >> $riot_nginx_site
  209. echo '' >> $riot_nginx_site
  210. echo ' # Security' >> $riot_nginx_site
  211. function_check nginx_ssl
  212. nginx_ssl $RIOT_DOMAIN_NAME
  213. function_check nginx_disable_sniffing
  214. nginx_disable_sniffing $RIOT_DOMAIN_NAME
  215. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $riot_nginx_site
  216. echo '' >> $riot_nginx_site
  217. echo ' # Logs' >> $riot_nginx_site
  218. echo ' access_log /dev/null;' >> $riot_nginx_site
  219. echo ' error_log /dev/null;' >> $riot_nginx_site
  220. echo '' >> $riot_nginx_site
  221. echo ' # Root' >> $riot_nginx_site
  222. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;" >> $riot_nginx_site
  223. echo '' >> $riot_nginx_site
  224. echo ' index index.html;' >> $riot_nginx_site
  225. echo '' >> $riot_nginx_site
  226. echo ' location / {' >> $riot_nginx_site
  227. function_check nginx_limits
  228. nginx_limits $RIOT_DOMAIN_NAME '15m'
  229. echo ' }' >> $riot_nginx_site
  230. echo '' >> $riot_nginx_site
  231. nginx_keybase ${RIOT_DOMAIN_NAME}
  232. echo '}' >> $riot_nginx_site
  233. echo '' >> $riot_nginx_site
  234. else
  235. echo -n '' > $riot_nginx_site
  236. fi
  237. echo 'server {' >> $riot_nginx_site
  238. echo " listen 127.0.0.1:$RIOT_ONION_PORT default_server;" >> $riot_nginx_site
  239. echo " server_name $RIOT_ONION_HOSTNAME;" >> $riot_nginx_site
  240. echo '' >> $riot_nginx_site
  241. function_check nginx_disable_sniffing
  242. nginx_disable_sniffing $RIOT_DOMAIN_NAME
  243. echo '' >> $riot_nginx_site
  244. echo ' # Logs' >> $riot_nginx_site
  245. echo ' access_log /dev/null;' >> $riot_nginx_site
  246. echo ' error_log /dev/null;' >> $riot_nginx_site
  247. echo '' >> $riot_nginx_site
  248. echo ' # Root' >> $riot_nginx_site
  249. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;" >> $riot_nginx_site
  250. echo '' >> $riot_nginx_site
  251. echo ' index index.html;' >> $riot_nginx_site
  252. echo '' >> $riot_nginx_site
  253. echo ' location / {' >> $riot_nginx_site
  254. function_check nginx_limits
  255. nginx_limits $RIOT_DOMAIN_NAME '15m'
  256. echo ' }' >> $riot_nginx_site
  257. echo '' >> $riot_nginx_site
  258. nginx_keybase ${RIOT_DOMAIN_NAME}
  259. echo '}' >> $riot_nginx_site
  260. sed '/Content-Security-Policy/d' $riot_nginx_site
  261. sed -i 's| DENY;| SAMEORIGIN;|g' $riot_nginx_site
  262. function_check create_site_certificate
  263. if [ ! -f /etc/ssl/certs/${RIOT_DOMAIN_NAME}.pem ]; then
  264. create_site_certificate $RIOT_DOMAIN_NAME 'yes'
  265. fi
  266. function_check nginx_ensite
  267. nginx_ensite $RIOT_DOMAIN_NAME
  268. function_check add_ddns_domain
  269. add_ddns_domain $RIOT_DOMAIN_NAME
  270. chown -R www-data:www-data /var/www/$RIOT_DOMAIN_NAME/htdocs
  271. systemctl restart nginx
  272. set_completion_param "riot domain" "$RIOT_DOMAIN_NAME"
  273. if ! grep -q "riot version:" ${COMPLETION_FILE}; then
  274. echo "riot version:${RIOT_VERSION}" >> ${COMPLETION_FILE}
  275. else
  276. sed -i "s|riot version.*|riot version:${RIOT_VERSION}|g" ${COMPLETION_FILE}
  277. fi
  278. APP_INSTALLED=1
  279. }
  280. # NOTE: deliberately no exit 0