]> git.proxmox.com Git - mirror_novnc.git/blob - utils/launch.sh
Remove local copies of websockify
[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 # try to find websockify (prefer local, try global, then download local)
105 if [[ -e ${HERE}/websockify ]]; then
106 WEBSOCKIFY=${HERE}/websockify/run
107
108 if [[ ! -x $WEBSOCKIFY ]]; then
109 echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable."
110 echo "If you inteded to use an installed websockify package, please remove ${HERE}/websockify."
111 exit 1
112 fi
113
114 echo "Using local websockify at $WEBSOCKIFY"
115 else
116 WEBSOCKIFY=$(which websockify 2>/dev/null)
117
118 if [[ $? -ne 0 ]]; then
119 echo "No installed websockify, attempting to clone websockify..."
120 WEBSOCKIFY=${HERE}/websockify/run
121 git clone https://github.com/kanaka/websockify
122 git update-index --assume-unchanged 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}