freedombone/src/freedombone-utils-turn

201 lines
7.0 KiB
Plaintext
Raw Permalink Normal View History

2016-12-30 16:52:46 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-12-30 16:52:46 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-12-30 16:52:46 +01:00
#
# TURN server functions
#
# License
# =======
#
2018-02-21 20:32:13 +01:00
# Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
2016-12-30 16:52:46 +01: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/>.
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}"
2018-03-02 23:20:49 +01:00
{ echo "lt-cred-mech";
echo "use-auth-secret";
echo "static-auth-secret=${turnkey}";
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}
2018-01-30 11:32:30 +01:00
firewall_remove ${TURN_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
2017-06-10 22:37:50 +02:00
systemctl daemon-reload
2016-12-30 16:52:46 +01:00
apt-get -y remove coturn
rm -rf /var/lib/turn
2018-03-02 23:20:49 +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}
2018-01-30 11:32:30 +01:00
rm /etc/avahi/services/turn.service
2017-01-01 21:40:08 +01:00
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
2018-03-02 23:20:49 +01:00
if ! grep -q '# End of TURN Server' "$turn_nginx_site"; then
if [[ "$ONION_ONLY" == "no" ]]; then
{ echo '# TURN Server';
echo 'server {';
echo " listen ${TURN_HTTP_PORT} ssl;";
echo " listen [::]:${TURN_HTTP_PORT} ssl;";
echo " server_name ${DEFAULT_DOMAIN_NAME};";
echo '';
echo ' # Security'; } >> "$turn_nginx_site"
2017-01-01 22:05:07 +01:00
function_check nginx_ssl
2018-03-02 23:20:49 +01:00
nginx_ssl "${DEFAULT_DOMAIN_NAME}"
2017-01-01 22:05:07 +01:00
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "${DEFAULT_DOMAIN_NAME}"
2018-03-02 23:20: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 ' # Index';
echo ' index index.html;';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$turn_nginx_site"
2017-01-01 22:05:07 +01:00
function_check nginx_limits
2018-03-02 23:20:49 +01:00
nginx_limits "${DEFAULT_DOMAIN_NAME}" '15m'
{ echo " proxy_pass http://localhost:${TURN_PORT};";
echo " proxy_set_header X-Forwarded-For \$remote_addr;";
echo ' }';
echo '}';
echo ''; } >> "$turn_nginx_site"
2017-01-01 22:05:07 +01:00
else
2018-03-02 23:20:49 +01:00
echo '# TURN Server' >> "$turn_nginx_site"
2017-01-01 22:05:07 +01:00
fi
2018-03-02 23:20:49 +01:00
{ echo 'server {';
echo " listen 127.0.0.1:$TURN_ONION_PORT default_server;";
echo " server_name $DEFAULT_DOMAIN_NAME;";
echo ''; } >> "$turn_nginx_site"
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "$DEFAULT_DOMAIN_NAME"
2018-03-02 23:20:49 +01:00
{ echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$turn_nginx_site"
2017-01-01 21:40:08 +01:00
function_check nginx_limits
2018-03-02 23:20:49 +01:00
nginx_limits "$DEFAULT_DOMAIN_NAME" '15m'
{ echo " proxy_pass http://localhost:${TURN_PORT};";
echo " proxy_set_header X-Forwarded-For \$remote_addr;";
echo ' }';
echo '}';
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 \
2017-06-20 14:40:08 +02:00
libssl1.1 libtool libxml2 \
2016-12-30 16:52:46 +01:00
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)"
2018-03-02 23:20:49 +01:00
generate_turn_key "$turnkey" /var/lib/turn/turnserver.conf
2016-12-30 16:52:46 +01:00
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
2018-03-02 23:20:49 +01:00
{ echo '[Unit]';
echo 'Description=TURN server';
echo 'After=network.target nginx.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo 'User=matrix';
echo "WorkingDirectory=/var/lib/turn";
echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf --pidfile /var/lib/matrix/homeserver.pid";
echo "Environment=REPORT_STATS=\"no\"";
echo 'Restart=always';
echo 'RestartSec=10';
echo '';
echo '[Install]';
echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/turn.service
2016-12-30 16:52:46 +01:00
systemctl enable turn
systemctl daemon-reload
systemctl start turn
2018-01-30 11:32:30 +01:00
firewall_add turn ${TURN_PORT}
firewall_add turn-http ${TURN_HTTP_PORT}
2017-01-01 21:40:08 +01:00
2018-03-02 23:20:49 +01:00
#TURN_ONION_HOSTNAME=$(add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT})
add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT}
{ echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
echo '<service-group>';
echo ' <name replace-wildcards="yes">%h TURN</name>';
echo ' <service>';
echo ' <type>_turn._tcp</type>';
echo " <port>$TURN_PORT</port>";
echo ' </service>';
echo ' <service>';
echo ' <type>_turn._udp</type>';
echo " <port>$TURN_PORT</port>";
echo ' </service>';
echo '</service-group>'; } > /etc/avahi/services/turn.service
2018-01-30 11:32:30 +01:00
systemctl restart avahi-daemon
2017-01-01 21:40:08 +01:00
systemctl restart nginx
2016-12-30 16:52:46 +01:00
}
# NOTE: deliberately no exit 0