launch.sh 4.4 KB

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