#!/bin/bash
if [[ ! $(ipfs p2p ls | grep x/ssh-12D3KooWHJnRBx1KxKDPFnnzzrXhk74bPtb5FPMjUwaxCBN8Wbs3) ]]; then
    ipfs --timeout=10s ping -n 4 /p2p/12D3KooWHJnRBx1KxKDPFnnzzrXhk74bPtb5FPMjUwaxCBN8Wbs3
    [[ $? == 0 ]] \
        && ipfs p2p forward /x/ssh-12D3KooWHJnRBx1KxKDPFnnzzrXhk74bPtb5FPMjUwaxCBN8Wbs3 /ip4/127.0.0.1/tcp/22210 /p2p/12D3KooWHJnRBx1KxKDPFnnzzrXhk74bPtb5FPMjUwaxCBN8Wbs3 \
        && echo "ssh fred@127.0.0.1 -p 22210" \
        || echo "CONTACT IPFSNODEID FAILED - ERROR -"
else
    echo "Tunnel /x/ssh 22210 already active..."
    echo "ssh fred@127.0.0.1 -p 22210"
    echo "ipfs p2p close -p /x/ssh-12D3KooWHJnRBx1KxKDPFnnzzrXhk74bPtb5FPMjUwaxCBN8Wbs3"
fi

