freedombone-utils-turn 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # TURN server functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2016-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. TURN_PORT=3478
  29. TURN_HTTP_PORT=3407
  30. TURN_ONION_PORT=8110
  31. function generate_turn_key {
  32. local turnkey="${1}"
  33. local filepath="${2}"
  34. { echo "lt-cred-mech";
  35. echo "use-auth-secret";
  36. echo "static-auth-secret=${turnkey}";
  37. echo "realm=turn.${DEFAULT_DOMAIN_NAME}"; } > "${filepath}"
  38. if [[ $ONION_ONLY == 'no' ]]; then
  39. echo "cert=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.crt" >> "${filepath}"
  40. echo "pkey=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.key" >> "${filepath}"
  41. fi
  42. }
  43. function remove_turn {
  44. firewall_remove ${TURN_HTTP_PORT}
  45. firewall_remove ${TURN_PORT}
  46. systemctl stop turn
  47. systemctl disable turn
  48. if [ -f /etc/systemd/system/turn.service ]; then
  49. rm /etc/systemd/system/turn.service
  50. fi
  51. systemctl daemon-reload
  52. apt-get -y remove coturn
  53. rm -rf /var/lib/turn
  54. sed -i "/# TURN Server/,/# End of TURN Server/d" "/etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}"
  55. remove_onion_service turn ${TURN_ONION_PORT}
  56. rm /etc/avahi/services/turn.service
  57. systemctl restart nginx
  58. }
  59. function install_turn {
  60. create_default_web_site
  61. # append the matrix server to the web site config
  62. turn_nginx_site=/etc/nginx/sites-available/$DEFAULT_DOMAIN_NAME
  63. if ! grep -q '# End of TURN Server' "$turn_nginx_site"; then
  64. if [[ "$ONION_ONLY" == "no" ]]; then
  65. { echo '# TURN Server';
  66. echo 'server {';
  67. echo " listen ${TURN_HTTP_PORT} ssl;";
  68. echo " listen [::]:${TURN_HTTP_PORT} ssl;";
  69. echo " server_name ${DEFAULT_DOMAIN_NAME};";
  70. echo '';
  71. echo ' # Security'; } >> "$turn_nginx_site"
  72. function_check nginx_ssl
  73. nginx_ssl "${DEFAULT_DOMAIN_NAME}"
  74. function_check nginx_security_options
  75. nginx_security_options "${DEFAULT_DOMAIN_NAME}"
  76. { echo ' add_header Strict-Transport-Security max-age=15768000;';
  77. echo '';
  78. echo ' # Logs';
  79. echo ' access_log /dev/null;';
  80. echo ' error_log /dev/null;';
  81. echo '';
  82. echo ' # Index';
  83. echo ' index index.html;';
  84. echo '';
  85. echo ' # Location';
  86. echo ' location / {'; } >> "$turn_nginx_site"
  87. function_check nginx_limits
  88. nginx_limits "${DEFAULT_DOMAIN_NAME}" '15m'
  89. { echo " proxy_pass http://localhost:${TURN_PORT};";
  90. echo " proxy_set_header X-Forwarded-For \$remote_addr;";
  91. echo ' }';
  92. echo '}';
  93. echo ''; } >> "$turn_nginx_site"
  94. else
  95. echo '# TURN Server' >> "$turn_nginx_site"
  96. fi
  97. { echo 'server {';
  98. echo " listen 127.0.0.1:$TURN_ONION_PORT default_server;";
  99. echo " server_name $DEFAULT_DOMAIN_NAME;";
  100. echo ''; } >> "$turn_nginx_site"
  101. function_check nginx_security_options
  102. nginx_security_options "$DEFAULT_DOMAIN_NAME"
  103. { echo '';
  104. echo ' # Logs';
  105. echo ' access_log /dev/null;';
  106. echo ' error_log /dev/null;';
  107. echo '';
  108. echo ' # Location';
  109. echo ' location / {'; } >> "$turn_nginx_site"
  110. function_check nginx_limits
  111. nginx_limits "$DEFAULT_DOMAIN_NAME" '15m'
  112. { echo " proxy_pass http://localhost:${TURN_PORT};";
  113. echo " proxy_set_header X-Forwarded-For \$remote_addr;";
  114. echo ' }';
  115. echo '}';
  116. echo '# End of TURN Server'; } >> "$turn_nginx_site"
  117. fi
  118. export DEBIAN_FRONTEND=noninteractive
  119. apt-get -yq install coreutils coturn \
  120. curl file gcc git libevent-2.0-5 \
  121. libevent-dev libffi-dev libffi6 \
  122. libgnutls28-dev libjpeg62-turbo \
  123. libjpeg62-turbo-dev libldap-2.4-2 \
  124. libldap2-dev libsasl2-dev \
  125. libsqlite3-dev libssl-dev \
  126. libssl1.1 libtool libxml2 \
  127. libxml2-dev libxslt1-dev libxslt1.1 \
  128. make python python-dev \
  129. python-pip python-psycopg2 \
  130. python-virtualenv sqlite unzip \
  131. zlib1g zlib1g-dev
  132. pip install --upgrade pip
  133. pip install --upgrade python-ldap
  134. pip install --upgrade lxml
  135. if [ ! -d /var/lib/turn ]; then
  136. mkdir /var/lib/turn
  137. fi
  138. turnkey="$(create_password 30)"
  139. generate_turn_key "$turnkey" /var/lib/turn/turnserver.conf
  140. chmod -R 700 /var/lib/turn/turnserver.conf
  141. chown -R matrix:matrix /var/lib/turn
  142. { echo '[Unit]';
  143. echo 'Description=TURN server';
  144. echo 'After=network.target nginx.target';
  145. echo '';
  146. echo '[Service]';
  147. echo 'Type=simple';
  148. echo 'User=matrix';
  149. echo "WorkingDirectory=/var/lib/turn";
  150. echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf --pidfile /var/lib/matrix/homeserver.pid";
  151. echo "Environment=REPORT_STATS=\"no\"";
  152. echo 'Restart=always';
  153. echo 'RestartSec=10';
  154. echo '';
  155. echo '[Install]';
  156. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/turn.service
  157. systemctl enable turn
  158. systemctl daemon-reload
  159. systemctl start turn
  160. firewall_add turn ${TURN_PORT}
  161. firewall_add turn-http ${TURN_HTTP_PORT}
  162. #TURN_ONION_HOSTNAME=$(add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT})
  163. add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT}
  164. { echo '<?xml version="1.0" standalone="no"?><!--*-nxml-*-->';
  165. echo '<!DOCTYPE service-group SYSTEM "avahi-service.dtd">';
  166. echo '<service-group>';
  167. echo ' <name replace-wildcards="yes">%h TURN</name>';
  168. echo ' <service>';
  169. echo ' <type>_turn._tcp</type>';
  170. echo " <port>$TURN_PORT</port>";
  171. echo ' </service>';
  172. echo ' <service>';
  173. echo ' <type>_turn._udp</type>';
  174. echo " <port>$TURN_PORT</port>";
  175. echo ' </service>';
  176. echo '</service-group>'; } > /etc/avahi/services/turn.service
  177. systemctl restart avahi-daemon
  178. systemctl restart nginx
  179. }
  180. # NOTE: deliberately no exit 0