launch.sh 3.13 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5 6 7

usage() {
    if [ "$*" ]; then
        echo "$*"
        echo
    fi
8
    echo "Usage: ${NAME} [--listen PORT] [--vnc VNC_HOST:PORT] [--cert CERT]"
9
    echo
10 11
    echo "Starts the WebSockets proxy and a mini-webserver and "
    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 16
    echo "    --vnc VNC_HOST:PORT   VNC server host:port proxy target"
    echo "                          Default: localhost:5900"
17 18
    echo "    --cert CERT           Path to combined cert/key file"
    echo "                          Default: self.pem"
19 20
    echo "    --web WEB             Path to web files (e.g. vnc.html)"
    echo "                          Default: ./"
21 22 23 24
    exit 2
}

NAME="$(basename $0)"
25
HERE="$(cd "$(dirname "$0")" && pwd)"
26
PORT="6080"
27
VNC_DEST="localhost:5900"
28
CERT=""
29
WEB=""
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
proxy_pid=""

die() {
    echo "$*"
    exit 1
}

cleanup() {
    trap - TERM QUIT INT EXIT
    trap "true" CHLD   # Ignore cleanup messages
    echo
    if [ -n "${proxy_pid}" ]; then
        echo "Terminating WebSockets proxy (${proxy_pid})"
        kill ${proxy_pid}
    fi
}

# Process Arguments

# Arguments that only apply to chrooter itself
while [ "$*" ]; do
    param=$1; shift; OPTARG=$1
    case $param in
53 54 55
    --listen)  PORT="${OPTARG}"; shift            ;;
    --vnc)     VNC_DEST="${OPTARG}"; shift        ;;
    --cert)    CERT="${OPTARG}"; shift            ;;
56
    --web)     WEB="${OPTARG}"; shift            ;;
57
    -h|--help) usage                              ;;
58
    -*) usage "Unknown chrooter option: ${param}" ;;
59
    *) break                                      ;;
60 61 62 63 64 65 66
    esac
done

# Sanity checks
which netstat >/dev/null 2>&1 \
    || die "Must have netstat installed"

67
netstat -ltn | grep -qs "${PORT} .*LISTEN" \
68
    && die "Port ${PORT} in use. Try --listen PORT"
69 70 71

trap "cleanup" TERM QUIT INT EXIT

72
# Find vnc.html
73 74 75 76 77
if [ -n "${WEB}" ]; then
    if [ ! -e "${WEB}/vnc.html" ]; then
        die "Could not find ${WEB}/vnc.html"
    fi
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 84
elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
    WEB=${HERE}/../share/novnc/
85 86 87 88
else
    die "Could not find vnc.html"
fi

89 90 91 92 93 94 95 96 97 98 99
# Find self.pem
if [ -n "${CERT}" ]; then
    if [ ! -e "${CERT}" ]; then
        die "Could not find ${CERT}"
    fi
elif [ -e "$(pwd)/self.pem" ]; then
    CERT="$(pwd)/self.pem"
elif [ -e "${HERE}/../self.pem" ]; then
    CERT="${HERE}/../self.pem"
elif [ -e "${HERE}/self.pem" ]; then
    CERT="${HERE}/self.pem"
100
else
101
    echo "Warning: could not find self.pem"
102 103
fi

104
echo "Starting webserver and WebSockets proxy on port ${PORT}"
105
${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
106 107
proxy_pid="$!"
sleep 1
108
if ! ps -p ${proxy_pid} >/dev/null; then
109 110 111 112 113
    proxy_pid=
    echo "Failed to start WebSockets proxy"
    exit 1
fi

Sam Mussmann's avatar
Sam Mussmann committed
114
echo -e "\n\nNavigate to this URL:\n"
115
echo -e "    http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
116 117
echo -e "Press Ctrl-C to exit\n\n"

118
wait ${proxy_pid}