33# Exit on error, undefined variables, and pipe failures
44set -euo pipefail
55
6+ error () { printf " 💀 ERROR: %s\n" " $@ " ; exit 1; }
7+
68# Function to check if vncserver is already installed
79check_installed () {
810 if command -v vncserver & > /dev/null; then
@@ -188,7 +190,7 @@ if command -v sudo &> /dev/null && sudo -n true 2> /dev/null; then
188190 SUDO=sudo
189191else
190192 kasm_config_file=" $HOME /.vnc/kasmvnc.yaml"
191- SUDO=
193+ SUDO=" "
192194
193195 echo " WARNING: Sudo access not available, using user config dir!"
194196
@@ -206,6 +208,7 @@ echo "Writing KasmVNC config to $kasm_config_file"
206208$SUDO tee " $kasm_config_file " > /dev/null << EOF
207209network:
208210 protocol: http
211+ interface: 127.0.0.1
209212 websocket_port: ${PORT}
210213 ssl:
211214 require_ssl: false
@@ -220,16 +223,82 @@ EOF
220223# and does not listen publicly
221224echo -e " password\npassword\n" | vncpasswd -wo -u " $USER "
222225
226+ get_http_dir () {
227+ # determine the served file path
228+ # Start with the default
229+ httpd_directory=" /usr/share/kasmvnc/www"
230+
231+ # Check the system configuration path
232+ if [[ -e /etc/kasmvnc/kasmvnc.yaml ]]; then
233+ d=($( grep -E " ^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml) )
234+ # If this grep is successful, it will return:
235+ # httpd_directory: /usr/share/kasmvnc/www
236+ if [[ $$ {# d[@]} -eq 2 && -d "$${d[1]}" ]]; then
237+ httpd_directory= " $$ {d[1]}"
238+ fi
239+ fi
240+
241+ # Check the home directory for overriding values
242+ if [[ -e " $HOME /.vnc/kasmvnc.yaml" ]]; then
243+ d=($( grep -E " ^\s*httpd_directory:.*$" /etc/kasmvnc/kasmvnc.yaml) )
244+ if [[ $$ {# d[@]} -eq 2 && -d "$${d[1]}" ]]; then
245+ httpd_directory= " $$ {d[1]}"
246+ fi
247+ fi
248+ echo $httpd_directory
249+ }
250+
251+ fix_server_index_file (){
252+ local fname= $$ {FUNCNAME[0]} # gets current function name
253+ if [[ $# -ne 1 ]]; then
254+ error " $fname requires exactly 1 parameter:\n\tpath to KasmVNC httpd_directory"
255+ fi
256+ local httpdir= " $1 "
257+ if [[ ! -d " $httpdir " ]]; then
258+ error " $fname : $httpdir is not a directory"
259+ fi
260+ pushd " $httpdir " > /dev/null
261+
262+ cat << ' EOH' > /tmp/path_vnc.html
263+ ${PATH_VNC_HTML}
264+ EOH
265+ $SUDO mv /tmp/path_vnc.html .
266+ # check for the switcheroo
267+ if [[ -f " index.html" && -L " vnc.html" ]]; then
268+ $SUDO mv $httpdir /index.html $httpdir /vnc.html
269+ fi
270+ $SUDO ln -s -f path_vnc.html index.html
271+ popd > /dev/null
272+ }
273+
274+ patch_kasm_http_files (){
275+ homedir= $( get_http_dir)
276+ fix_server_index_file " $homedir "
277+ }
278+
279+ if [[ " ${SUBDOMAIN} " == " false" ]]; then
280+ echo " 🩹 Patching up webserver files to support path-sharing..."
281+ patch_kasm_http_files
282+ fi
283+
284+ VNC_LOG= " /tmp/kasmvncserver.log"
223285# Start the server
224286printf " 🚀 Starting KasmVNC server...\n"
225- vncserver -select-de " ${DESKTOP_ENVIRONMENT} " -disableBasicAuth > /tmp/kasmvncserver.log 2>&1 &
226- pid=$!
227-
228- # Wait for server to start
229- sleep 5
230- grep -v ' ^[[:space:]]*$' /tmp/kasmvncserver.log | tail -n 10
231- if ps -p $pid | grep -q " ^$pid " ; then
232- echo " ERROR: Failed to start KasmVNC server. Check full logs at /tmp/kasmvncserver.log"
287+
288+ set +e
289+ vncserver -s elect-de " ${DESKTOP_ENVIRONMENT} " -d isableBasicAuth > " $VNC_LOG " 2>&1
290+ RETVAL= $?
291+ set -e
292+
293+ if [[ $RETVAL -ne 0 ]]; then
294+ echo " ERROR: Failed to start KasmVNC server. Return code: $RETVAL "
295+ if [[ -f " $VNC_LOG " ]]; then
296+ echo " Full logs:"
297+ cat " $VNC_LOG "
298+ else
299+ echo " ERROR: Log file not found: $VNC_LOG "
300+ fi
233301 exit 1
234302fi
303+
235304printf " 🚀 KasmVNC server started successfully!\n"
0 commit comments