]> git.proxmox.com Git - mirror_novnc.git/blob - utils/launch.sh
wsproxy.py: add web serving capability.
[mirror_novnc.git] / utils / launch.sh
1 #!/bin/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 a mini-webserver and the WebSockets proxy and"
11 echo "provides a cut and paste URL to go to."
12 echo
13 echo " --listen PORT Port for webserver/proxy 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 exit 2
20 }
21
22 NAME="$(basename $0)"
23 HERE="$(cd "$(dirname "$0")" && pwd)"
24 PORT="6080"
25 VNC_DEST="localhost:5900"
26 CERT=""
27 proxy_pid=""
28
29 die() {
30 echo "$*"
31 exit 1
32 }
33
34 cleanup() {
35 trap - TERM QUIT INT EXIT
36 trap "true" CHLD # Ignore cleanup messages
37 echo
38 if [ -n "${proxy_pid}" ]; then
39 echo "Terminating WebSockets proxy (${proxy_pid})"
40 kill ${proxy_pid}
41 fi
42 }
43
44 # Process Arguments
45
46 # Arguments that only apply to chrooter itself
47 while [ "$*" ]; do
48 param=$1; shift; OPTARG=$1
49 case $param in
50 --listen) PORT="${OPTARG}"; shift ;;
51 --vnc) VNC_DEST="${OPTARG}"; shift ;;
52 --cert) CERT="${OPTARG}"; shift ;;
53 -h|--help) usage ;;
54 -*) usage "Unknown chrooter option: ${param}" ;;
55 *) break ;;
56 esac
57 done
58
59 # Sanity checks
60 which netstat >/dev/null 2>&1 \
61 || die "Must have netstat installed"
62
63 netstat -ltn | grep -qs "${PORT}.*LISTEN" \
64 && die "Port ${PORT} in use. Try --listen PORT"
65
66 trap "cleanup" TERM QUIT INT EXIT
67
68 # Find vnc.html
69 if [ -e "$(pwd)/vnc.html" ]; then
70 WEB=$(pwd)
71 elif [ -e "${HERE}/../vnc.html" ]; then
72 WEB=${HERE}/../
73 elif [ -e "${HERE}/vnc.html" ]; then
74 WEB=${HERE}
75 else
76 die "Could not find vnc.html"
77 fi
78
79 # Find self.pem
80 if [ -n "${CERT}" ]; then
81 if [ ! -e "${CERT}" ]; then
82 die "Could not find ${CERT}"
83 fi
84 elif [ -e "$(pwd)/self.pem" ]; then
85 CERT="$(pwd)/self.pem"
86 elif [ -e "${HERE}/../self.pem" ]; then
87 CERT="${HERE}/../self.pem"
88 elif [ -e "${HERE}/self.pem" ]; then
89 CERT="${HERE}/self.pem"
90 else
91 echo "Warning: could not find self.pem"
92 fi
93
94 echo "Starting webserver and WebSockets proxy on port ${PORT}"
95 ${HERE}/wsproxy.py -f --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
96 proxy_pid="$!"
97 sleep 1
98 if ps -p ${proxy_pid} >/dev/null; then
99 echo "Started WebSockets proxy (pid: ${proxy_pid})"
100 else
101 proxy_pid=
102 echo "Failed to start WebSockets proxy"
103 exit 1
104 fi
105
106 echo -e "\n\nNavigate to to this URL:\n"
107 echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
108 echo -e "Press Ctrl-C to exit\n\n"
109
110 wait ${proxy_pid}