freedombone/src/freedombone-utils-turn

182 lines
7.2 KiB
Plaintext
Raw Normal View History

2016-12-30 16:52:46 +01:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# TURN server functions
#
# License
# =======
#
# Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
#
# 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/>.
TURN_PORT=3478
2017-01-01 21:40:08 +01:00
TURN_HTTP_PORT=3407
TURN_ONION_PORT=8110
2016-12-30 16:52:46 +01:00
function generate_turn_key {
local turnkey="${1}"
local filepath="${2}"
echo "lt-cred-mech" > "${filepath}"
echo "use-auth-secret" >> "${filepath}"
echo "static-auth-secret=${turnkey}" >> "${filepath}"
echo "realm=turn.${DEFAULT_DOMAIN_NAME}" >> "${filepath}"
2016-12-31 00:16:21 +01:00
if [[ $ONION_ONLY == 'no' ]]; then
echo "cert=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.crt" >> "${filepath}"
echo "pkey=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.key" >> "${filepath}"
fi
2016-12-30 16:52:46 +01:00
}
function remove_turn {
2017-01-01 21:40:08 +01:00
firewall_remove ${TURN_HTTP_PORT}
2016-12-30 16:52:46 +01:00
systemctl stop turn
systemctl disable turn
if [ -f /etc/systemd/system/turn.service ]; then
rm /etc/systemd/system/turn.service
fi
apt-get -y remove coturn
rm -rf /var/lib/turn
2017-01-01 22:24:22 +01:00
sed -i "/# TURN Server/,/# End of TURN Server/d" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
2017-01-01 21:40:08 +01:00
remove_onion_service turn ${TURN_ONION_PORT}
systemctl restart nginx
2016-12-30 16:52:46 +01:00
}
function install_turn {
2017-01-01 21:40:08 +01:00
create_default_web_site
# append the matrix server to the web site config
turn_nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
2017-01-01 22:38:41 +01:00
if ! grep -q "# End of TURN Server" $turn_nginx_site; then
2017-01-01 22:05:07 +01:00
if [[ $ONION_ONLY == "no" ]]; then
echo '# TURN Server' >> $turn_nginx_site
echo 'server {' >> $turn_nginx_site
echo " listen ${TURN_HTTP_PORT} ssl;" >> $turn_nginx_site
echo " listen [::]:${TURN_HTTP_PORT} ssl;" >> $turn_nginx_site
echo " server_name ${DEFAULT_DOMAIN_NAME};" >> $turn_nginx_site
echo '' >> $turn_nginx_site
echo ' # Security' >> $turn_nginx_site
function_check nginx_ssl
nginx_ssl ${DEFAULT_DOMAIN_NAME}
function_check nginx_disable_sniffing
nginx_disable_sniffing ${DEFAULT_DOMAIN_NAME}
echo ' add_header Strict-Transport-Security max-age=15768000;' >> $turn_nginx_site
echo '' >> $turn_nginx_site
echo ' # Logs' >> $turn_nginx_site
echo ' access_log /dev/null;' >> $turn_nginx_site
echo ' error_log /dev/null;' >> $turn_nginx_site
echo '' >> $turn_nginx_site
echo ' # Index' >> $turn_nginx_site
echo ' index index.html;' >> $turn_nginx_site
echo '' >> $turn_nginx_site
echo ' # Location' >> $turn_nginx_site
echo ' location / {' >> $turn_nginx_site
function_check nginx_limits
nginx_limits ${DEFAULT_DOMAIN_NAME} '15m'
echo " proxy_pass http://localhost:${TURN_PORT};" >> $turn_nginx_site
echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $turn_nginx_site
echo ' }' >> $turn_nginx_site
echo '}' >> $turn_nginx_site
echo '' >> $turn_nginx_site
else
echo '# TURN Server' >> $turn_nginx_site
fi
2017-01-01 21:40:08 +01:00
echo 'server {' >> $turn_nginx_site
2017-01-01 22:05:07 +01:00
echo " listen 127.0.0.1:$TURN_ONION_PORT default_server;" >> $turn_nginx_site
echo " server_name $DEFAULT_DOMAIN_NAME;" >> $turn_nginx_site
2017-01-01 21:40:08 +01:00
echo '' >> $turn_nginx_site
function_check nginx_disable_sniffing
2017-01-01 22:05:07 +01:00
nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
2017-01-01 21:40:08 +01:00
echo '' >> $turn_nginx_site
echo ' # Logs' >> $turn_nginx_site
echo ' access_log /dev/null;' >> $turn_nginx_site
echo ' error_log /dev/null;' >> $turn_nginx_site
echo '' >> $turn_nginx_site
echo ' # Location' >> $turn_nginx_site
echo ' location / {' >> $turn_nginx_site
function_check nginx_limits
2017-01-01 22:05:07 +01:00
nginx_limits $DEFAULT_DOMAIN_NAME '15m'
2017-01-01 21:40:08 +01:00
echo " proxy_pass http://localhost:${TURN_PORT};" >> $turn_nginx_site
echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $turn_nginx_site
echo ' }' >> $turn_nginx_site
echo '}' >> $turn_nginx_site
2017-01-01 22:05:07 +01:00
echo '# End of TURN Server' >> $turn_nginx_site
2017-01-01 21:40:08 +01:00
fi
2016-12-30 16:52:46 +01:00
export DEBIAN_FRONTEND=noninteractive
apt-get -yq install coreutils coturn \
curl file gcc git libevent-2.0-5 \
libevent-dev libffi-dev libffi6 \
libgnutls28-dev libjpeg62-turbo \
libjpeg62-turbo-dev libldap-2.4-2 \
libldap2-dev libsasl2-dev \
libsqlite3-dev libssl-dev \
libssl1.0.0 libtool libxml2 \
libxml2-dev libxslt1-dev libxslt1.1 \
make python python-dev \
python-pip python-psycopg2 \
python-virtualenv sqlite unzip \
zlib1g zlib1g-dev
pip install --upgrade pip
pip install --upgrade python-ldap
pip install --upgrade lxml
if [ ! -d /var/lib/turn ]; then
mkdir /var/lib/turn
fi
turnkey="$(create_password 30)"
generate_turn_key $turnkey /var/lib/turn/turnserver.conf
2016-12-31 00:16:21 +01:00
chmod -R 700 /var/lib/turn/turnserver.conf
2016-12-30 16:52:46 +01:00
chown -R matrix:matrix /var/lib/turn
2016-12-30 17:46:09 +01:00
echo '[Unit]' > /etc/systemd/system/turn.service
2016-12-30 17:14:41 +01:00
echo 'Description=TURN server' >> /etc/systemd/system/turn.service
echo 'After=network.target nginx.target' >> /etc/systemd/system/turn.service
2016-12-30 16:52:46 +01:00
echo '' >> /etc/systemd/system/turn.service
echo '[Service]' >> /etc/systemd/system/turn.service
echo 'Type=simple' >> /etc/systemd/system/turn.service
echo 'User=matrix' >> /etc/systemd/system/turn.service
echo "WorkingDirectory=/var/lib/turn" >> /etc/systemd/system/turn.service
2016-12-30 23:34:22 +01:00
echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf --pidfile /var/lib/matrix/homeserver.pid" >> /etc/systemd/system/turn.service
2016-12-30 17:48:17 +01:00
echo "Environment=REPORT_STATS=\"no\"" >> /etc/systemd/system/turn.service
2016-12-30 16:52:46 +01:00
echo 'Restart=always' >> /etc/systemd/system/turn.service
echo 'RestartSec=10' >> /etc/systemd/system/turn.service
echo '' >> /etc/systemd/system/turn.service
echo '[Install]' >> /etc/systemd/system/turn.service
echo 'WantedBy=multi-user.target' >> /etc/systemd/system/turn.service
systemctl enable turn
systemctl daemon-reload
systemctl start turn
2017-01-01 21:40:08 +01:00
firewall_add turn ${TURN_HTTP_PORT}
TURN_ONION_HOSTNAME=$(add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT})
systemctl restart nginx
2016-12-30 16:52:46 +01:00
}
# NOTE: deliberately no exit 0