]> git.proxmox.com Git - mirror_novnc.git/blame - utils/launch.sh
noVNC 1.0.0
[mirror_novnc.git] / utils / launch.sh
CommitLineData
e9155818 1#!/usr/bin/env bash
b89c6db3 2
15e733f5
JM
3# Copyright 2016 Joel Martin
4# Copyright 2016 Solly Ross
5# Licensed under MPL 2.0 or any later version (see LICENSE.txt)
6
b89c6db3
JM
7usage() {
8 if [ "$*" ]; then
9 echo "$*"
10 echo
11 fi
c77d9fcc 12 echo "Usage: ${NAME} [--listen PORT] [--vnc VNC_HOST:PORT] [--cert CERT] [--ssl-only]"
b89c6db3 13 echo
f2538f33
JM
14 echo "Starts the WebSockets proxy and a mini-webserver and "
15 echo "provides a cut-and-paste URL to go to."
6f514864 16 echo
f2538f33 17 echo " --listen PORT Port for proxy/webserver to listen on"
96bc3d30 18 echo " Default: 6080"
b89c6db3
JM
19 echo " --vnc VNC_HOST:PORT VNC server host:port proxy target"
20 echo " Default: localhost:5900"
96bc3d30
JM
21 echo " --cert CERT Path to combined cert/key file"
22 echo " Default: self.pem"
3516bdf3
JM
23 echo " --web WEB Path to web files (e.g. vnc.html)"
24 echo " Default: ./"
c77d9fcc
WF
25 echo " --ssl-only Disable non-https connections."
26 echo " "
b89c6db3
JM
27 exit 2
28}
29
30NAME="$(basename $0)"
2ace90e6
SR
31REAL_NAME="$(readlink -f $0)"
32HERE="$(cd "$(dirname "$REAL_NAME")" && pwd)"
96bc3d30 33PORT="6080"
b89c6db3 34VNC_DEST="localhost:5900"
96bc3d30 35CERT=""
3516bdf3 36WEB=""
b89c6db3 37proxy_pid=""
c77d9fcc 38SSLONLY=""
b89c6db3
JM
39
40die() {
41 echo "$*"
42 exit 1
43}
44
45cleanup() {
46 trap - TERM QUIT INT EXIT
47 trap "true" CHLD # Ignore cleanup messages
48 echo
b89c6db3
JM
49 if [ -n "${proxy_pid}" ]; then
50 echo "Terminating WebSockets proxy (${proxy_pid})"
51 kill ${proxy_pid}
52 fi
53}
54
55# Process Arguments
56
57# Arguments that only apply to chrooter itself
58while [ "$*" ]; do
59 param=$1; shift; OPTARG=$1
60 case $param in
96bc3d30
JM
61 --listen) PORT="${OPTARG}"; shift ;;
62 --vnc) VNC_DEST="${OPTARG}"; shift ;;
63 --cert) CERT="${OPTARG}"; shift ;;
3516bdf3 64 --web) WEB="${OPTARG}"; shift ;;
c77d9fcc 65 --ssl-only) SSLONLY="--ssl-only" ;;
96bc3d30 66 -h|--help) usage ;;
b89c6db3 67 -*) usage "Unknown chrooter option: ${param}" ;;
96bc3d30 68 *) break ;;
b89c6db3
JM
69 esac
70done
71
72# Sanity checks
73which netstat >/dev/null 2>&1 \
74 || die "Must have netstat installed"
75
83d3e02f 76netstat -ltn | grep -qs ":${PORT} .*LISTEN" \
96bc3d30 77 && die "Port ${PORT} in use. Try --listen PORT"
b89c6db3
JM
78
79trap "cleanup" TERM QUIT INT EXIT
80
0f7f146f 81# Find vnc.html
3516bdf3
JM
82if [ -n "${WEB}" ]; then
83 if [ ! -e "${WEB}/vnc.html" ]; then
84 die "Could not find ${WEB}/vnc.html"
85 fi
86elif [ -e "$(pwd)/vnc.html" ]; then
96bc3d30 87 WEB=$(pwd)
0f7f146f 88elif [ -e "${HERE}/../vnc.html" ]; then
96bc3d30 89 WEB=${HERE}/../
0f7f146f 90elif [ -e "${HERE}/vnc.html" ]; then
96bc3d30 91 WEB=${HERE}
3516bdf3
JM
92elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
93 WEB=${HERE}/../share/novnc/
0f7f146f
JM
94else
95 die "Could not find vnc.html"
96fi
0f7f146f 97
96bc3d30
JM
98# Find self.pem
99if [ -n "${CERT}" ]; then
100 if [ ! -e "${CERT}" ]; then
101 die "Could not find ${CERT}"
102 fi
103elif [ -e "$(pwd)/self.pem" ]; then
104 CERT="$(pwd)/self.pem"
105elif [ -e "${HERE}/../self.pem" ]; then
106 CERT="${HERE}/../self.pem"
107elif [ -e "${HERE}/self.pem" ]; then
108 CERT="${HERE}/self.pem"
b89c6db3 109else
96bc3d30 110 echo "Warning: could not find self.pem"
b89c6db3
JM
111fi
112
6f514864
SR
113# try to find websockify (prefer local, try global, then download local)
114if [[ -e ${HERE}/websockify ]]; then
115 WEBSOCKIFY=${HERE}/websockify/run
116
117 if [[ ! -x $WEBSOCKIFY ]]; then
118 echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable."
54e835ee 119 echo "If you intended to use an installed websockify package, please remove ${HERE}/websockify."
6f514864
SR
120 exit 1
121 fi
122
123 echo "Using local websockify at $WEBSOCKIFY"
124else
125 WEBSOCKIFY=$(which websockify 2>/dev/null)
126
127 if [[ $? -ne 0 ]]; then
128 echo "No installed websockify, attempting to clone websockify..."
129 WEBSOCKIFY=${HERE}/websockify/run
101ff127 130 git clone https://github.com/novnc/websockify ${HERE}/websockify
6f514864
SR
131
132 if [[ ! -e $WEBSOCKIFY ]]; then
133 echo "Unable to locate ${HERE}/websockify/run after downloading"
134 exit 1
135 fi
136
137 echo "Using local websockify at $WEBSOCKIFY"
138 else
139 echo "Using installed websockify at $WEBSOCKIFY"
140 fi
141fi
142
96bc3d30 143echo "Starting webserver and WebSockets proxy on port ${PORT}"
6f514864 144#${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
c77d9fcc 145${WEBSOCKIFY} ${SSLONLY} --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
b89c6db3
JM
146proxy_pid="$!"
147sleep 1
f2538f33 148if ! ps -p ${proxy_pid} >/dev/null; then
b89c6db3
JM
149 proxy_pid=
150 echo "Failed to start WebSockets proxy"
151 exit 1
152fi
153
19ed81fd 154echo -e "\n\nNavigate to this URL:\n"
27a1f6cb
WF
155if [ "x$SSLONLY" == "x" ]; then
156 echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
157else
158 echo -e " https://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
159fi
160
b89c6db3
JM
161echo -e "Press Ctrl-C to exit\n\n"
162
96bc3d30 163wait ${proxy_pid}