launch.sh 4.2 KB

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