]> git.proxmox.com Git - mirror_novnc.git/blob - utils/launch.sh
Merge pull request #245 from wrouesnel/vnc-auto-fix
[mirror_novnc.git] / utils / launch.sh
1 #!/usr/bin/env bash
2
3 usage() {
4 if [ "$*" ]; then
5 echo "$*"
6 echo
7 fi
8 echo "Usage: ${NAME} [--listen PORT] [--vnc VNC_HOST:PORT] [--cert CERT]"
9 echo
10 echo "Starts the WebSockets proxy and a mini-webserver and "
11 echo "provides a cut-and-paste URL to go to."
12 echo
13 echo " --listen PORT Port for proxy/webserver to listen on"
14 echo " Default: 6080"
15 echo " --vnc VNC_HOST:PORT VNC server host:port proxy target"
16 echo " Default: localhost:5900"
17 echo " --cert CERT Path to combined cert/key file"
18 echo " Default: self.pem"
19 echo " --web WEB Path to web files (e.g. vnc.html)"
20 echo " Default: ./"
21 exit 2
22 }
23
24 NAME="$(basename $0)"
25 HERE="$(cd "$(dirname "$0")" && pwd)"
26 PORT="6080"
27 VNC_DEST="localhost:5900"
28 CERT=""
29 WEB=""
30 proxy_pid=""
31
32 die() {
33 echo "$*"
34 exit 1
35 }
36
37 cleanup() {
38 trap - TERM QUIT INT EXIT
39 trap "true" CHLD # Ignore cleanup messages
40 echo
41 if [ -n "${proxy_pid}" ]; then
42 echo "Terminating WebSockets proxy (${proxy_pid})"
43 kill ${proxy_pid}
44 fi
45 }
46
47 # Process Arguments
48
49 # Arguments that only apply to chrooter itself
50 while [ "$*" ]; do
51 param=$1; shift; OPTARG=$1
52 case $param in
53 --listen) PORT="${OPTARG}"; shift ;;
54 --vnc) VNC_DEST="${OPTARG}"; shift ;;
55 --cert) CERT="${OPTARG}"; shift ;;
56 --web) WEB="${OPTARG}"; shift ;;
57 -h|--help) usage ;;
58 -*) usage "Unknown chrooter option: ${param}" ;;
59 *) break ;;
60 esac
61 done
62
63 # Sanity checks
64 which netstat >/dev/null 2>&1 \
65 || die "Must have netstat installed"
66
67 netstat -ltn | grep -qs "${PORT} .*LISTEN" \
68 && die "Port ${PORT} in use. Try --listen PORT"
69
70 trap "cleanup" TERM QUIT INT EXIT
71
72 # Find vnc.html
73 if [ -n "${WEB}" ]; then
74 if [ ! -e "${WEB}/vnc.html" ]; then
75 die "Could not find ${WEB}/vnc.html"
76 fi
77 elif [ -e "$(pwd)/vnc.html" ]; then
78 WEB=$(pwd)
79 elif [ -e "${HERE}/../vnc.html" ]; then
80 WEB=${HERE}/../
81 elif [ -e "${HERE}/vnc.html" ]; then
82 WEB=${HERE}
83 elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
84 WEB=${HERE}/../share/novnc/
85 else
86 die "Could not find vnc.html"
87 fi
88
89 # Find self.pem
90 if [ -n "${CERT}" ]; then
91 if [ ! -e "${CERT}" ]; then
92 die "Could not find ${CERT}"
93 fi
94 elif [ -e "$(pwd)/self.pem" ]; then
95 CERT="$(pwd)/self.pem"
96 elif [ -e "${HERE}/../self.pem" ]; then
97 CERT="${HERE}/../self.pem"
98 elif [ -e "${HERE}/self.pem" ]; then
99 CERT="${HERE}/self.pem"
100 else
101 echo "Warning: could not find self.pem"
102 fi
103
104 echo "Starting webserver and WebSockets proxy on port ${PORT}"
105 ${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
106 proxy_pid="$!"
107 sleep 1
108 if ! ps -p ${proxy_pid} >/dev/null; then
109 proxy_pid=
110 echo "Failed to start WebSockets proxy"
111 exit 1
112 fi
113
114 echo -e "\n\nNavigate to this URL:\n"
115 echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
116 echo -e "Press Ctrl-C to exit\n\n"
117
118 wait ${proxy_pid}