-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-restart-vpn.sh
55 lines (41 loc) · 1.85 KB
/
run-restart-vpn.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
#!/bin/bash
#this script checks if the interface wg0 is already present. if yes, bring down the interface/VPN and reconnect.
cd /data/split-vpn/wireguard/protonvpn/ || exit
# Prerequisite checks
for cmd in curl jq wg ip; do
if ! command -v "$cmd" &>/dev/null; then
echo >&2 "$cmd required, but not installed. Install with 'apt install $cmd' (Debian/Ubuntu) or 'yum install $cmd' (CentOS/RHEL)."
exit 1
fi
done
# Check if wg0 interface is already present and bring it down if necessary
if ip link show wg0 &>/dev/null; then
echo "wg0 interface is up. Bringing it down..."
wg-quick down /data/split-vpn/wireguard/protonvpn/configs/wg0.conf
fi
apiurl="https://api.protonvpn.ch/vpn/logicals"
entrycountry="AT"
exitcountry="AT"
vpntier="2"
get_min_load_server() {
local response=$(curl -s "$apiurl")
[ -z "$response" ] && { echo "ERROR - API not responding or not reachable"; exit 1; }
local server_data=$(echo "$response" | jq -c '.LogicalServers[] | select(.EntryCountry == "'$entrycountry'" and .ExitCountry == "'$exitcountry'" and .Tier == '$vpntier' and .Status != 0)')
local min_load=101
local min_load_server=""
for data in $server_data; do
local server=$(echo "$data" | jq -r '.Name')
local load=$(echo "$data" | jq -r '.Load')
if (( $(echo "$load < $min_load" | bc -l) )); then
min_load="$load"
min_load_server="$server"
fi
done
[ -z "$min_load_server" ] && { echo "ERROR - No available servers matching criteria"; exit 1; }
echo "$min_load_server"
}
min_load_server=$(get_min_load_server)
config_path="/data/split-vpn/wireguard/protonvpn/configs"
cp "$config_path/wg0_${min_load_server}.conf" "$config_path/wg0.conf" && chmod 600 "$config_path/wg0.conf"
echo "Using server $min_load_server"
wg-quick up "$config_path/wg0.conf" | tee wireguard.log 2>&1