summaryrefslogtreecommitdiff
path: root/scripts/tokssh
blob: eeddb2187539988956461e1b87e8673970c16ad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
set -e
function help {
    cat <<EOF
A simple wrapper to use like SSH

Usage:
    tokssh [ssh options] [user@]address [-s secret]

where

    ssh options:   options to pass to ssh process
    user:          login on remote host
    address:       either a ToxID or a hostname. ~/.tuntox/hosts is read to map
                hostname to ToxID. hostname MUST resolve to 127.0.0.1

    -s             optional secret to use to connect to tuntox server

examples:
    tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054
    tokssh 5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054
    tokssh -p 2222 -o ForwardAgent=yes user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054
    tokssh user@5A40C3443ABD6E1DDEE682E83F84A4D556C24C22D2230DCC141A4723C123473C171A4D9C4054 -s TuNToXSeCreT

files:
    ~/.tuntox/hosts maps hostname to ToxID and optional secret.
    format is
    hostname    ToxID   secret(optional)
EOF
}

strargs="'$*'"
if [ -z "${strargs##*-h*}" ] || [ -z "${strargs##*--help*}" ] ;then
    help
    exit
fi

array=( $@ )
len=${#array[@]}

if [ $len -lt 1 ]; then
    help
    exit
fi


# look for secret and remvove it from args
if [ $len -gt 2 ] && [ "${array[$len-2]}" == "-s" ]
then
    secret="${array[@]:$len-2:$len-1}"
    len=$[len-2]
fi

userhost=${array[$len-1]}
args=${array[@]:0:$len-1}

# check for user@id
arruserhost=(${userhost//@/ })
arruserhostlen=${#arruserhost[@]}

if [ $arruserhostlen -gt 1 ]
then
    # last argument is user@toxid
    user="${arruserhost[0]}@"
    toxid=${arruserhost[1]}
    hostname=localhost
else
    # last argument is just toxid
    user=""
    toxid=$userhost
    hostname=localhost
fi

#search toxid in ~/.tuntox/hosts and map it to toxid
if [ -f ~/.tuntox/hosts ]; then
    while read c_hostname c_toxid c_secret; do
        if [ "${c_hostname:0:1}" != "#" ] && [ "$c_hostname" == "$toxid" ]; then
            toxid="$c_toxid"
            if [ "$secret" == "" ]; then
                secret="-s $c_secret"
            fi
            break
        fi
    done < ~/.tuntox/hosts
fi

ssh -o ProxyCommand="tuntox -i $toxid -W 127.0.0.1:%p $secret" $args ${user}${hostname}