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