| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# TURN server functions
#
# License
# =======
#
# Copyright (C) 2016-2018 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
TURN_HTTP_PORT=3407
TURN_ONION_PORT=8110
function generate_turn_key {
    local turnkey="${1}"
    local filepath="${2}"
    { echo "lt-cred-mech";
      echo "use-auth-secret";
      echo "static-auth-secret=${turnkey}";
      echo "realm=turn.${DEFAULT_DOMAIN_NAME}"; } > "${filepath}"
    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
}
function remove_turn {
    firewall_remove ${TURN_HTTP_PORT}
    firewall_remove ${TURN_PORT}
    systemctl stop turn
    systemctl disable turn
    if [ -f /etc/systemd/system/turn.service ]; then
        rm /etc/systemd/system/turn.service
    fi
    systemctl daemon-reload
    apt-get -y remove coturn
    rm -rf /var/lib/turn
    sed -i "/# TURN Server/,/# End of TURN Server/d" "/etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}"
    remove_onion_service turn ${TURN_ONION_PORT}
    rm /etc/avahi/services/turn.service
    systemctl restart nginx
}
function install_turn {
    create_default_web_site
    # append the matrix server to the web site config
    turn_nginx_site=/etc/nginx/sites-available/$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 ${DEFAULT_DOMAIN_NAME};";
              echo '';
              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;';
              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"
            function_check nginx_limits
            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"
        else
            echo '# TURN Server' >> "$turn_nginx_site"
        fi
        { echo 'server {';
          echo "    listen 127.0.0.1:$TURN_ONION_PORT default_server;";
          echo "    server_name $DEFAULT_DOMAIN_NAME;";
          echo ''; } >> "$turn_nginx_site"
        function_check nginx_disable_sniffing
        nginx_disable_sniffing "$DEFAULT_DOMAIN_NAME"
        { echo '';
          echo '  # Logs';
          echo '  access_log /dev/null;';
          echo '  error_log /dev/null;';
          echo '';
          echo '  # Location';
          echo '  location / {'; } >> "$turn_nginx_site"
        function_check nginx_limits
        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"
    fi
    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.1 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
    chmod -R 700 /var/lib/turn/turnserver.conf
    chown -R matrix:matrix /var/lib/turn
    { 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
    systemctl enable turn
    systemctl daemon-reload
    systemctl start turn
    firewall_add turn ${TURN_PORT}
    firewall_add turn-http ${TURN_HTTP_PORT}
    #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
    systemctl restart avahi-daemon
    systemctl restart nginx
}
# NOTE: deliberately no exit 0
 |