-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtunn
executable file
·180 lines (155 loc) · 3.15 KB
/
tunn
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/bash
# helper functions
tunn_check_op() {
if [ "$OP" != "$1" ]; then
usage 1
fi
}
tunn_read() {
# get list of tunnels
readarray -t TLIST < "$CONFIG"
export TLIST
}
tunn_entry() {
ENTRY="${TLIST[$1]}"
IFS=' ' read -r -a ENTRY_ARR <<< "$ENTRY"
# reconstruct command based on output format
export SOCKETNAME="${ENTRY_ARR[0]}"
export PORT="${ENTRY_ARR[1]}"
# gets last N-2 words
export CMD="${ENTRY_ARR[*]: -$((${#ENTRY_ARR[@]}-2))}"
}
# operations
tunn_make() {
CMD="$@"
if [ -z "$CMD" ]; then
usage 1
fi
# generate a random name for socket
SOCKETNUM=$(uuidgen)
SOCKETNAME="$NAMEPRE"_"$SOCKETNUM"
# execute tunnel command
eval $CMD -N -D $PORT -f -M -S "$SOCKETNAME" 2>/dev/null
TEXIT=$?
# add to list of tunnels
if [ $TEXIT -eq 0 ]; then
echo "$SOCKETNAME $PORT $CMD" >> "$CONFIG"
else
echo "Could not create tunnel (exit code $TEXIT)"
exit $TEXIT
fi
}
tunn_list() {
if [ -n "$1" ]; then
usage 1
fi
if [ -n "$UPDATE" ]; then
tunn_read
# clear list of tunnels
: > "$CONFIG"
# write open tunnels back into list
for ((i=0; i < ${#TLIST[@]}; i++)); do
tunn_entry $i
if eval $CMD -S "$SOCKETNAME" -O check >&/dev/null; then
echo "${TLIST[$i]}" >> "$CONFIG"
else
rm "$SOCKETNAME" >&/dev/null
fi
done
unset TLIST
fi
tunn_read
if [ ${#TLIST[@]} -eq 0 ]; then
return
fi
echo "index: socket port command"
for ((i=0; i < ${#TLIST[@]}; i++)); do
echo "$i: ${TLIST[$i]}"
done
}
tunn_kill() {
INDEX="$1"
if [ -z "$INDEX" ]; then
usage 1
fi
# get this tunnel
tunn_read
if [ $INDEX -lt ${#TLIST[@]} ]; then
tunn_entry $INDEX
else
echo "Tunnel index $INDEX not found"
exit 2
fi
# execute kill command
eval $CMD -S "$SOCKETNAME" -O exit
# update list
sed -i "$((INDEX+1))"d "$CONFIG"
}
# defaults
CONFIG=~/.tunnlist
USE_ALIASES=true
NAMEPRE=~/tsock
PORT=8864
SOCKETNAME=""
CMD=""
INDEX=""
UPDATE=""
usage() {
ECHO="echo -e"
$ECHO "tunn [operation] [options] [arguments]"
$ECHO
$ECHO "Operations:"
$ECHO
$ECHO "make \t make new tunnel"
$ECHO "\t-n [name] \t tunnel socket name prefix (default: ${NAMEPRE})"
$ECHO "\t-p [port] \t tunnel port (default: $PORT)"
$ECHO "\t[command] \t ssh command to run when making tunnel (required)"
$ECHO
$ECHO "list \t list open tunnels"
$ECHO "\t-u \t update list of tunnels (check status and remove closed ones)"
$ECHO
$ECHO "kill \t kill specified tunnel"
$ECHO "\t[index] \t index of tunnel (required)"
$ECHO
$ECHO "Common options:"
$ECHO "-A \t disable loading of aliases"
$ECHO "-h \t print this message and exit"
exit $1
}
# get operation
OP=$1
shift 1
OPFN=""
case "$OP" in
make) OPFN=tunn_make
;;
list) OPFN=tunn_list
;;
kill) OPFN=tunn_kill
;;
*) usage 1
;;
esac
while getopts "An:p:uh" opt; do
case "$opt" in
A) USE_ALIASES=""
;;
n) tunn_check_op make; NAMEPRE="$OPTARG"
;;
p) tunn_check_op make; PORT="$OPTARG"
;;
u) tunn_check_op list; UPDATE=true
;;
h) usage 0
;;
esac
done
# get ssh aliases
if [ -n "$USE_ALIASES" ]; then
shopt -s expand_aliases
source ~/.bashrc
fi
# get args for operation (if any)
shift $(($OPTIND - 1))
# execute operation
$OPFN "$@"