-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwgreconnect.bash
executable file
·184 lines (164 loc) · 4.38 KB
/
wgreconnect.bash
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
181
182
183
184
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 [-h] [-i interval] [-t timeout] [-v] [-f] INTERFACE TARGET_IP"
echo "Options:"
echo " -h Show this help message"
echo " -i interval Check interval in seconds (default: 60)"
echo " -t timeout Ping timeout in seconds (default: 5)"
echo " -v Verbose output"
echo " -f Force restart even if ping succeeds"
echo
echo "Example: $0 -i 30 wg0 8.8.8.8"
exit 1
}
# Function to log messages
log() {
local level=$1
shift
local msg="$@"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
INFO)
[ "$VERBOSE" = true ] && echo "[$timestamp] INFO: $msg"
;;
WARN)
echo "[$timestamp] WARNING: $msg" >&2
;;
ERROR)
echo "[$timestamp] ERROR: $msg" >&2
;;
*)
echo "[$timestamp] $msg"
;;
esac
}
# Function to check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
log ERROR "This script must be run as root"
exit 1
fi
}
# Function to check dependencies
check_dependencies() {
for cmd in systemctl ping ip; do
if ! command -v "$cmd" >/dev/null 2>&1; then
log ERROR "Required command '$cmd' not found"
exit 1
fi
done
}
# Function to validate interface
validate_interface() {
local interface=$1
if ! ip link show "$interface" >/dev/null 2>&1; then
log ERROR "Interface $interface does not exist"
exit 1
fi
}
# Function to restart WireGuard interface
restart_wireguard() {
local interface=$1
log INFO "Restarting WireGuard interface $interface"
if systemctl restart "wg-quick@${interface}"; then
log INFO "WireGuard interface $interface restarted successfully"
return 0
else
log ERROR "Failed to restart WireGuard interface $interface"
return 1
fi
}
# Default values
INTERVAL=60
TIMEOUT=5
VERBOSE=false
FORCE=false
# Parse arguments
while getopts "hi:t:vf" opt; do
case $opt in
h)
usage
;;
i)
if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -lt 1 ]; then
log ERROR "Interval must be a positive number"
exit 1
fi
INTERVAL=$OPTARG
;;
t)
if ! [[ "$OPTARG" =~ ^[0-9]+$ ]] || [ "$OPTARG" -lt 1 ]; then
log ERROR "Timeout must be a positive number"
exit 1
fi
TIMEOUT=$OPTARG
;;
v)
VERBOSE=true
;;
f)
FORCE=true
;;
\?)
usage
;;
esac
done
shift $((OPTIND-1))
# Check required arguments
if [ "$#" -ne 2 ]; then
log ERROR "Missing required arguments"
usage
fi
INTERFACE=$1
TARGET_IP=$2
# Initial checks
check_root
check_dependencies
validate_interface "$INTERFACE"
# Validate IP address format
if ! [[ "$TARGET_IP" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
log ERROR "Invalid IP address format. Expected format: XXX.XXX.XXX.XXX"
exit 1
fi
log INFO "Starting WireGuard connection monitor:"
log INFO "- Interface: $INTERFACE"
log INFO "- Target IP: $TARGET_IP"
log INFO "- Check Interval: $INTERVAL seconds"
log INFO "- Ping Timeout: $TIMEOUT seconds"
log INFO "- Force Restart: $FORCE"
FAILURES=0
RESTARTS=0
START_TIME=$(date +%s)
while true; do
if [ "$FORCE" = true ]; then
log INFO "Forced restart requested"
if restart_wireguard "$INTERFACE"; then
((RESTARTS++))
fi
FORCE=false
else
if ! ping -c 1 -W "$TIMEOUT" "$TARGET_IP" >/dev/null 2>&1; then
((FAILURES++))
log WARN "Ping failed (failure #$FAILURES)"
if restart_wireguard "$INTERFACE"; then
((RESTARTS++))
FAILURES=0
fi
else
FAILURES=0
[ "$VERBOSE" = true ] && log INFO "Connection is healthy"
fi
fi
# Show statistics if verbose
if [ "$VERBOSE" = true ]; then
CURRENT_TIME=$(date +%s)
UPTIME=$((CURRENT_TIME - START_TIME))
log INFO "Statistics:"
log INFO "- Uptime: ${UPTIME}s"
log INFO "- Total Restarts: $RESTARTS"
log INFO "- Current Failures: $FAILURES"
fi
sleep "$INTERVAL"
done