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