freedombone-utils-turn 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # TURN server functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. TURN_PORT=3478
  31. TURN_HTTP_PORT=3407
  32. TURN_ONION_PORT=8110
  33. function generate_turn_key {
  34. local turnkey="${1}"
  35. local filepath="${2}"
  36. echo "lt-cred-mech" > "${filepath}"
  37. echo "use-auth-secret" >> "${filepath}"
  38. echo "static-auth-secret=${turnkey}" >> "${filepath}"
  39. echo "realm=turn.${DEFAULT_DOMAIN_NAME}" >> "${filepath}"
  40. if [[ $ONION_ONLY == 'no' ]]; then
  41. echo "cert=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.crt" >> "${filepath}"
  42. echo "pkey=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.key" >> "${filepath}"
  43. fi
  44. }
  45. function remove_turn {
  46. firewall_remove ${TURN_HTTP_PORT}
  47. systemctl stop turn
  48. systemctl disable turn
  49. if [ -f /etc/systemd/system/turn.service ]; then
  50. rm /etc/systemd/system/turn.service
  51. fi
  52. systemctl daemon-reload
  53. apt-get -y remove coturn
  54. rm -rf /var/lib/turn
  55. sed -i "/# TURN Server/,/# End of TURN Server/d" /etc/nginx/sites-available/${DEFAULT_DOMAIN_NAME}
  56. remove_onion_service turn ${TURN_ONION_PORT}
  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' >> $turn_nginx_site
  66. echo 'server {' >> $turn_nginx_site
  67. echo " listen ${TURN_HTTP_PORT} ssl;" >> $turn_nginx_site
  68. echo " listen [::]:${TURN_HTTP_PORT} ssl;" >> $turn_nginx_site
  69. echo " server_name ${DEFAULT_DOMAIN_NAME};" >> $turn_nginx_site
  70. echo '' >> $turn_nginx_site
  71. echo ' # Security' >> $turn_nginx_site
  72. function_check nginx_ssl
  73. nginx_ssl ${DEFAULT_DOMAIN_NAME}
  74. function_check nginx_disable_sniffing
  75. nginx_disable_sniffing ${DEFAULT_DOMAIN_NAME}
  76. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $turn_nginx_site
  77. echo '' >> $turn_nginx_site
  78. echo ' # Logs' >> $turn_nginx_site
  79. echo ' access_log /dev/null;' >> $turn_nginx_site
  80. echo ' error_log /dev/null;' >> $turn_nginx_site
  81. echo '' >> $turn_nginx_site
  82. echo ' # Index' >> $turn_nginx_site
  83. echo ' index index.html;' >> $turn_nginx_site
  84. echo '' >> $turn_nginx_site
  85. echo ' # Location' >> $turn_nginx_site
  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};" >> $turn_nginx_site
  90. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $turn_nginx_site
  91. echo ' }' >> $turn_nginx_site
  92. echo '}' >> $turn_nginx_site
  93. echo '' >> $turn_nginx_site
  94. else
  95. echo '# TURN Server' >> $turn_nginx_site
  96. fi
  97. echo 'server {' >> $turn_nginx_site
  98. echo " listen 127.0.0.1:$TURN_ONION_PORT default_server;" >> $turn_nginx_site
  99. echo " server_name $DEFAULT_DOMAIN_NAME;" >> $turn_nginx_site
  100. echo '' >> $turn_nginx_site
  101. function_check nginx_disable_sniffing
  102. nginx_disable_sniffing $DEFAULT_DOMAIN_NAME
  103. echo '' >> $turn_nginx_site
  104. echo ' # Logs' >> $turn_nginx_site
  105. echo ' access_log /dev/null;' >> $turn_nginx_site
  106. echo ' error_log /dev/null;' >> $turn_nginx_site
  107. echo '' >> $turn_nginx_site
  108. echo ' # Location' >> $turn_nginx_site
  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};" >> $turn_nginx_site
  113. echo ' proxy_set_header X-Forwarded-For $remote_addr;' >> $turn_nginx_site
  114. echo ' }' >> $turn_nginx_site
  115. echo '}' >> $turn_nginx_site
  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]' > /etc/systemd/system/turn.service
  143. echo 'Description=TURN server' >> /etc/systemd/system/turn.service
  144. echo 'After=network.target nginx.target' >> /etc/systemd/system/turn.service
  145. echo '' >> /etc/systemd/system/turn.service
  146. echo '[Service]' >> /etc/systemd/system/turn.service
  147. echo 'Type=simple' >> /etc/systemd/system/turn.service
  148. echo 'User=matrix' >> /etc/systemd/system/turn.service
  149. echo "WorkingDirectory=/var/lib/turn" >> /etc/systemd/system/turn.service
  150. echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf --pidfile /var/lib/matrix/homeserver.pid" >> /etc/systemd/system/turn.service
  151. echo "Environment=REPORT_STATS=\"no\"" >> /etc/systemd/system/turn.service
  152. echo 'Restart=always' >> /etc/systemd/system/turn.service
  153. echo 'RestartSec=10' >> /etc/systemd/system/turn.service
  154. echo '' >> /etc/systemd/system/turn.service
  155. echo '[Install]' >> /etc/systemd/system/turn.service
  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_HTTP_PORT}
  161. TURN_ONION_HOSTNAME=$(add_onion_service turn ${TURN_PORT} ${TURN_ONION_PORT})
  162. systemctl restart nginx
  163. }
  164. # NOTE: deliberately no exit 0