freedombone-app-riot 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Riot Web user interface for Matrix
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  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. VARIANTS='full full-vim chat'
  29. IN_DEFAULT_INSTALL=0
  30. SHOW_ON_ABOUT=1
  31. NOT_ON_ONION=1
  32. RIOT_VERSION='0.15.3'
  33. RIOT_FILENAME="riot-v${RIOT_VERSION}"
  34. RIOT_HASH='0aecaa1c0d1e387c1730fea33cdb01b1a296e6146b7aef6a819fa90d9efc026e'
  35. RIOT_DOWNLOAD_URL="https://github.com/vector-im/riot-web/releases/download/v${RIOT_VERSION}"
  36. RIOT_ONION_PORT=8115
  37. RIOT_ONION_HOSTNAME=
  38. RIOT_DOMAIN_NAME=
  39. RIOT_CODE=
  40. riot_variables=(MY_USERNAME
  41. RIOT_DOMAIN_NAME
  42. MATRIX_DOMAIN_NAME
  43. SYSTEM_TYPE
  44. ONION_ONLY
  45. DDNS_PROVIDER)
  46. function logging_on_riot {
  47. echo -n ''
  48. }
  49. function logging_off_riot {
  50. echo -n ''
  51. }
  52. function remove_user_riot {
  53. echo -n ''
  54. }
  55. function add_user_riot {
  56. echo '0'
  57. }
  58. function riot_remove_bad_links {
  59. sed -i '/riot.im/d' /var/www/$RIOT_DOMAIN_NAME/htdocs/home.html
  60. }
  61. function install_interactive_riot {
  62. if [[ $ONION_ONLY != "no" ]]; then
  63. RIOT_DOMAIN_NAME='riot.local'
  64. else
  65. RIOT_DETAILS_COMPLETE=
  66. while [ ! $RIOT_DETAILS_COMPLETE ]
  67. do
  68. data=$(mktemp 2>/dev/null)
  69. if [[ "$DDNS_PROVIDER" == *"freedns"* ]]; then
  70. dialog --backtitle $"Freedombone Configuration" \
  71. --title $"Riot Web user interface for Matrix" \
  72. --form $"\\nPlease enter your details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
  73. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  74. $"Code:" 2 1 "$(grep 'RIOT_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
  75. 2> "$data"
  76. else
  77. dialog --backtitle $"Freedombone Configuration" \
  78. --title $"Riot Web user interface for Matrix" \
  79. --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 \
  80. $"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
  81. 2> "$data"
  82. fi
  83. sel=$?
  84. case $sel in
  85. 1) rm -f "$data"
  86. exit 1;;
  87. 255) rm -f "$data"
  88. exit 1;;
  89. esac
  90. RIOT_DOMAIN_NAME=$(sed -n 1p < "$data")
  91. if [ "$RIOT_DOMAIN_NAME" ]; then
  92. TEST_DOMAIN_NAME=$RIOT_DOMAIN_NAME
  93. validate_domain_name
  94. if [[ "$TEST_DOMAIN_NAME" != "$RIOT_DOMAIN_NAME" ]]; then
  95. RIOT_DOMAIN_NAME=
  96. dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
  97. else
  98. if [[ "$DDNS_PROVIDER" == *"freedns"* ]]; then
  99. RIOT_CODE=$(sed -n 2p < "$data")
  100. validate_freedns_code "$RIOT_CODE"
  101. if [ ! "$VALID_CODE" ]; then
  102. RIOT_DOMAIN_NAME=
  103. fi
  104. fi
  105. fi
  106. fi
  107. if [ $RIOT_DOMAIN_NAME ]; then
  108. RIOT_DETAILS_COMPLETE="yes"
  109. fi
  110. rm -f "$data"
  111. done
  112. # save the results in the config file
  113. write_config_param "RIOT_CODE" "$RIOT_CODE"
  114. fi
  115. write_config_param "RIOT_DOMAIN_NAME" "$RIOT_DOMAIN_NAME"
  116. APP_INSTALLED=1
  117. }
  118. function reconfigure_riot {
  119. echo -n ''
  120. }
  121. function riot_download {
  122. # download
  123. if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
  124. wget "${RIOT_DOWNLOAD_URL}/${RIOT_FILENAME}.tar.gz" -O "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz"
  125. fi
  126. if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
  127. echo $'Unable to download Riot Web from releases'
  128. exit 62836
  129. fi
  130. cd "$INSTALL_DIR" || exit 2468724628
  131. # check the hash
  132. curr_hash=$(sha256sum ${RIOT_FILENAME}.tar.gz | awk -F ' ' '{print $1}')
  133. if [[ "$curr_hash" != "$RIOT_HASH" ]]; then
  134. echo $'Riot download hash does not match'
  135. exit 78352
  136. fi
  137. tar -xzvf ${RIOT_FILENAME}.tar.gz
  138. if [ ! -d "$INSTALL_DIR/${RIOT_FILENAME}" ]; then
  139. echo $'Unable to extract Riot Web tarball'
  140. exit 542826
  141. fi
  142. cp -r "$INSTALL_DIR/${RIOT_FILENAME}/"* "/var/www/$RIOT_DOMAIN_NAME/htdocs"
  143. chown -R www-data:www-data "/var/www/$RIOT_DOMAIN_NAME/htdocs"
  144. }
  145. function upgrade_riot {
  146. if ! grep -q 'riot version:' "$COMPLETION_FILE"; then
  147. return
  148. fi
  149. CURR_RIOT_VERSION=$(get_completion_param "riot version")
  150. echo "riot current version: ${CURR_RIOT_VERSION}"
  151. echo "riot app version: ${RIOT_VERSION}"
  152. if [[ "${CURR_RIOT_VERSION}" == "${RIOT_VERSION}" ]]; then
  153. return
  154. fi
  155. riot_download
  156. sed -i "s|riot version.*|riot version:$RIOT_VERSION|g" "${COMPLETION_FILE}"
  157. riot_remove_bad_links
  158. systemctl restart nginx
  159. }
  160. function backup_local_riot {
  161. echo -n ''
  162. }
  163. function restore_local_riot {
  164. echo -n ''
  165. }
  166. function backup_remote_riot {
  167. echo -n ''
  168. }
  169. function restore_remote_riot {
  170. echo -n ''
  171. }
  172. function remove_riot {
  173. function_check remove_onion_service
  174. remove_onion_service riot ${RIOT_ONION_PORT}
  175. nginx_dissite $RIOT_DOMAIN_NAME
  176. if [ -f /etc/nginx/sites-available/$RIOT_DOMAIN_NAME ]; then
  177. rm /etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  178. fi
  179. if [ -d /var/www/$RIOT_DOMAIN_NAME ]; then
  180. rm -rf /var/www/$RIOT_DOMAIN_NAME
  181. fi
  182. remove_completion_param install_riot
  183. sed -i '/riot /d' "$COMPLETION_FILE"
  184. }
  185. function install_riot {
  186. if [[ $ONION_ONLY != 'no' ]]; then
  187. return
  188. fi
  189. # check that matrix has been installed
  190. if [ ! "$MATRIX_DOMAIN_NAME" ]; then
  191. exit 687292
  192. fi
  193. if [[ "$MATRIX_DOMAIN_NAME" != *'.'* ]]; then
  194. exit 256288
  195. fi
  196. if [ ! -d /var/lib/matrix ]; then
  197. exit 827334
  198. fi
  199. function_check get_completion_param
  200. MATRIX_ONION_DOMAIN_NAME=$(get_completion_param "matrix onion domain")
  201. apt-get -yq install wget
  202. if [ ! -d /var/www/$RIOT_DOMAIN_NAME/htdocs ]; then
  203. mkdir -p /var/www/$RIOT_DOMAIN_NAME/htdocs
  204. fi
  205. if [ ! -d "$INSTALL_DIR" ]; then
  206. mkdir "$INSTALL_DIR"
  207. fi
  208. riot_download
  209. cd "/var/www/$RIOT_DOMAIN_NAME/htdocs" || exit 4628462876
  210. if [[ "$ONION_ONLY" == 'no' ]]; then
  211. riot_config_file="config.${RIOT_DOMAIN_NAME}.json"
  212. cp config.sample.json $riot_config_file
  213. sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
  214. sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
  215. sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"https://${MATRIX_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
  216. sed -i "/\"servers\":/a \"${MATRIX_DOMAIN_NAME}\"," "$riot_config_file"
  217. else
  218. riot_config_file="config.${MATRIX_ONION_DOMAIN_NAME}.json"
  219. cp config.sample.json "$riot_config_file"
  220. sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
  221. sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
  222. sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
  223. sed -i "/\"servers\":/a \"${MATRIX_ONION_DOMAIN_NAME}\"," "$riot_config_file"
  224. fi
  225. sed -i "s|\"integrations_ui_url\":.*|\"integrations_ui_url\": \"\",|g" "$riot_config_file"
  226. sed -i "s|\"integrations_rest_url\":.*|\"integrations_rest_url\": \"\",|g" "$riot_config_file"
  227. sed -i 's|https://piwik.riot.im/||g' "$riot_config_file"
  228. RIOT_ONION_HOSTNAME=$(add_onion_service riot 80 ${RIOT_ONION_PORT})
  229. riot_nginx_site=/etc/nginx/sites-available/$RIOT_DOMAIN_NAME
  230. if [[ $ONION_ONLY == "no" ]]; then
  231. function_check nginx_http_redirect
  232. nginx_http_redirect $RIOT_DOMAIN_NAME
  233. { echo 'server {';
  234. echo ' listen 443 ssl;';
  235. echo ' #listen [::]:443 ssl;';
  236. echo " server_name $RIOT_DOMAIN_NAME;";
  237. echo '';
  238. echo ' # Security'; } >> $riot_nginx_site
  239. function_check nginx_ssl
  240. nginx_ssl $RIOT_DOMAIN_NAME
  241. function_check nginx_security_options
  242. nginx_security_options $RIOT_DOMAIN_NAME
  243. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  244. echo '';
  245. echo ' # Logs';
  246. echo ' access_log /dev/null;';
  247. echo ' error_log /dev/null;';
  248. echo '';
  249. echo ' # Root';
  250. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
  251. echo '';
  252. echo ' index index.html;';
  253. echo '';
  254. echo ' location / {'; } >> $riot_nginx_site
  255. function_check nginx_limits
  256. nginx_limits $RIOT_DOMAIN_NAME '15m'
  257. { echo ' }';
  258. echo '}';
  259. echo ''; } >> $riot_nginx_site
  260. else
  261. echo -n '' > $riot_nginx_site
  262. fi
  263. { echo 'server {';
  264. echo " listen 127.0.0.1:$RIOT_ONION_PORT default_server;";
  265. echo " server_name $RIOT_ONION_HOSTNAME;";
  266. echo ''; } >> $riot_nginx_site
  267. function_check nginx_security_options
  268. nginx_security_options $RIOT_DOMAIN_NAME
  269. { echo '';
  270. echo ' # Logs';
  271. echo ' access_log /dev/null;';
  272. echo ' error_log /dev/null;';
  273. echo '';
  274. echo ' # Root';
  275. echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
  276. echo '';
  277. echo ' index index.html;';
  278. echo '';
  279. echo ' location / {'; } >> $riot_nginx_site
  280. function_check nginx_limits
  281. nginx_limits $RIOT_DOMAIN_NAME '15m'
  282. echo ' }' >> "$riot_nginx_site"
  283. echo '}' >> "$riot_nginx_site"
  284. sed '/Content-Security-Policy/d' "$riot_nginx_site"
  285. sed -i 's| DENY;| SAMEORIGIN;|g' "$riot_nginx_site"
  286. sed -i 's|limit_conn conn_limit_per_ip.*|limit_conn conn_limit_per_ip 50;|g' "$riot_nginx_site"
  287. sed -i 's|limit_req zone.*|limit_req zone=req_limit_per_ip burst=20 nodelay;|g' "$riot_nginx_site"
  288. function_check create_site_certificate
  289. if [ ! -f "/etc/ssl/certs/${RIOT_DOMAIN_NAME}.pem" ]; then
  290. create_site_certificate "$RIOT_DOMAIN_NAME" 'yes'
  291. fi
  292. function_check nginx_ensite
  293. nginx_ensite $RIOT_DOMAIN_NAME
  294. function_check add_ddns_domain
  295. add_ddns_domain $RIOT_DOMAIN_NAME
  296. riot_remove_bad_links
  297. chown -R www-data:www-data /var/www/$RIOT_DOMAIN_NAME/htdocs
  298. systemctl restart nginx
  299. set_completion_param "riot domain" "$RIOT_DOMAIN_NAME"
  300. if ! grep -q "riot version:" "${COMPLETION_FILE}"; then
  301. echo "riot version:${RIOT_VERSION}" >> "${COMPLETION_FILE}"
  302. else
  303. sed -i "s|riot version.*|riot version:${RIOT_VERSION}|g" "${COMPLETION_FILE}"
  304. fi
  305. APP_INSTALLED=1
  306. }
  307. # NOTE: deliberately no exit 0