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

