selkies-gstreamer-entrypoint.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. set -e
  6. # Wait for XDG_RUNTIME_DIR
  7. until [ -d "${XDG_RUNTIME_DIR}" ]; do sleep 0.5; done
  8. # Configure joystick interposer
  9. export SELKIES_INTERPOSER='/usr/$LIB/selkies_joystick_interposer.so'
  10. export LD_PRELOAD="${SELKIES_INTERPOSER}${LD_PRELOAD:+:${LD_PRELOAD}}"
  11. # Set default display
  12. export DISPLAY="${DISPLAY:-:20}"
  13. # PipeWire-Pulse server socket path
  14. export PIPEWIRE_LATENCY="128/48000"
  15. export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}"
  16. export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
  17. export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
  18. export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
  19. # Export environment variables required for Selkies-GStreamer
  20. export GST_DEBUG="${GST_DEBUG:-*:2}"
  21. export GSTREAMER_PATH=/opt/gstreamer
  22. # Source environment for GStreamer
  23. . /opt/gstreamer/gst-env
  24. export SELKIES_ENCODER="${SELKIES_ENCODER:-x264enc}"
  25. export SELKIES_ENABLE_RESIZE="${SELKIES_ENABLE_RESIZE:-false}"
  26. if [ -z "${SELKIES_TURN_REST_URI}" ] && { { [ -z "${SELKIES_TURN_USERNAME}" ] || [ -z "${SELKIES_TURN_PASSWORD}" ]; } && [ -z "${SELKIES_TURN_SHARED_SECRET}" ] || [ -z "${SELKIES_TURN_HOST}" ] || [ -z "${SELKIES_TURN_PORT}" ]; }; then
  27. export TURN_RANDOM_PASSWORD="$(tr -dc 'A-Za-z0-9' < /dev/urandom 2>/dev/null | head -c 24)"
  28. export SELKIES_TURN_HOST="${SELKIES_TURN_HOST:-$(dig -4 TXT +short @ns1.google.com o-o.myaddr.l.google.com 2>/dev/null | { read output; if [ -z "$output" ] || echo "$output" | grep -q '^;;'; then exit 1; else echo "$(echo $output | sed 's,\",,g')"; fi } || dig -6 TXT +short @ns1.google.com o-o.myaddr.l.google.com 2>/dev/null | { read output; if [ -z "$output" ] || echo "$output" | grep -q '^;;'; then exit 1; else echo "[$(echo $output | sed 's,\",,g')]"; fi } || hostname -I 2>/dev/null | awk '{print $1; exit}' || echo '127.0.0.1')}"
  29. export TURN_EXTERNAL_IP="${TURN_EXTERNAL_IP:-$(getent ahostsv4 $(echo ${SELKIES_TURN_HOST} | tr -d '[]') 2>/dev/null | awk '{print $1; exit}' || getent ahostsv6 $(echo ${SELKIES_TURN_HOST} | tr -d '[]') 2>/dev/null | awk '{print "[" $1 "]"; exit}')}"
  30. export SELKIES_TURN_PORT="${SELKIES_TURN_PORT:-3478}"
  31. export SELKIES_TURN_USERNAME="selkies"
  32. export SELKIES_TURN_PASSWORD="${TURN_RANDOM_PASSWORD}"
  33. export SELKIES_TURN_PROTOCOL="${SELKIES_TURN_PROTOCOL:-tcp}"
  34. export SELKIES_STUN_HOST="${SELKIES_STUN_HOST:-stun.l.google.com}"
  35. export SELKIES_STUN_PORT="${SELKIES_STUN_PORT:-19302}"
  36. /etc/start-turnserver.sh &
  37. fi
  38. # Wait for X server to start
  39. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  40. # Configure NGINX
  41. if [ "$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')" != "false" ]; then htpasswd -bcm "${XDG_RUNTIME_DIR}/.htpasswd" "${SELKIES_BASIC_AUTH_USER:-${USER}}" "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}"; fi
  42. echo "# Selkies-GStreamer NGINX Configuration
  43. server {
  44. access_log /dev/stdout;
  45. error_log /dev/stderr;
  46. listen ${NGINX_PORT:-8080} $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  47. listen [::]:${NGINX_PORT:-8080} $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  48. ssl_certificate ${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem};
  49. ssl_certificate_key ${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key};
  50. $(if [ \"$(echo ${SELKIES_ENABLE_BASIC_AUTH} | tr '[:upper:]' '[:lower:]')\" != \"false\" ]; then echo "auth_basic \"Selkies\";"; echo -n " auth_basic_user_file ${XDG_RUNTIME_DIR}/.htpasswd;"; fi)
  51. location / {
  52. root /opt/gst-web/;
  53. index index.html index.htm;
  54. }
  55. location /health {
  56. proxy_http_version 1.1;
  57. proxy_read_timeout 3600s;
  58. proxy_send_timeout 3600s;
  59. proxy_connect_timeout 3600s;
  60. proxy_buffering off;
  61. client_max_body_size 10M;
  62. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  63. }
  64. location /turn {
  65. proxy_http_version 1.1;
  66. proxy_read_timeout 3600s;
  67. proxy_send_timeout 3600s;
  68. proxy_connect_timeout 3600s;
  69. proxy_buffering off;
  70. client_max_body_size 10M;
  71. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  72. }
  73. location /ws {
  74. proxy_set_header Upgrade \$http_upgrade;
  75. proxy_set_header Connection \"upgrade\";
  76. proxy_set_header Host \$host;
  77. proxy_set_header X-Real-IP \$remote_addr;
  78. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  79. proxy_set_header X-Forwarded-Proto \$scheme;
  80. proxy_http_version 1.1;
  81. proxy_read_timeout 3600s;
  82. proxy_send_timeout 3600s;
  83. proxy_connect_timeout 3600s;
  84. proxy_buffering off;
  85. client_max_body_size 10M;
  86. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  87. }
  88. location /webrtc/signalling {
  89. proxy_set_header Upgrade \$http_upgrade;
  90. proxy_set_header Connection \"upgrade\";
  91. proxy_set_header Host \$host;
  92. proxy_set_header X-Real-IP \$remote_addr;
  93. proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
  94. proxy_set_header X-Forwarded-Proto \$scheme;
  95. proxy_http_version 1.1;
  96. proxy_read_timeout 3600s;
  97. proxy_send_timeout 3600s;
  98. proxy_connect_timeout 3600s;
  99. proxy_buffering off;
  100. client_max_body_size 10M;
  101. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_PORT:-8081};
  102. }
  103. location /metrics {
  104. proxy_http_version 1.1;
  105. proxy_read_timeout 3600s;
  106. proxy_send_timeout 3600s;
  107. proxy_connect_timeout 3600s;
  108. proxy_buffering off;
  109. client_max_body_size 10M;
  110. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:${SELKIES_METRICS_HTTP_PORT:-9081};
  111. }
  112. error_page 500 502 503 504 /50x.html;
  113. location = /50x.html {
  114. root /opt/gst-web/;
  115. }
  116. }" | tee /etc/nginx/sites-available/default > /dev/null
  117. # Clear the cache registry
  118. rm -rf "${HOME}/.cache/gstreamer-1.0"
  119. # Start the Selkies-GStreamer WebRTC HTML5 remote desktop application
  120. selkies-gstreamer \
  121. --addr="localhost" \
  122. --port="${SELKIES_PORT:-8081}" \
  123. --enable_basic_auth="false" \
  124. --enable_metrics_http="true" \
  125. --metrics_http_port="${SELKIES_METRICS_HTTP_PORT:-9081}" \
  126. $@