boringproxy/scripts/tunnel.sh

36 lines
928 B
Bash
Raw Permalink Normal View History

2020-10-06 20:00:40 -05:00
#!/bin/bash
server=$1
token=$2
domain=$3
localPort=$4
api="https://$server/api"
echo "Creating tunnel"
json=$(curl -s -H "Authorization: bearer $token" -X POST "$api/tunnels?domain=$domain")
serverAddress=$(echo "$json" | jq -r '.server_address')
serverPort=$(echo "$json" | jq -r '.server_port')
username=$(echo "$json" | jq -r '.username')
tunnelPort=$(echo "$json" | jq -r '.tunnel_port')
tunnelPrivateKey=$(echo "$json" | jq -r '.tunnel_private_key')
# TODO: It would be nice if we could avoid writing the private key to disk.
2020-10-12 19:22:08 -05:00
# I tried process substitution but it didn't work.
2020-10-06 20:00:40 -05:00
keyFile=$(mktemp)
chmod 0600 $keyFile
printf -- "$tunnelPrivateKey" > $keyFile
2020-10-06 20:00:40 -05:00
echo "Connecting to tunnel"
ssh -i $keyFile \
-NR 127.0.0.1:$tunnelPort:127.0.0.1:$localPort \
$username@$serverAddress -p $serverPort
echo "Cleaning up"
rm $keyFile
#curl -s -H "Authorization: bearer $token" -X DELETE "$api/tunnels?domain=$domain"