-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcakewalker.sh
executable file
·85 lines (67 loc) · 1.99 KB
/
cakewalker.sh
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
#!/bin/bash
set -e
cd "${BASH_SOURCE%/*}" || exit
source ./cakeway_env.sh
echo "Version: Cakewalker v$CW_VERSION"
if [[ "$1" -ne "-c" ]]; then
echo "Error: No command supplied"
exit 1
fi
COMMAND=$2
if [[ "$COMMAND" == cakeway* ]] || [[ "$COMMAND" == cakewalker* ]]; then
CPARTS=($COMMAND)
COMMAND=${CPARTS[@]:1}
fi
update_auth_keys() {
AUTH_KEYS="$CW_WALKERSSH/authorized_keys"
TUNNELS=($(ls -c ~/keys/))
TUNCOUNT=${#TUNNELS[@]}
# Write init key
INITOPTS='no-X11-forwarding,no-agent-forwarding,command="new-tunnel"'
echo $INITOPTS $(cat $CW_INITKEY.pub) > $AUTH_KEYS
# Write tunnel keys
for i in "${!TUNNELS[@]}"; do
# echo "$i => ${TUNNELS[$i]}";
TUNINDEX=$((TUNCOUNT - i))
TUNNEL=${TUNNELS[$i]}
PORT=$((TUNINDEX + CW_PORT_MIN))
PUBKEY=$(ssh-keygen -i -f ~/keys/$TUNNEL)
TUNENV='environment="CW_PORT='$PORT'",environment="CW_TUNNEL='$TUNNEL'"'
TUNOPTS='no-X11-forwarding,no-agent-forwarding,permitlisten="localhost:'$PORT'",command="tunnel",'$TUNENV
echo $TUNOPTS $PUBKEY >> $AUTH_KEYS
done
}
case "$COMMAND" in
new-tunnel)
echo -n 'Key? '
read PUBKEY
echo 'OK'
mkdir -p ~/tmp
TEMPKEYFILE=$(mktemp ~/tmp/newkey_XXXXXX)
echo $PUBKEY > $TEMPKEYFILE
mkdir -p ~/keys
KEYFILE=~/keys/$(ssh-keygen -l -f "$TEMPKEYFILE" | awk '{ print $3 }' | sed "s/'//g") # | awk -F@ '{ print $1}')
if [[ -f "$KEYFILE" ]]; then
echo 'Error: Keyfile already exists'
exit 1
fi
ssh-keygen -e -f "$TEMPKEYFILE" > $KEYFILE
rm $TEMPKEYFILE
update_auth_keys
echo 'OK'
exit 0
;;
tunnel)
echo 'Port:' $CW_PORT
echo 'Tunnel:' $CW_TUNNEL
exit 0
;;
update-auth-keys)
update_auth_keys
exit 0
;;
*)
echo "Error: Unknown command '$2'"
exit 1
;;
esac