kasmvnc-entrypoint.sh 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Set default display
  9. export DISPLAY="${DISPLAY:-:20}"
  10. # PipeWire-Pulse server socket path
  11. export PIPEWIRE_LATENCY="32/48000"
  12. export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}"
  13. export PIPEWIRE_RUNTIME_DIR="${PIPEWIRE_RUNTIME_DIR:-${XDG_RUNTIME_DIR:-/tmp}}"
  14. export PULSE_RUNTIME_PATH="${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}"
  15. export PULSE_SERVER="${PULSE_SERVER:-unix:${PULSE_RUNTIME_PATH:-${XDG_RUNTIME_DIR:-/tmp}/pulse}/native}"
  16. # Configure KasmVNC
  17. export KASM_DISPLAY=":21"
  18. yq -i "
  19. .command_line.prompt = false |
  20. .desktop.resolution.width = ${DISPLAY_SIZEW} |
  21. .desktop.resolution.height = ${DISPLAY_SIZEH} |
  22. .desktop.allow_resize = $(echo ${SELKIES_ENABLE_RESIZE-false} | tr '[:upper:]' '[:lower:]') |
  23. .desktop.pixel_depth = ${DISPLAY_CDEPTH} |
  24. .network.interface = \"127.0.0.1\" |
  25. .network.websocket_port = 8082 |
  26. .network.ssl.require_ssl = $(echo ${SELKIES_ENABLE_HTTPS-false} | tr '[:upper:]' '[:lower:]') |
  27. .encoding.max_frame_rate = ${DISPLAY_REFRESH}
  28. " /etc/kasmvnc/kasmvnc.yaml
  29. if [ -n "${SELKIES_HTTPS_CERT}" ]; then yq -i ".network.ssl.pem_certificate = \"${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem}\"" /etc/kasmvnc/kasmvnc.yaml; fi
  30. if [ -n "${SELKIES_HTTPS_KEY}" ]; then yq -i ".network.ssl.pem_key = \"${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key}\"" /etc/kasmvnc/kasmvnc.yaml; fi
  31. if [ "$(echo ${SELKIES_ENABLE_RESIZE} | tr '[:upper:]' '[:lower:]')" = "true" ]; then export KASM_PROXY_FLAG="${KASM_PROXY_FLAG} -r"; fi
  32. mkdir -pm700 ~/.vnc
  33. (echo "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}"; echo "${SELKIES_BASIC_AUTH_PASSWORD:-${PASSWD}}";) | kasmvncpasswd -u "${SELKIES_BASIC_AUTH_USER:-${USER}}" -ow ~/.kasmpasswd
  34. touch ~/.vnc/.de-was-selected
  35. # Wait for X server to start
  36. echo 'Waiting for X Socket' && until [ -S "/tmp/.X11-unix/X${DISPLAY#*:}" ]; do sleep 0.5; done && echo 'X Server is ready'
  37. # Configure NGINX
  38. 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
  39. echo "# Selkies KasmVNC NGINX Configuration
  40. server {
  41. access_log /dev/stdout;
  42. error_log /dev/stderr;
  43. listen 8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  44. listen [::]:8080 $(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "ssl"; fi);
  45. ssl_certificate ${SELKIES_HTTPS_CERT-/etc/ssl/certs/ssl-cert-snakeoil.pem};
  46. ssl_certificate_key ${SELKIES_HTTPS_KEY-/etc/ssl/private/ssl-cert-snakeoil.key};
  47. $(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)
  48. location / {
  49. proxy_set_header Upgrade \$http_upgrade;
  50. proxy_set_header Connection \"upgrade\";
  51. proxy_set_header Host \$host;
  52. proxy_set_header X-Real-IP 127.0.0.1;
  53. proxy_set_header X-Forwarded-For 127.0.0.1;
  54. proxy_set_header X-Forwarded-Proto \$scheme;
  55. proxy_http_version 1.1;
  56. proxy_read_timeout 3600s;
  57. proxy_send_timeout 3600s;
  58. proxy_connect_timeout 3600s;
  59. proxy_buffering off;
  60. client_max_body_size 10M;
  61. proxy_pass http$(if [ \"$(echo ${SELKIES_ENABLE_HTTPS} | tr '[:upper:]' '[:lower:]')\" = \"true\" ]; then echo -n "s"; fi)://localhost:8082;
  62. }
  63. }" | tee /etc/nginx/sites-available/default > /dev/null
  64. # Run KasmVNC
  65. if ls ~/.vnc/*\:"${KASM_DISPLAY#*:}".pid >/dev/null 2>&1; then kasmvncserver -kill "${KASM_DISPLAY}"; fi
  66. kasmvncserver "${KASM_DISPLAY}" -geometry "${DISPLAY_SIZEW}x${DISPLAY_SIZEH}" -depth "${DISPLAY_CDEPTH}" -noxstartup -FrameRate "${DISPLAY_REFRESH}" -interface 127.0.0.1 -rfbport 9082 -websocketPort 8082 -disableBasicAuth -AlwaysShared -BlacklistTimeout 0 ${KASM_FLAG}
  67. until [ -S "/tmp/.X11-unix/X${KASM_DISPLAY#*:}" ]; do sleep 0.5; done;
  68. kasmxproxy -a "${DISPLAY}" -v "${KASM_DISPLAY}" -f "${DISPLAY_REFRESH}" ${KASM_PROXY_FLAG}