Newer
Older
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
# TURN server functions
#
# License
# =======
#
#
# 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
function generate_turn_key {
local turnkey="${1}"
local filepath="${2}"
if [ ! -f "/etc/ssl/certs/${domain}.dhparam" ]; then
echo $'Generating dhparams for turn server'
# shellcheck disable=SC2086
${PROJECT_NAME}-dhparam -h "${domain}" -o "/etc/ssl/certs/${domain}.dhparam"
if [ ! -f "/etc/ssl/certs/${domain}.dhparam" ]; then
echo $"/etc/ssl/certs/${domain}.dhparam not found when configuring turn server"
fi
cp "/etc/ssl/certs/${domain}.dhparam" "/var/lib/turn/${domain}.dhparam"
chown turnserver:turnserver "/var/lib/turn/${domain}.dhparam"
{ echo "tls-listening-port=$TURN_PORT";
echo 'fingerprint';
echo 'lt-cred-mech';
echo 'use-auth-secret';
echo "static-auth-secret=$turnkey";
echo "realm=$domain";
echo 'total-quota=100';
echo 'bps-capacity=0';
echo 'stale-nonce';
echo "cert=/etc/letsencrypt/live/${domain}/fullchain.pem";
echo "pkey=/etc/letsencrypt/live/${domain}/privkey.pem";
echo "dh-file=/var/lib/turn/${domain}.dhparam";
echo 'cipher-list="ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AES:RSA+3DES:!ADH:!AECDH:!MD5"';
echo 'no-loopback-peers';
echo 'no-multicast-peers';
echo 'no-tlsv1';
echo 'no-tlsv1_1';
echo 'no-stdout-log';
echo 'simple-log';
echo 'log-file=/dev/null'; } > "${filepath}"
else
{ echo "tls-listening-port=$TURN_PORT";
echo 'fingerprint';
echo 'lt-cred-mech';
echo 'use-auth-secret';
echo "static-auth-secret=$turnkey";
echo "realm=$domain";
echo 'total-quota=100';
echo 'bps-capacity=0';
echo 'stale-nonce';
echo 'no-loopback-peers';
echo 'no-multicast-peers';
echo 'no-stdout-log';
echo 'simple-log';
echo 'log-file=/dev/null'; } > "${filepath}"
fi
systemctl stop turn
systemctl disable turn
if [ -f /etc/systemd/system/turn.service ]; then
rm /etc/systemd/system/turn.service
fi
sed -i "/# TURN Server/,/# End of TURN Server/d" "/etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}"
curr_default_domain_name=$DEFAULT_DOMAIN_NAME
if [ "$1" ]; then
curr_default_domain_name="$1"
if [[ "$curr_default_domain_name" != "$DEFAULT_DOMAIN_NAME" ]]; then
if [ -f /var/lib/turn/turnserver.conf ]; then
rm /var/lib/turn/turnserver.conf
fi
fi
fi
create_default_web_site "$curr_default_domain_name"
turn_nginx_site=/etc/nginx/sites-available/$curr_default_domain_name
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 ${curr_default_domain_name};";
nginx_ssl "${curr_default_domain_name}"
nginx_security_options "${curr_default_domain_name}"
{ 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"
nginx_limits "${curr_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"
{ echo 'server {';
echo " listen 127.0.0.1:$TURN_ONION_PORT default_server;";
echo " server_name $curr_default_domain_name;";
nginx_security_options "$curr_default_domain_name"
{ echo '';
echo ' # Logs';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$turn_nginx_site"
nginx_limits "$curr_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"
$INSTALL_PACKAGES 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 \
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==9.0.3
# calling upgrade option prevents installation on ARM
pip install lxml
if [ ! -d /var/lib/turn ]; then
mkdir /var/lib/turn
fi
if [ ! -f /var/lib/turn/turnserver.conf ]; then
turnkey="$(create_password 30)"
generate_turn_key "$turnkey" /var/lib/turn/turnserver.conf "$curr_default_domain_name"
else
turnkey=$(grep 'static-auth-secret' /var/lib/turn/turnserver.conf | awk -F '=' '{print $2}')
fi
write_config_param TURN_KEY "$turnkey"
adduser --system --home=/var/lib/turn/ --group turnserver
adduser mailpile debian-tor
adduser mailpile www-data
if [ -d /etc/letsencrypt ]; then
chgrp -R ssl-cert /etc/letsencrypt
chmod -R g=rX /etc/letsencrypt
usermod -a -G ssl-cert turnserver
fi
chmod -R 700 /var/lib/turn/turnserver.conf
chown -R turnserver:turnserver /var/lib/turn
{ echo '[Unit]';
echo 'Description=TURN server';
echo 'After=network.target nginx.target';
echo '';
echo '[Service]';
echo 'Type=simple';
echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf --pidfile /var/lib/turn/turnserver.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
systemctl enable turn
systemctl daemon-reload
systemctl start turn
if [[ "$ONION_ONLY" == 'no' ]]; then
firewall_add turn ${TURN_PORT}
firewall_add turn-http ${TURN_HTTP_PORT}
fi
#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
function change_default_domain_name_turn {
new_default_domain_name="$1"
install_turn "$new_default_domain_name"
}