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

