-
Notifications
You must be signed in to change notification settings - Fork 0
/
mktun.sh
executable file
·95 lines (71 loc) · 1.92 KB
/
mktun.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
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
set -e
CONFIG_DIR="$(dirname $0)"
# These configurations can be overriden in tun.conf
# or via environment
KEEPALIVE="${KEEPALIVE:-900}" # 15 minutes
ABORT_AFTER="${ABORT_AFTER:-60}"
MONITOR_LOG="${MONITOR_LOG:-/tmp/ngrok.log}"
function now {
/bin/date '+%Y-%m-%d %H:%M:%S %Z'
}
function info {
echo "[$(now)] $@"
}
function die {
info $@ >&2
exit 1
}
function tailpid {
ps -eopid,command | grep "[t]ail -f $MONITOR_LOG" | awk '{print $1}'
}
function notify {
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$(now) - $@\"}" "$NOTIFY_URL"
}
function cleanup {
if [ -n "$ngrok_pid" ]; then
if (ps -p $ngrok_pid -opid,command | grep -q ngrok); then
info "Bringing down ngrok at pid $ngrok_pid"
kill -KILL $ngrok_pid
notify "Killed tunnel."
fi
fi
if [ -n "$(tailpid)" ]; then
info "Bringing down tail"
kill -KILL $(tailpid)
fi
}
trap cleanup EXIT
if [ -r "$CONFIG_DIR/tun.conf" ]; then # read config
source $CONFIG_DIR/tun.conf
fi
if [ -z "$NOTIFY_URL" ]; then
die "NOTIFY_URL must be set!"
fi
if (which gtimeout &> /dev/null); then
export TIMEOUT_EXEC=gtimeout
else
if !(which timeout &> /dev/null || which gtimeout &> /dev/null); then
die "This script requires GNU timeout"
fi
export TIMEOUT_EXEC=timeout
fi
if (ps -eopid,command | grep -q '[n]grok'); then
info "ngrok is already running, exiting."
exit 0
fi
&> $MONITOR_LOG ngrok tcp --log stdout 22 &
ngrok_pid=$!
info "ngrok pid is $ngrok_pid"
$TIMEOUT_EXEC $ABORT_AFTER tail -f $MONITOR_LOG | while read LINE; do # give 30 sec to establish a successful connection
if [[ "${LINE}" == *"client session established"* ]]; then
info "Tunnel connected"
notify "Tunnel established!"
sleep $KEEPALIVE
info "Disconnecting tunnel after $KEEPALIVE s"
kill -KILL $(tailpid)
exit 0
fi
done
notify "Failed to establish tunnel"
die "Could not establish ngrok connection"