forked from rikkagcp1/uc3525
-
Notifications
You must be signed in to change notification settings - Fork 36
/
monitor.sh
executable file
·150 lines (116 loc) · 3.04 KB
/
monitor.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
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
#!/bin/bash
SUPERVISORCTL_CMD="supervisorctl ${SUPERVISORCTL_ARGS}"
CFD_REFRESH=${CFD_REFRESH:-'/app/cfd_refresh.sh'}
CFD_PROC_NAME=${CFD_PROC_NAME:-'cloudflared'}
VERBOSE_LEVEL=${VERBOSE_LEVEL:-1}
function get_field_from_header() {
local segments=($1)
for segment in "${segments[@]}"; do
if [[ "${segment%%:*}" == "$2" ]]; then
echo "${segment#*:}"
break
fi
done
}
function write_log() {
echo "$@" 1>&2
}
function log_0() {
write_log "0 $@"
}
function log_1() {
[[ $VERBOSE_LEVEL > 0 ]] && write_log "1 $@"
}
function log_2() {
[[ $VERBOSE_LEVEL > 1 ]] && write_log "2 $@"
}
declare -A CFD_STATES=(
[UNKNOWN]="Unknown State"
[LAUNCHED]="Cloudflared has been launched"
[CREATED]="Tunnel created"
[ADDRESSED]="Tunnel URL retrieved"
[EXPIRED]="Tunnel expired"
)
# A global variable that represent the current state
CFD_STATE=${CFD_STATES[UNKNOWN]}
function enter_cfd_state() {
if [[ $1 != $CFD_STATE ]]; then
CFD_STATE="$1"
log_0 "Entered State: \"$1\""
fi
}
function on_cfd_starting() {
enter_cfd_state "${CFD_STATES[LAUNCHED]}"
}
# Process each line of the stderr output
function on_cfd_stderr() {
local next_state=$CFD_STATE
case $CFD_STATE in
"${CFD_STATES[LAUNCHED]}")
if [[ $1 == *"Your quick Tunnel has been created!"* ]]; then
next_state=${CFD_STATES[CREATED]}
fi
;;
"${CFD_STATES[CREATED]}")
TUNNEL_URL=$(echo "$1" | grep -oE "https:\/\/.*[a-z]+.trycloudflare.com" | sed "s#https://##")
if [[ -n "$TUNNEL_URL" ]]; then
next_state=${CFD_STATES[ADDRESSED]}
log_0 "Tunnel URL -> \"$TUNNEL_URL\""
# Update webpage and subscription
$CFD_REFRESH "$TUNNEL_URL"
fi
;;
"${CFD_STATES[ADDRESSED]}")
if [[ $1 == *"Unregistered tunnel connection"* || $1 == *"Unauthorized: Failed to get tunnel"* ]]; then
next_state=${CFD_STATES[EXPIRED]}
# Tunnel expired or invalidated by Cloudflare for whatever reasons
$SUPERVISORCTL_CMD restart $CFD_PROC_NAME > /dev/null
log_0 "Replacing expired tunnel..."
fi
;;
"${CFD_STATES[EXPIRED]}")
;;
*)
log_1 "Warning: Entered unknown state!"
;;
esac
enter_cfd_state "$next_state"
}
log_0 "$SUPERVISORCTL_CMD"
while :
do
echo "READY"
read -r line
#echo $line 1>&2
EVENT_NAME=$(get_field_from_header "$line" "eventname")
BUF_LEN=$(get_field_from_header "$line" "len")
read -n $BUF_LEN -r line
#echo $line 1>&2
PROC=$(get_field_from_header "$line" "processname")
log_1 "[$PROC] $EVENT_NAME -> $BUF_LEN"
if [[ $PROC == $CFD_PROC_NAME ]]; then
case "$EVENT_NAME" in
"PROCESS_STATE_STARTING")
on_cfd_starting
;;
"PROCESS_LOG_STDERR")
char_to_be_read=$((BUF_LEN-${#line}-1))
log_2 "Contents -> $char_to_be_read"
while ((char_to_be_read > 0)) ; do
read -r line
log_2 "STDERR> $line"
char_to_be_read=$((char_to_be_read-${#line}-1))
on_cfd_stderr "$line"
done
;;
"PROCESS_STATE_FATAL")
Sleep 5
$SUPERVISORCTL_CMD restart $CFD_PROC_NAME > /dev/null
;;
*)
;;
esac
fi
echo -ne "RESULT 2\nOK"
log_1 "-----------------------------------------------------"
done