]> git.proxmox.com Git - mirror_novnc.git/blob - utils/launch.sh
Merge pull request #474 from kanaka/bug/throw-error-from-constructor
[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 REAL_NAME="$(readlink -f $0)"
26 HERE="$(cd "$(dirname "$REAL_NAME")" && pwd)"
27 PORT="6080"
28 VNC_DEST="localhost:5900"
29 CERT=""
30 WEB=""
31 proxy_pid=""
32
33 die() {
34 echo "$*"
35 exit 1
36 }
37
38 cleanup() {
39 trap - TERM QUIT INT EXIT
40 trap "true" CHLD # Ignore cleanup messages
41 echo
42 if [ -n "${proxy_pid}" ]; then
43 echo "Terminating WebSockets proxy (${proxy_pid})"
44 kill ${proxy_pid}
45 fi
46 }
47
48 # Process Arguments
49
50 # Arguments that only apply to chrooter itself
51 while [ "$*" ]; do
52 param=$1; shift; OPTARG=$1
53 case $param in
54 --listen) PORT="${OPTARG}"; shift ;;
55 --vnc) VNC_DEST="${OPTARG}"; shift ;;
56 --cert) CERT="${OPTARG}"; shift ;;
57 --web) WEB="${OPTARG}"; shift ;;
58 -h|--help) usage ;;
59 -*) usage "Unknown chrooter option: ${param}" ;;
60 *) break ;;
61 esac
62 done
63
64 # Sanity checks
65 which netstat >/dev/null 2>&1 \
66 || die "Must have netstat installed"
67
68 netstat -ltn | grep -qs "${PORT} .*LISTEN" \
69 && die "Port ${PORT} in use. Try --listen PORT"
70
71 trap "cleanup" TERM QUIT INT EXIT
72
73 # Find vnc.html
74 if [ -n "${WEB}" ]; then
75 if [ ! -e "${WEB}/vnc.html" ]; then
76 die "Could not find ${WEB}/vnc.html"
77 fi
78 elif [ -e "$(pwd)/vnc.html" ]; then
79 WEB=$(pwd)
80 elif [ -e "${HERE}/../vnc.html" ]; then
81 WEB=${HERE}/../
82 elif [ -e "${HERE}/vnc.html" ]; then
83 WEB=${HERE}
84 elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
85 WEB=${HERE}/../share/novnc/
86 else
87 die "Could not find vnc.html"
88 fi
89
90 # Find self.pem
91 if [ -n "${CERT}" ]; then
92 if [ ! -e "${CERT}" ]; then
93 die "Could not find ${CERT}"
94 fi
95 elif [ -e "$(pwd)/self.pem" ]; then
96 CERT="$(pwd)/self.pem"
97 elif [ -e "${HERE}/../self.pem" ]; then
98 CERT="${HERE}/../self.pem"
99 elif [ -e "${HERE}/self.pem" ]; then
100 CERT="${HERE}/self.pem"
101 else
102 echo "Warning: could not find self.pem"
103 fi
104
105 # try to find websockify (prefer local, try global, then download local)
106 if [[ -e ${HERE}/websockify ]]; then
107 WEBSOCKIFY=${HERE}/websockify/run
108
109 if [[ ! -x $WEBSOCKIFY ]]; then
110 echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable."
111 echo "If you inteded to use an installed websockify package, please remove ${HERE}/websockify."
112 exit 1
113 fi
114
115 echo "Using local websockify at $WEBSOCKIFY"
116 else
117 WEBSOCKIFY=$(which websockify 2>/dev/null)
118
119 if [[ $? -ne 0 ]]; then
120 echo "No installed websockify, attempting to clone websockify..."
121 WEBSOCKIFY=${HERE}/websockify/run
122 git clone https://github.com/kanaka/websockify ${HERE}/websockify
123
124 if [[ ! -e $WEBSOCKIFY ]]; then
125 echo "Unable to locate ${HERE}/websockify/run after downloading"
126 exit 1
127 fi
128
129 echo "Using local websockify at $WEBSOCKIFY"
130 else
131 echo "Using installed websockify at $WEBSOCKIFY"
132 fi
133 fi
134
135 echo "Starting webserver and WebSockets proxy on port ${PORT}"
136 #${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
137 ${WEBSOCKIFY} --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
138 proxy_pid="$!"
139 sleep 1
140 if ! ps -p ${proxy_pid} >/dev/null; then
141 proxy_pid=
142 echo "Failed to start WebSockets proxy"
143 exit 1
144 fi
145
146 echo -e "\n\nNavigate to this URL:\n"
147 echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
148 echo -e "Press Ctrl-C to exit\n\n"
149
150 wait ${proxy_pid}