-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjd2opreconnect.sh
executable file
·137 lines (116 loc) · 4.22 KB
/
jd2opreconnect.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
#!/usr/bin/env /bin/bash
## BASIC CONFIGURATIONS
COUNTRY=sg
OPENPYN_SERVICE=openpyn.service
OPENPYN_BINARY=openpyn
SLEEP_AFTER_RECONNECTING=5s
SLEEP_RETRY=5
## FORMATTING
S_BLACK=$(tput setaf 0)
S_RED=$(tput setaf 1)
S_GREEN=$(tput setaf 2)
S_YELLOW=$(tput setaf 3)
S_BLUE=$(tput setaf 4)
S_MAGENTA=$(tput setaf 5)
S_CYAN=$(tput setaf 6)
S_WHITE=$(tput setaf 7)
S_BOLD=$(tput bold)
S_DIM=$(tput dim)
S_RESET=$(tput sgr0)
## FUNCTIONS
function info {
echo "${S_BOLD}${S_WHITE}${S_DIM}[INFO]${S_RESET} $1"
}
function warn {
echo "${S_BOLD}${S_WHITE}${S_DIM}[WARNING]${S_RESET} ${S_YELLOW}$1${S_RESET}"
}
function err {
echo "${S_BOLD}${S_WHITE}${S_DIM}[ERROR]${S_RESET} ${S_BOLD}${S_RED}$1${S_RESET}"
}
function pass_service_status_to_var {
eval "${1}=\"$(systemctl show ${OPENPYN_SERVICE} --no-page)\""
}
function parse_is_service_active {
SERVICE_ACTIVE_STATE=$(printf '%s' "${1}" | sed -E -n "s/^ActiveState=(.+)$/\1/p")
[[ "$SERVICE_ACTIVE_STATE" == "active" ]] && return
false
}
function echo_and_sleep {
info "Sleeping for ${S_RED}${S_BOLD}${1}${S_RESET}..."
sleep ${1}
}
## MAIN SCRIPT
if [ "$EUID" -ne 0 ]; then
err "Please run this script as root (or add this script to sudoers file)"
exit 1
fi
if [[ -z "$COUNTRY" ]]; then
err "Country must be set, exiting..."
exit 1
else
info "Country set to ${S_RED}${S_BOLD}${COUNTRY^^}${S_RESET}, fetching servers list..."
SAVEIFS=$IFS
IFS=$'\n'
SERVERS=($(${OPENPYN_BINARY} -l ${COUNTRY} | sed -E -n "s/^server = ([a-zA-Z0-9]+).*/\1/ip"))
IFS=$SAVEIFS
SERVERS_STRING=$(printf ", %s" "${SERVERS[@]}")
SERVERS_STRING=${SERVERS_STRING:1}
SERVERS_COUNT=${#SERVERS[@]}
fi
info "${S_RED}${S_BOLD}${COUNTRY^^}${S_RESET} servers (${SERVERS_COUNT}):${S_BOLD}${S_GREEN}${SERVERS_STRING}${S_RESET}"
if [[ $SERVERS_COUNT -lt 2 ]]; then
err "There are less than 2 servers in ${COUNTRY}, please consider using another country..."
exit 1
fi
pass_service_status_to_var SERVICE_STATUS
NEXT_SERVER_INDEX=0
if parse_is_service_active "${SERVICE_STATUS}"; then
info "${OPENPYN_SERVICE} is already running..."
LAST_SERVER=$(printf '%s' "${SERVICE_STATUS}" | sed -E -n "s/^ExecStart=.*--server ([a-zA-Z0-9]+).*$/\1/p")
if [[ -n "$LAST_SERVER" ]]; then
info "Currently connected to ${S_BOLD}${S_GREEN}${LAST_SERVER}${S_RESET}"
LAST_SERVER_INDEX=
for i in "${!SERVERS[@]}"; do
if [[ "${SERVERS[$i]}" = "${LAST_SERVER}" ]]; then
LAST_SERVER_INDEX=${i}
fi
done
if [[ -n "$LAST_SERVER_INDEX" ]]; then
info "Server ${S_BOLD}${S_GREEN}${LAST_SERVER}${S_RESET} is at index #${LAST_SERVER_INDEX} in ${S_RED}${S_BOLD}${COUNTRY^^}${S_RESET} servers array"
NEXT_SERVER_INDEX=$(( LAST_SERVER_INDEX + 1 ))
if [[ -n "${SERVERS[$NEXT_SERVER_INDEX]}" ]]; then
info "Next server will be index #${NEXT_SERVER_INDEX} ${S_BOLD}${S_GREEN}${SERVERS[$NEXT_SERVER_INDEX]}${S_RESET}..."
else
NEXT_SERVER_INDEX=0
info "It is the last server in the array, looping back to index #0 ${S_BOLD}${S_GREEN}${SERVERS[0]}${S_RESET}..."
fi
else
warn "${LAST_SERVER} is not found in servers array, continuing with index #0 ${S_BOLD}${S_GREEN}${SERVERS[0]}${S_RESET}..."
fi
else
warn "Currently connected server was not explicitly configured, continuing with index #0 ${S_BOLD}${S_GREEN}${SERVERS[0]}${S_RESET}..."
fi
else
warn "${OPENPYN_SERVICE} is not running, continuing with index #0 ${S_BOLD}${S_GREEN}${SERVERS[0]}${S_RESET}..."
fi
${OPENPYN_BINARY} --daemon --server ${SERVERS[$NEXT_SERVER_INDEX]} 2>&1
SUCCESS=$?
if [[ ${SUCCESS} -eq 0 ]]; then
echo_and_sleep ${SLEEP_AFTER_RECONNECTING}
N=0
while [ "$N" -lt ${SLEEP_RETRY} ]; do
N=$(( N + 1 ))
pass_service_status_to_var SERVICE_STATUS
if parse_is_service_active "${SERVICE_STATUS}"; then
info "Service successfully restarted, exiting..."
exit 0
else
echo_and_sleep ${SLEEP_AFTER_RECONNECTING}
fi
done
err "Failed to restart service, exiting..."
exit 1
else
err "Failed to rotate server, exiting..."
exit 1
fi