This commit is contained in:
Kris Lamoureux 2023-07-23 03:34:27 -04:00
parent ef5aacdbbd
commit 94b47f2477

41
run-proxy.sh Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
# Find private key file
PRIVATE_KEY="$(find .vagrant -name "private_key")"
# Does the private_key file exist?
if [ ! -f "$PRIVATE_KEY" ]; then
echo "[ERROR] File not found at \"$PRIVATE_KEY\""
exit 1
fi
# Is the private_key a valid SSH key?
echo "Checking validity of private key at $(pwd)/$PRIVATE_KEY"
if ! ssh-keygen -l -f "$PRIVATE_KEY"; then
echo "[Error] The private key at \"$PRIVATE_KEY\" is invalid (CODE: $?)"
exit 1
fi
# Find an IP on the VM for the SSH tunnel
HOST_IP="$(vagrant ssh -c "hostname -I | cut -d' ' -f${HOSTNAME_FIELD:-1}" 2>/dev/null | sed 's/.$//')"
CMD="ssh -fNT -i $PRIVATE_KEY -L 8443:localhost:8443 -L 80:localhost:80 -L 443:localhost:443.*vagrant@$HOST_IP"
PS_TUNNELS="$(ps aux | grep -e "$CMD" | grep -v grep)"
if [ "$(echo "$PS_TUNNELS" | wc -l)" -gt 0 ]; then
echo "[ERROR] Tunnel already seems to exist"
echo "$PS_TUNNELS"
exit 1
fi
# Create an SSH tunnel
echo "Starting background SSH connection for localhost port forwarding"
set -x
ssh -fNT -i "$PRIVATE_KEY" \
-L 8443:localhost:8443 \
-L 80:localhost:80 \
-L 443:localhost:443 \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
vagrant@"${HOST_IP}" 2>/dev/null