]> git.proxmox.com Git - mirror_novnc.git/blob - utils/launch.sh
feat: add French localization strings
[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] [--ssl-only]"
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 echo " --ssl-only Disable non-https connections."
26 echo " "
27 exit 2
28 }
29
30 NAME="$(basename $0)"
31 REAL_NAME="$(readlink -f $0)"
32 HERE="$(cd "$(dirname "$REAL_NAME")" && pwd)"
33 PORT="6080"
34 VNC_DEST="localhost:5900"
35 CERT=""
36 WEB=""
37 proxy_pid=""
38 SSLONLY=""
39
40 die() {
41 echo "$*"
42 exit 1
43 }
44
45 cleanup() {
46 trap - TERM QUIT INT EXIT
47 trap "true" CHLD # Ignore cleanup messages
48 echo
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
58 while [ "$*" ]; do
59 param=$1; shift; OPTARG=$1
60 case $param in
61 --listen) PORT="${OPTARG}"; shift ;;
62 --vnc) VNC_DEST="${OPTARG}"; shift ;;
63 --cert) CERT="${OPTARG}"; shift ;;
64 --web) WEB="${OPTARG}"; shift ;;
65 --ssl-only) SSLONLY="--ssl-only" ;;
66 -h|--help) usage ;;
67 -*) usage "Unknown chrooter option: ${param}" ;;
68 *) break ;;
69 esac
70 done
71
72 # Sanity checks
73 if bash -c "exec 7<>/dev/tcp/localhost/${PORT}" &> /dev/null; then
74 exec 7<&-
75 exec 7>&-
76 die "Port ${PORT} in use. Try --listen PORT"
77 else
78 exec 7<&-
79 exec 7>&-
80 fi
81
82 trap "cleanup" TERM QUIT INT EXIT
83
84 # Find vnc.html
85 if [ -n "${WEB}" ]; then
86 if [ ! -e "${WEB}/vnc.html" ]; then
87 die "Could not find ${WEB}/vnc.html"
88 fi
89 elif [ -e "$(pwd)/vnc.html" ]; then
90 WEB=$(pwd)
91 elif [ -e "${HERE}/../vnc.html" ]; then
92 WEB=${HERE}/../
93 elif [ -e "${HERE}/vnc.html" ]; then
94 WEB=${HERE}
95 elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
96 WEB=${HERE}/../share/novnc/
97 else
98 die "Could not find vnc.html"
99 fi
100
101 # Find self.pem
102 if [ -n "${CERT}" ]; then
103 if [ ! -e "${CERT}" ]; then
104 die "Could not find ${CERT}"
105 fi
106 elif [ -e "$(pwd)/self.pem" ]; then
107 CERT="$(pwd)/self.pem"
108 elif [ -e "${HERE}/../self.pem" ]; then
109 CERT="${HERE}/../self.pem"
110 elif [ -e "${HERE}/self.pem" ]; then
111 CERT="${HERE}/self.pem"
112 else
113 echo "Warning: could not find self.pem"
114 fi
115
116 # try to find websockify (prefer local, try global, then download local)
117 if [[ -e ${HERE}/websockify ]]; then
118 WEBSOCKIFY=${HERE}/websockify/run
119
120 if [[ ! -x $WEBSOCKIFY ]]; then
121 echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable."
122 echo "If you intended to use an installed websockify package, please remove ${HERE}/websockify."
123 exit 1
124 fi
125
126 echo "Using local websockify at $WEBSOCKIFY"
127 else
128 WEBSOCKIFY=$(which websockify 2>/dev/null)
129
130 if [[ $? -ne 0 ]]; then
131 echo "No installed websockify, attempting to clone websockify..."
132 WEBSOCKIFY=${HERE}/websockify/run
133 git clone https://github.com/novnc/websockify ${HERE}/websockify
134
135 if [[ ! -e $WEBSOCKIFY ]]; then
136 echo "Unable to locate ${HERE}/websockify/run after downloading"
137 exit 1
138 fi
139
140 echo "Using local websockify at $WEBSOCKIFY"
141 else
142 echo "Using installed websockify at $WEBSOCKIFY"
143 fi
144 fi
145
146 echo "Starting webserver and WebSockets proxy on port ${PORT}"
147 #${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
148 ${WEBSOCKIFY} ${SSLONLY} --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
149 proxy_pid="$!"
150 sleep 1
151 if ! ps -p ${proxy_pid} >/dev/null; then
152 proxy_pid=
153 echo "Failed to start WebSockets proxy"
154 exit 1
155 fi
156
157 echo -e "\n\nNavigate to this URL:\n"
158 if [ "x$SSLONLY" == "x" ]; then
159 echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
160 else
161 echo -e " https://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
162 fi
163
164 echo -e "Press Ctrl-C to exit\n\n"
165
166 wait ${proxy_pid}