-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcakewalk
133 lines (110 loc) · 3.25 KB
/
cakewalk
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
set -e
CW_HOST=
CW_DATA=~/.cakeway
CW_KEYS=$CW_DATA/keys
CW_INITKEY=$CW_KEYS/init.key
CW_PORTS=$CW_DATA/ports
usage() {
echo $"Usage: $0 <new | connect | list | remove>"
exit 1
}
pre_cmd() {
echo -ne '\e[90m'
}
post_cmd() {
echo -e '\e[0m'
}
run_cmd() {
pre_cmd
$@
post_cmd
}
not_impl() {
echo "Not implemented yet!"
exit 1
}
keyfile() {
echo -n "$CW_KEYS/tun.$1.key"
}
portfile() {
echo -n "$CW_PORTS/$1"
}
get_resp() {
echo "$1" | grep -i $2 | awk -F': ' '{ print $2 }'
}
case $1 in
n|new)
TUNNEL=$2
TUNNEL_KEY=$(keyfile $TUNNEL)
mkdir -p "$CW_KEYS"
if [[ -z "$TUNNEL" ]]; then
echo $"Usage: $0 new <NAME>"
exit 1
else
echo -e "Creating new cakeway tunnel \e[36m$TUNNEL\e[0m"
echo "Generating key..."
pre_cmd
ssh-keygen -f $TUNNEL_KEY -b 2048 -t rsa-sha2-512 -C "cakewalker@$TUNNEL" -N ''
post_cmd
echo "Connecting to cakeway daemon..."
run_cmd ssh -T cakewalker@$CW_HOST -i $CW_INITKEY < $TUNNEL_KEY.pub
echo "Retrieving tunnel configuration..."
TUNRESP=$(ssh -T cakewalker@$CW_HOST -i $TUNNEL_KEY)
R_VERSION=$(get_resp "$TUNRESP" 'version')
R_PORT=$(get_resp "$TUNRESP" 'port')
R_TUNNEL=$(get_resp "$TUNRESP" 'tunnel')
echo -e "Version: \e[36m$R_VERSION\e[0m"
echo -e "Tunnel: \e[36m$R_TUNNEL\e[0m"
echo -e "Port: \e[36m$R_PORT\e[0m"
PORTFILE=$(portfile $TUNNEL)
mkdir -p $CW_PORTS
echo -n "$R_PORT" > $PORTFILE
echo ''
echo 'Done! Use "'$0 'connect' $TUNNEL '<LOCAL PORT>" to connect!'
fi
;;
c|open|connect)
TUNNEL=$2
TUNNEL_KEY=$(keyfile $TUNNEL)
PORTFILE=$(portfile $TUNNEL)
L_PORT=$3
if [[ -z "$TUNNEL" ]] || [[ -z "$L_PORT" ]]; then
echo $"Usage: $0 connect <NAME> <PORT>"
exit 1
elif [[ ! -f "$TUNNEL_KEY" ]]; then
echo -e "Invalid tunnel \e[36m$TUNNEL\e[0m (key does not exist)"
echo 1
elif [[ ! -f "$PORTFILE" ]]; then
echo -e "Invalid tunnel \e[36m$TUNNEL\e[0m (port entry does not exist)"
echo 1
else
R_PORT=$(cat $PORTFILE)
echo -e "Connecting cakeway tunnel \e[36m$TUNNEL\e[0m to local port \e[35m$L_PORT\e[0m... \nUse Ctrl-C to exit"
run_cmd ssh cakewalker@$CW_HOST -i $TUNNEL_KEY -N -R $R_PORT:localhost:$L_PORT
fi
;;
l|list)
echo "Available tunnels:"
for KF in $CW_KEYS/tun.*.pub; do
KEY=$(basename $KF .key.pub | sed 's/tun.//g')
echo " - $KEY"
done
;;
r|remove|delete)
TUNNEL=$2
TUNNEL_KEY=$(keyfile $TUNNEL)
if [[ -z "$TUNNEL" ]]; then
echo $"Usage: $0 remove <NAME>"
exit 1
elif [[ ! -f "$TUNNEL_KEY" ]]; then
echo -e "Invalid tunnel \e[36m$TUNNEL\e[0m (key does not exist)"
echo 1
else
rm $TUNNEL_KEY $TUNNEL_KEY.pub
echo -e "Tunnel \e[36m$TUNNEL\e[0m deleted"
fi
;;
*)
usage
esac