freedomboneeee/src/freedombone-app-riot

357 lines
12 KiB
Plaintext
Raw Permalink Normal View History

2017-05-10 01:04:09 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2017-05-10 01:04:09 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2017-05-10 01:04:09 +02:00
#
# Riot Web user interface for Matrix
#
# License
# =======
#
2018-01-25 19:35:39 +01:00
# Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
2017-05-10 01:04:09 +02:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim chat'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
NOT_ON_ONION=1
2017-05-10 01:04:09 +02:00
2018-05-23 14:09:37 +02:00
RIOT_VERSION='0.15.3'
2017-07-20 16:35:24 +02:00
RIOT_FILENAME="riot-v${RIOT_VERSION}"
2018-05-23 14:11:18 +02:00
RIOT_HASH='0aecaa1c0d1e387c1730fea33cdb01b1a296e6146b7aef6a819fa90d9efc026e'
2017-05-10 01:04:09 +02:00
RIOT_DOWNLOAD_URL="https://github.com/vector-im/riot-web/releases/download/v${RIOT_VERSION}"
RIOT_ONION_PORT=8115
RIOT_ONION_HOSTNAME=
RIOT_DOMAIN_NAME=
RIOT_CODE=
riot_variables=(MY_USERNAME
RIOT_DOMAIN_NAME
MATRIX_DOMAIN_NAME
SYSTEM_TYPE
2017-06-20 16:53:42 +02:00
ONION_ONLY
2017-05-10 01:04:09 +02:00
DDNS_PROVIDER)
function logging_on_riot {
echo -n ''
}
function logging_off_riot {
echo -n ''
}
2017-05-10 01:04:09 +02:00
function remove_user_riot {
echo -n ''
}
function add_user_riot {
echo '0'
}
2018-01-20 14:13:31 +01:00
function riot_remove_bad_links {
sed -i '/riot.im/d' /var/www/$RIOT_DOMAIN_NAME/htdocs/home.html
}
2017-05-10 01:04:09 +02:00
function install_interactive_riot {
if [[ $ONION_ONLY != "no" ]]; then
RIOT_DOMAIN_NAME='riot.local'
else
RIOT_DETAILS_COMPLETE=
while [ ! $RIOT_DETAILS_COMPLETE ]
do
2018-03-01 00:17:49 +01:00
data=$(mktemp 2>/dev/null)
2018-05-12 18:48:20 +02:00
if [[ "$DDNS_PROVIDER" == *"freedns"* ]]; then
2017-05-10 01:04:09 +02:00
dialog --backtitle $"Freedombone Configuration" \
--title $"Riot Web user interface for Matrix" \
2018-03-01 00:17:49 +01:00
--form $"\\nPlease enter your details.\\n\\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 13 65 3 \
2017-05-10 01:04:09 +02:00
$"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
$"Code:" 2 1 "$(grep 'RIOT_CODE' temp.cfg | awk -F '=' '{print $2}')" 2 15 40 255 \
2018-03-01 00:17:49 +01:00
2> "$data"
2017-05-10 01:04:09 +02:00
else
dialog --backtitle $"Freedombone Configuration" \
--title $"Riot Web user interface for Matrix" \
2018-03-01 00:17:49 +01:00
--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 \
2017-05-10 01:04:09 +02:00
$"Domain:" 1 1 "$(grep 'RIOT_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 15 40 40 \
2018-03-01 00:17:49 +01:00
2> "$data"
2017-05-10 01:04:09 +02:00
fi
sel=$?
case $sel in
2018-03-01 00:17:49 +01:00
1) rm -f "$data"
exit 1;;
255) rm -f "$data"
exit 1;;
2017-05-10 01:04:09 +02:00
esac
2018-03-01 00:17:49 +01:00
RIOT_DOMAIN_NAME=$(sed -n 1p < "$data")
if [ "$RIOT_DOMAIN_NAME" ]; then
2017-05-10 01:04:09 +02:00
TEST_DOMAIN_NAME=$RIOT_DOMAIN_NAME
validate_domain_name
2018-03-01 00:17:49 +01:00
if [[ "$TEST_DOMAIN_NAME" != "$RIOT_DOMAIN_NAME" ]]; then
2017-05-10 01:04:09 +02:00
RIOT_DOMAIN_NAME=
dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
else
2018-05-12 18:48:20 +02:00
if [[ "$DDNS_PROVIDER" == *"freedns"* ]]; then
2018-03-01 00:17:49 +01:00
RIOT_CODE=$(sed -n 2p < "$data")
2017-05-10 01:04:09 +02:00
validate_freedns_code "$RIOT_CODE"
2018-03-01 00:17:49 +01:00
if [ ! "$VALID_CODE" ]; then
2017-05-10 01:04:09 +02:00
RIOT_DOMAIN_NAME=
fi
fi
fi
fi
if [ $RIOT_DOMAIN_NAME ]; then
RIOT_DETAILS_COMPLETE="yes"
fi
2018-03-01 00:17:49 +01:00
rm -f "$data"
2017-05-10 01:04:09 +02:00
done
# save the results in the config file
write_config_param "RIOT_CODE" "$RIOT_CODE"
fi
write_config_param "RIOT_DOMAIN_NAME" "$RIOT_DOMAIN_NAME"
APP_INSTALLED=1
}
function reconfigure_riot {
echo -n ''
}
2017-05-10 12:50:54 +02:00
function riot_download {
# download
2018-03-01 00:17:49 +01:00
if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
wget "${RIOT_DOWNLOAD_URL}/${RIOT_FILENAME}.tar.gz" -O "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz"
2017-05-10 12:50:54 +02:00
fi
2018-03-01 00:17:49 +01:00
if [ ! -f "$INSTALL_DIR/${RIOT_FILENAME}.tar.gz" ]; then
2017-05-10 12:50:54 +02:00
echo $'Unable to download Riot Web from releases'
exit 62836
fi
2018-03-01 00:17:49 +01:00
cd "$INSTALL_DIR" || exit 2468724628
2017-05-10 12:50:54 +02:00
# check the hash
curr_hash=$(sha256sum ${RIOT_FILENAME}.tar.gz | awk -F ' ' '{print $1}')
if [[ "$curr_hash" != "$RIOT_HASH" ]]; then
echo $'Riot download hash does not match'
exit 78352
fi
tar -xzvf ${RIOT_FILENAME}.tar.gz
2018-03-01 00:17:49 +01:00
if [ ! -d "$INSTALL_DIR/${RIOT_FILENAME}" ]; then
2017-05-10 12:50:54 +02:00
echo $'Unable to extract Riot Web tarball'
exit 542826
fi
2018-03-03 12:49:17 +01:00
cp -r "$INSTALL_DIR/${RIOT_FILENAME}/"* "/var/www/$RIOT_DOMAIN_NAME/htdocs"
2017-05-10 15:15:53 +02:00
2018-03-01 00:17:49 +01:00
chown -R www-data:www-data "/var/www/$RIOT_DOMAIN_NAME/htdocs"
2017-05-10 12:50:54 +02:00
}
2017-05-10 01:04:09 +02:00
function upgrade_riot {
2018-03-01 00:17:49 +01:00
if ! grep -q 'riot version:' "$COMPLETION_FILE"; then
2017-05-10 12:50:54 +02:00
return
fi
CURR_RIOT_VERSION=$(get_completion_param "riot version")
echo "riot current version: ${CURR_RIOT_VERSION}"
echo "riot app version: ${RIOT_VERSION}"
if [[ "${CURR_RIOT_VERSION}" == "${RIOT_VERSION}" ]]; then
return
fi
riot_download
2018-03-01 00:17:49 +01:00
sed -i "s|riot version.*|riot version:$RIOT_VERSION|g" "${COMPLETION_FILE}"
2018-01-20 14:13:31 +01:00
riot_remove_bad_links
2017-05-10 12:50:54 +02:00
systemctl restart nginx
2017-05-10 01:04:09 +02:00
}
function backup_local_riot {
echo -n ''
}
function restore_local_riot {
echo -n ''
}
function backup_remote_riot {
echo -n ''
}
function restore_remote_riot {
echo -n ''
}
function remove_riot {
function_check remove_onion_service
remove_onion_service riot ${RIOT_ONION_PORT}
2017-05-10 12:54:49 +02:00
2017-05-10 01:04:09 +02:00
nginx_dissite $RIOT_DOMAIN_NAME
if [ -f /etc/nginx/sites-available/$RIOT_DOMAIN_NAME ]; then
rm /etc/nginx/sites-available/$RIOT_DOMAIN_NAME
fi
2017-05-10 12:54:49 +02:00
if [ -d /var/www/$RIOT_DOMAIN_NAME ]; then
rm -rf /var/www/$RIOT_DOMAIN_NAME
fi
2017-05-10 01:04:09 +02:00
remove_completion_param install_riot
2018-03-01 00:17:49 +01:00
sed -i '/riot /d' "$COMPLETION_FILE"
2017-05-10 01:04:09 +02:00
}
function install_riot {
if [[ $ONION_ONLY != 'no' ]]; then
return
fi
2017-05-10 01:04:09 +02:00
# check that matrix has been installed
2018-03-01 00:17:49 +01:00
if [ ! "$MATRIX_DOMAIN_NAME" ]; then
2017-05-10 01:04:09 +02:00
exit 687292
fi
if [[ "$MATRIX_DOMAIN_NAME" != *'.'* ]]; then
exit 256288
fi
if [ ! -d /var/lib/matrix ]; then
exit 827334
fi
2017-06-20 16:52:40 +02:00
function_check get_completion_param
MATRIX_ONION_DOMAIN_NAME=$(get_completion_param "matrix onion domain")
2017-05-10 01:04:09 +02:00
apt-get -yq install wget
if [ ! -d /var/www/$RIOT_DOMAIN_NAME/htdocs ]; then
mkdir -p /var/www/$RIOT_DOMAIN_NAME/htdocs
fi
2018-03-01 00:17:49 +01:00
if [ ! -d "$INSTALL_DIR" ]; then
mkdir "$INSTALL_DIR"
2017-05-10 01:04:09 +02:00
fi
2017-05-10 12:50:54 +02:00
riot_download
2017-05-10 01:04:09 +02:00
2018-03-01 00:17:49 +01:00
cd "/var/www/$RIOT_DOMAIN_NAME/htdocs" || exit 4628462876
2017-05-10 01:04:09 +02:00
2018-03-01 00:17:49 +01:00
if [[ "$ONION_ONLY" == 'no' ]]; then
2018-01-20 14:36:18 +01:00
riot_config_file="config.${RIOT_DOMAIN_NAME}.json"
cp config.sample.json $riot_config_file
2018-03-01 00:17:49 +01:00
sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"https://${MATRIX_DOMAIN_NAME}\",|g" "$riot_config_file"
sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"https://${MATRIX_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
sed -i "/\"servers\":/a \"${MATRIX_DOMAIN_NAME}\"," "$riot_config_file"
2017-06-20 16:52:40 +02:00
else
2018-01-20 14:36:18 +01:00
riot_config_file="config.${MATRIX_ONION_DOMAIN_NAME}.json"
2018-03-01 00:17:49 +01:00
cp config.sample.json "$riot_config_file"
sed -i "s|\"default_hs_url\":.*|\"default_hs_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
sed -i "s|\"default_is_url\":.*|\"default_is_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}\",|g" "$riot_config_file"
sed -i "s|\"bug_report_endpoint_url\":.*|\"bug_report_endpoint_url\": \"http://${MATRIX_ONION_DOMAIN_NAME}/bugs\",|g" "$riot_config_file"
sed -i "/\"servers\":/a \"${MATRIX_ONION_DOMAIN_NAME}\"," "$riot_config_file"
2017-06-20 16:52:40 +02:00
fi
2018-03-01 00:17:49 +01:00
sed -i "s|\"integrations_ui_url\":.*|\"integrations_ui_url\": \"\",|g" "$riot_config_file"
sed -i "s|\"integrations_rest_url\":.*|\"integrations_rest_url\": \"\",|g" "$riot_config_file"
sed -i 's|https://piwik.riot.im/||g' "$riot_config_file"
2017-05-10 01:04:09 +02:00
RIOT_ONION_HOSTNAME=$(add_onion_service riot 80 ${RIOT_ONION_PORT})
riot_nginx_site=/etc/nginx/sites-available/$RIOT_DOMAIN_NAME
if [[ $ONION_ONLY == "no" ]]; then
function_check nginx_http_redirect
nginx_http_redirect $RIOT_DOMAIN_NAME
2018-03-01 00:17:49 +01:00
{ echo 'server {';
echo ' listen 443 ssl;';
echo ' #listen [::]:443 ssl;';
echo " server_name $RIOT_DOMAIN_NAME;";
echo '';
echo ' # Security'; } >> $riot_nginx_site
2017-05-10 01:04:09 +02:00
function_check nginx_ssl
nginx_ssl $RIOT_DOMAIN_NAME
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options $RIOT_DOMAIN_NAME
2017-05-10 01:04:09 +02:00
2018-03-01 00:17:49 +01:00
{ echo ' add_header Strict-Transport-Security max-age=15768000;';
echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Root';
echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
echo '';
echo ' index index.html;';
echo '';
echo ' location / {'; } >> $riot_nginx_site
2017-05-10 01:04:09 +02:00
function_check nginx_limits
nginx_limits $RIOT_DOMAIN_NAME '15m'
2018-03-01 00:17:49 +01:00
{ echo ' }';
echo '}';
echo ''; } >> $riot_nginx_site
2017-05-10 01:04:09 +02:00
else
echo -n '' > $riot_nginx_site
fi
2018-03-01 00:17:49 +01:00
{ echo 'server {';
echo " listen 127.0.0.1:$RIOT_ONION_PORT default_server;";
echo " server_name $RIOT_ONION_HOSTNAME;";
echo ''; } >> $riot_nginx_site
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options $RIOT_DOMAIN_NAME
2018-03-01 00:17:49 +01:00
{ echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Root';
echo " root /var/www/$RIOT_DOMAIN_NAME/htdocs;";
echo '';
echo ' index index.html;';
echo '';
echo ' location / {'; } >> $riot_nginx_site
2017-05-10 01:04:09 +02:00
function_check nginx_limits
nginx_limits $RIOT_DOMAIN_NAME '15m'
2018-03-01 00:17:49 +01:00
echo ' }' >> "$riot_nginx_site"
echo '}' >> "$riot_nginx_site"
2017-05-10 01:04:09 +02:00
2018-03-01 00:17:49 +01:00
sed '/Content-Security-Policy/d' "$riot_nginx_site"
sed -i 's| DENY;| SAMEORIGIN;|g' "$riot_nginx_site"
sed -i 's|limit_conn conn_limit_per_ip.*|limit_conn conn_limit_per_ip 50;|g' "$riot_nginx_site"
sed -i 's|limit_req zone.*|limit_req zone=req_limit_per_ip burst=20 nodelay;|g' "$riot_nginx_site"
2017-05-10 13:59:31 +02:00
2017-05-10 01:04:09 +02:00
function_check create_site_certificate
2018-03-01 00:17:49 +01:00
if [ ! -f "/etc/ssl/certs/${RIOT_DOMAIN_NAME}.pem" ]; then
create_site_certificate "$RIOT_DOMAIN_NAME" 'yes'
2017-05-10 01:04:09 +02:00
fi
function_check nginx_ensite
nginx_ensite $RIOT_DOMAIN_NAME
function_check add_ddns_domain
add_ddns_domain $RIOT_DOMAIN_NAME
2018-01-20 14:36:18 +01:00
riot_remove_bad_links
2017-05-10 01:04:09 +02:00
chown -R www-data:www-data /var/www/$RIOT_DOMAIN_NAME/htdocs
systemctl restart nginx
set_completion_param "riot domain" "$RIOT_DOMAIN_NAME"
2018-03-01 00:17:49 +01:00
if ! grep -q "riot version:" "${COMPLETION_FILE}"; then
echo "riot version:${RIOT_VERSION}" >> "${COMPLETION_FILE}"
2017-05-10 12:50:54 +02:00
else
2018-03-01 00:17:49 +01:00
sed -i "s|riot version.*|riot version:${RIOT_VERSION}|g" "${COMPLETION_FILE}"
2017-05-10 12:50:54 +02:00
fi
2017-05-10 01:04:09 +02:00
APP_INSTALLED=1
}
# NOTE: deliberately no exit 0