qvm-connect-tcp 972 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash --
  2. set -e
  3. [[ "$DEBUG" == "1" ]] && set -x
  4. LOCALPORT="$(echo "$1" | cut -d ':' -f1)"
  5. DOMAIN="$(echo "$1" | cut -d ':' -f2)"
  6. PORT="$(echo "$1" | cut -d ':' -f3)"
  7. print_usage() {
  8. cat >&2 <<USAGE
  9. Usage: $0 [localport]:[vmname]:[port]
  10. Bind localport to another VM port using the qubes.ConnectTCP RPC service.
  11. USAGE
  12. }
  13. check_port() {
  14. [[ "$1" -ge 1 ]] && [[ "$1" -le 65535 ]]
  15. }
  16. if [ $# -lt 1 ] ; then
  17. print_usage
  18. exit 1
  19. fi
  20. [[ -z "$PORT" ]] && { echo "Please provide PORT"; exit 1; };
  21. [[ -z "$LOCALPORT" ]] && LOCALPORT="$PORT"
  22. if check_port "$PORT" && check_port "$LOCALPORT"; then
  23. if [ -n "$DOMAIN" ]; then
  24. msg="Binding TCP '$DOMAIN:$PORT' to 'localhost:$LOCALPORT'..."
  25. else
  26. msg="Binding TCP '@default:$PORT' to 'localhost:$LOCALPORT'..."
  27. fi
  28. echo "$msg"
  29. sudo socat TCP-LISTEN:"$LOCALPORT",reuseaddr,fork EXEC:"qrexec-client-vm \'$DOMAIN\' qubes.ConnectTCP+$PORT" &
  30. else
  31. echo "Invalid port provided"
  32. exit 1
  33. fi