freedombone-utils-turn 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. function generate_turn_key {
  32. local turnkey="${1}"
  33. local filepath="${2}"
  34. echo "lt-cred-mech" > "${filepath}"
  35. echo "use-auth-secret" >> "${filepath}"
  36. echo "static-auth-secret=${turnkey}" >> "${filepath}"
  37. echo "realm=turn.${DEFAULT_DOMAIN_NAME}" >> "${filepath}"
  38. echo "cert=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.crt" >> "${filepath}"
  39. echo "pkey=$MATRIX_DATA_DIR/${DEFAULT_DOMAIN_NAME}.tls.key" >> "${filepath}"
  40. }
  41. function remove_turn {
  42. firewall_remove ${TURN_PORT}
  43. }
  44. function remove_turn {
  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. apt-get -y remove coturn
  52. rm -rf /var/lib/turn
  53. }
  54. function install_turn {
  55. export DEBIAN_FRONTEND=noninteractive
  56. apt-get -yq install coreutils coturn \
  57. curl file gcc git libevent-2.0-5 \
  58. libevent-dev libffi-dev libffi6 \
  59. libgnutls28-dev libjpeg62-turbo \
  60. libjpeg62-turbo-dev libldap-2.4-2 \
  61. libldap2-dev libsasl2-dev \
  62. libsqlite3-dev libssl-dev \
  63. libssl1.0.0 libtool libxml2 \
  64. libxml2-dev libxslt1-dev libxslt1.1 \
  65. make python python-dev \
  66. python-pip python-psycopg2 \
  67. python-virtualenv sqlite unzip \
  68. zlib1g zlib1g-dev
  69. pip install --upgrade pip
  70. pip install --upgrade python-ldap
  71. pip install --upgrade lxml
  72. if [ ! -d /var/lib/turn ]; then
  73. mkdir /var/lib/turn
  74. fi
  75. turnkey="$(create_password 30)"
  76. generate_turn_key $turnkey /var/lib/turn/turnserver.conf
  77. chown -R matrix:matrix /var/lib/turn
  78. echo '[Unit]' > /etc/systemd/system/turn.service
  79. echo 'Description=TURN server' >> /etc/systemd/system/turn.service
  80. echo 'After=network.target nginx.target' >> /etc/systemd/system/turn.service
  81. echo '' >> /etc/systemd/system/turn.service
  82. echo '[Service]' >> /etc/systemd/system/turn.service
  83. echo 'Type=simple' >> /etc/systemd/system/turn.service
  84. echo 'User=matrix' >> /etc/systemd/system/turn.service
  85. echo "WorkingDirectory=/var/lib/turn" >> /etc/systemd/system/turn.service
  86. echo "ExecStart=/usr/bin/turnserver -c /var/lib/turn/turnserver.conf" >> /etc/systemd/system/turn.service
  87. echo "Environment=REPORT_STATS=\"no\"" >> /etc/systemd/system/turn.service
  88. echo 'Restart=always' >> /etc/systemd/system/turn.service
  89. echo 'RestartSec=10' >> /etc/systemd/system/turn.service
  90. echo '' >> /etc/systemd/system/turn.service
  91. echo '[Install]' >> /etc/systemd/system/turn.service
  92. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/turn.service
  93. systemctl enable turn
  94. systemctl daemon-reload
  95. systemctl start turn
  96. firewall_add turn ${TURN_PORT}
  97. }
  98. # NOTE: deliberately no exit 0