launch.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. REAL_NAME="$(readlink -f $0)"
  24. HERE="$(cd "$(dirname "$REAL_NAME")" && pwd)"
  25. PORT="6080"
  26. VNC_DEST="localhost:5900"
  27. CERT=""
  28. WEB=""
  29. proxy_pid=""
  30. die() {
  31. echo "$*"
  32. exit 1
  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. # Process Arguments
  44. # Arguments that only apply to chrooter itself
  45. while [ "$*" ]; do
  46. param=$1; shift; OPTARG=$1
  47. case $param in
  48. --listen) PORT="${OPTARG}"; shift ;;
  49. --vnc) VNC_DEST="${OPTARG}"; shift ;;
  50. --cert) CERT="${OPTARG}"; shift ;;
  51. --web) WEB="${OPTARG}"; shift ;;
  52. -h|--help) usage ;;
  53. -*) usage "Unknown chrooter option: ${param}" ;;
  54. *) break ;;
  55. esac
  56. done
  57. # Sanity checks
  58. which netstat >/dev/null 2>&1 \
  59. || die "Must have netstat installed"
  60. netstat -ltn | grep -qs "${PORT} .*LISTEN" \
  61. && die "Port ${PORT} in use. Try --listen PORT"
  62. trap "cleanup" TERM QUIT INT EXIT
  63. # Find vnc.html
  64. if [ -n "${WEB}" ]; then
  65. if [ ! -e "${WEB}/vnc.html" ]; then
  66. die "Could not find ${WEB}/vnc.html"
  67. fi
  68. elif [ -e "$(pwd)/vnc.html" ]; then
  69. WEB=$(pwd)
  70. elif [ -e "${HERE}/../vnc.html" ]; then
  71. WEB=${HERE}/../
  72. elif [ -e "${HERE}/vnc.html" ]; then
  73. WEB=${HERE}
  74. elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then
  75. WEB=${HERE}/../share/novnc/
  76. else
  77. die "Could not find vnc.html"
  78. fi
  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. # try to find websockify (prefer local, try global, then download local)
  94. if [[ -e ${HERE}/websockify ]]; then
  95. WEBSOCKIFY=${HERE}/websockify/run
  96. if [[ ! -x $WEBSOCKIFY ]]; then
  97. echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable."
  98. echo "If you inteded to use an installed websockify package, please remove ${HERE}/websockify."
  99. exit 1
  100. fi
  101. echo "Using local websockify at $WEBSOCKIFY"
  102. else
  103. WEBSOCKIFY=$(which websockify 2>/dev/null)
  104. if [[ $? -ne 0 ]]; then
  105. echo "No installed websockify, attempting to clone websockify..."
  106. WEBSOCKIFY=${HERE}/websockify/run
  107. git clone https://github.com/kanaka/websockify ${HERE}/websockify
  108. if [[ ! -e $WEBSOCKIFY ]]; then
  109. echo "Unable to locate ${HERE}/websockify/run after downloading"
  110. exit 1
  111. fi
  112. echo "Using local websockify at $WEBSOCKIFY"
  113. else
  114. echo "Using installed websockify at $WEBSOCKIFY"
  115. fi
  116. fi
  117. echo "Starting webserver and WebSockets proxy on port ${PORT}"
  118. #${HERE}/websockify --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
  119. ${WEBSOCKIFY} --web ${WEB} ${CERT:+--cert ${CERT}} ${PORT} ${VNC_DEST} &
  120. proxy_pid="$!"
  121. sleep 1
  122. if ! ps -p ${proxy_pid} >/dev/null; then
  123. proxy_pid=
  124. echo "Failed to start WebSockets proxy"
  125. exit 1
  126. fi
  127. echo -e "\n\nNavigate to this URL:\n"
  128. echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n"
  129. echo -e "Press Ctrl-C to exit\n\n"
  130. wait ${proxy_pid}