start_matrix.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/bash
  2. set -eux
  3. if [[ ! -f /app/data/synapse/homeserver.yaml ]]; then
  4. echo "=> Detected first run"
  5. # create dirs (for whatever reason the nginx dir couldn't get created by nginx)
  6. mkdir -p /app/data/synapse \
  7. /app/data/nginx/fastcgi \
  8. /run/nginx_log \
  9. /run/synapse \
  10. /run/turn_log \
  11. /app/data/appservice/config /app/data/appservice/db
  12. # copy matrix-appservice-webhooks config
  13. # cp /app/code/matrix-appservice-webhooks/config/sample.yaml /app/data/appservice/config/config.yaml
  14. # ln -sf /app/data/appservice/appservice-registration-webhooks.yaml /app/data/synapse/appservice-registration-webhooks.yaml
  15. # create certs
  16. openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out /app/data/synapse/${APP_DOMAIN}.tls.crt -keyout /app/data/synapse/${APP_DOMAIN}.tls.key -subj "/C=DE/ST=Germany/L=Germany/O=Example/OU=Example/CN=${APP_DOMAIN}"
  17. # fix permissions
  18. chown -R www-data.www-data /run/synapse \
  19. /run/turn_log \
  20. /app/data
  21. # copy turn config
  22. cp /usr/share/coturn/examples/etc/turnserver.conf /app/data/turnserver.conf
  23. # copy email templates for matrix
  24. cp -r /app/code/synapse/synapse/res/templates /app/data/templates
  25. # set default TURN listening port
  26. if [ -z ${TURN_TLS_PORT+x} ]; then TURN_TLS_PORT="3478"; else echo "TURN_TLS_PORT is set to '$TURN_TLS_PORT'"; fi
  27. cd /app/data/synapse
  28. python3 -m synapse.app.homeserver \
  29. --server-name ${APP_DOMAIN} \
  30. --config-path homeserver.yaml \
  31. --report-stats=no \
  32. --generate-config
  33. # synapse config
  34. sed -i "s/server_name:.*/server_name: ${APP_DOMAIN}/" homeserver.yaml
  35. sed -i "s/#public_baseurl:.*/public_baseurl: https:\/\/${APP_DOMAIN}/" homeserver.yaml
  36. sed -i "s/web_client:.*/web_client: False/" homeserver.yaml
  37. sed -i "s/bind_addresses: \['::1'.*/bind_addresses: \['0.0.0.0'\]/" homeserver.yaml
  38. sed -i "s/sqlite3/psycopg2/" homeserver.yaml
  39. sed -i "s/ database: .*/ user: ${POSTGRESQL_USERNAME}\n password: ${POSTGRESQL_PASSWORD}\n database: ${POSTGRESQL_DATABASE}\n host: ${POSTGRESQL_HOST}\n cp_min: 5\n cp_max: 10/" homeserver.yaml
  40. sed -i "s/#enable_registration:.*/enable_registration: True/" homeserver.yaml
  41. sed -i "s/#password_providers:/password_providers:/" homeserver.yaml
  42. sed -i 's/# - module: "ldap_auth_provider.LdapAuthProvider"/ - module: "ldap_auth_provider.LdapAuthProvider"/' homeserver.yaml
  43. sed -i 's/# config:/ config:/' homeserver.yaml
  44. sed -i 's/# enabled: true/ enabled: true/' homeserver.yaml
  45. sed -i "s,# uri: .*, uri: \"${LDAP_URL}\"," homeserver.yaml
  46. sed -i 's/# start_tls: true/ start_tls: false/' homeserver.yaml
  47. sed -i 's/# base: "ou=users,dc=example,dc=com"/ base: "ou=users,dc=cloudron"/' homeserver.yaml
  48. sed -i 's/# attributes:/ attributes:/' homeserver.yaml
  49. sed -i 's/# uid: "cn"/ uid: "username"/' homeserver.yaml
  50. sed -i 's/# mail: "email"/ mail: "mail"/' homeserver.yaml
  51. sed -i 's/# name: "givenName"/ name: "username"/' homeserver.yaml
  52. sed -i 's/#max_upload_size:.*/max_upload_size: "100M"/' homeserver.yaml
  53. sed -i 's/#auto_join_rooms:/auto_join_rooms:/' homeserver.yaml
  54. sed -i 's/# - "#example:example.com"/ - "#example:example.com"/' homeserver.yaml
  55. sed -i "s/example:example.com/discuss:${APP_DOMAIN}/" homeserver.yaml
  56. sed -i "s/turn_allow_guests:.*/turn_allow_guests: False/" homeserver.yaml
  57. sed -i "s/enable_group_creation:.*/enable_group_creation: True/" homeserver.yaml
  58. sed -i "s/enable_group_creation:.*/enable_group_creation: True/" homeserver.yaml
  59. sed -i "s/#url_preview_enabled:.*/url_preview_enabled: true/" homeserver.yaml
  60. sed -i "s/#url_preview_ip_range_blacklist.*/url_preview_ip_range_blacklist:\n - '127.0.0.0\/8'\n - '10.0.0.0\/8'/" homeserver.yaml
  61. sed -i "s/## Server ##/## Server ##\n\nno_tls: True/" homeserver.yaml
  62. # also enable the tls port, not quite sure why this is needed, but without it, no integratiosn are possible
  63. #sed -i "s/#- port: 8448/- port: 8448/" homeserver.yaml
  64. #sed -i "s/# type: http/ type: http/" homeserver.yaml
  65. #sed -i "s/# tls: true/ tls: true/" homeserver.yaml
  66. #sed -i "s/# resources:/ resources:/" homeserver.yaml
  67. #sed -i "s/# - names: \[client, federation\]/ - names: \[client, federation\]/" homeserver.yaml
  68. #sed -i "s/#tls_certificate_path:/tls_certificate_path:/" homeserver.yaml
  69. #sed -i "s/#tls_private_key_path:/tls_private_key_path:/" homeserver.yaml
  70. # get synapse log name and set path
  71. LOG_CONFIG=$(ls *.log.config)
  72. sed -i "s/log_file: .*/log_file: \"\/run\/synapse\/homeserver.log\"/" homeserver.yaml
  73. sed -i "s/ filename:.*/ filename: \/run\/synapse\/homeserver.log/" ${LOG_CONFIG}
  74. # coturn
  75. TURNPWD=$(pwgen -s 64 1)
  76. sed -i "s/#tls-listening-port=5349/tls-listening-port=3478/" /app/data/turnserver.conf
  77. sed -i "s/#realm=mycompany.org/realm=${APP_DOMAIN}/" /app/data/turnserver.conf
  78. sed -i "s/#lt-cred-mech/lt-cred-mech/" /app/data/turnserver.conf
  79. sed -i "s/#use-auth-secret/use-auth-secret/" /app/data/turnserver.conf
  80. sed -i "s/#lt-cred-mech/lt-cred-mech/" /app/data/turnserver.conf
  81. sed -i "s/#static-auth-secret=.*/static-auth-secret=${TURNPWD}/" /app/data/turnserver.conf
  82. sed -i "s/#turn_uris: .*/turn_uris: [\"turn:${APP_DOMAIN}:${TURN_TLS_PORT}?transport=udp\", \"turn:${APP_DOMAIN}:${TURN_TLS_PORT}?transport=tcp\"]/" homeserver.yaml
  83. sed -i "s/#turn_shared_secret: .*/turn_shared_secret: \"${TURNPWD}\"/" homeserver.yaml
  84. sed -i "s/#cipher-list=.*/cipher-list=\"HIGH\"/" /app/data/turnserver.conf
  85. sed -i "s/#log-file=.*/log-file=\/run\/turn_log\/turn.log/" /app/data/turnserver.conf
  86. # get cert names from synapse
  87. TLS_CRT=$(ls *.tls.crt)
  88. TLS_KEY=$(ls *.tls.key)
  89. #TLS_DH=$(ls *.tls.dh)
  90. sed -i "s,#cert=.*,cert=/app/data/synapse/${TLS_CRT}," /app/data/turnserver.conf
  91. sed -i "s,#pkey=.*,pkey=/app/data/synapse/${TLS_KEY}," /app/data/turnserver.conf
  92. #sed -i "s,#dh-file=.*,dh-file=/app/data/synapse/${TLS_DH}," /app/data/turnserver.conf
  93. # set up email
  94. sed -i 's/#email:/email:/' homeserver.yaml
  95. sed -i 's/# enable_notifs: false/ enable_notifs: true/' homeserver.yaml
  96. sed -i 's/# smtp_host: \"localhost\"/ smtp_host: \"${MAIL_SMTP_SERVER}\"/' homeserver.yaml
  97. sed -i 's/# smtp_port: .*/ smtp_port: ${MAIL_SMTP_PORT}/' homeserver.yaml
  98. sed -i 's/# smtp_user: \"exampleusername\"/ smtp_user: \"${MAIL_SMTP_USERNAME}\"/' homeserver.yaml
  99. sed -i 's/# smtp_pass: \"examplepassword\"/ smtp_pass: \"${MAIL_SMTP_PASSWORD}\"/' homeserver.yaml
  100. sed -i 's/# require_transport_security: False/ require_transport_security: false/' homeserver.yaml
  101. sed -i "s/# notif_from: .*/ notif_from: \"Your Friendly Matrix Home Server <${MAIL_FROM}>\"/" homeserver.yaml
  102. sed -i 's/# app_name: Matrix/ app_name: Matrix/' homeserver.yaml
  103. sed -i 's/# notif_template_html: .*/ notif_template_html: notif_mail.html/' homeserver.yaml
  104. sed -i 's/# notif_template_text: .*/ notif_template_text: notif_mail.txt/' homeserver.yaml
  105. sed -i 's/# notif_for_new_users: True/ notif_for_new_users: True/' homeserver.yaml
  106. sed -i "s,#tls_fingerprints:.*,tls_fingerprints: [{sha256: \"${TLS_FINGERPRINT}\"}]," homeserver.yaml
  107. fi
  108. # logs
  109. mkdir -p /run/nginx_log /run/synapse /run/turn_log
  110. # fix permissions
  111. chown -R www-data.www-data /run/synapse /run/turn_log /app/data
  112. cd /app/data/synapse
  113. # check if TURN port has changed and update it
  114. if [ -z ${TURN_TLS_PORT+x} ]; then TURN_TLS_PORT="3478"; fi
  115. sed -i "s/turn_uris: .*/turn_uris: [\"turn:${APP_DOMAIN}:${TURN_TLS_PORT}?transport=udp\", \"turn:${APP_DOMAIN}:${TURN_TLS_PORT}?transport=tcp\"]/" homeserver.yaml
  116. sed -i "s/tls-listening-port=.*/tls-listening-port=${TURN_TLS_PORT}/" /app/data/turnserver.conf
  117. # check if certificate changed and update fingerprint
  118. TLS_FINGERPRINT=$(openssl s_client -connect ${APP_DOMAIN}:${TURN_TLS_PORT} < /dev/null 2> /dev/null | openssl x509 -outform DER | openssl sha256 -binary | base64 | tr -d '=')
  119. sed -i "s,^tls_fingerprints:.*,tls_fingerprints: [{sha256: \"${TLS_FINGERPRINT}\"}]," homeserver.yaml
  120. gosu www-data turnserver -c /app/data/turnserver.conf --daemon -v
  121. # update user and pass in case they changed
  122. sed -i "s/ user: .*/ user: ${POSTGRESQL_USERNAME}/" homeserver.yaml
  123. sed -i "s/ password: .*/ password: ${POSTGRESQL_PASSWORD}/" homeserver.yaml
  124. sed -i "s/ database: .*/ database: ${POSTGRESQL_DATABASE}/" homeserver.yaml
  125. sed -i "s, uri: .*, uri: \"${LDAP_URL}\"," homeserver.yaml
  126. #update email settings in case they changed
  127. sed -i "s/ smtp_host:.*/ smtp_host: \"${MAIL_SMTP_SERVER}\"/" homeserver.yaml
  128. sed -i "s/ smtp_port:.*/ smtp_port: ${MAIL_SMTP_PORT}/" homeserver.yaml
  129. sed -i "s/ smtp_user:.*/ smtp_user: \"${MAIL_SMTP_USERNAME}\"/" homeserver.yaml
  130. sed -i "s/ smtp_pass:.*/ smtp_pass: \"${MAIL_SMTP_PASSWORD}\"/" homeserver.yaml
  131. gosu www-data python3 -m synapse.app.homeserver --config-path homeserver.yaml &> /dev/null &
  132. exec /usr/sbin/nginx -g 'daemon off;'