-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·67 lines (59 loc) · 1.71 KB
/
run.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
#!/bin/bash
FORCE_NEW=${FORCE_NEW:-0}
WG_PATH="/etc/wireguard"
[ "$DEBUG" ] && WG_PATH=$PWD
LOG_FILE="/var/log/wg-roulette.log"
WG_INTERFACE=${WG_INTERFACE:-wg0}
WG_LOCATION="$WG_PATH/$WG_INTERFACE.conf"
WG_CONFIG_FOLDER=${WG_CONFIG_FOLDER:-conf}
## My VPN provider has an internal ip that I can ping to verify that my tunnel is active.
PING_IP=${PING_IP:-10.64.0.1}
RANDOM_CONFIG=""
[ "$DEBUG" ] && WG_CONFIG_FOLDER=$WG_CONFIG_FOLDER.example
killTunnel(){
HAS_WG_INTERFACE=$(ifconfig | grep $WG_INTERFACE)
if [ "$HAS_WG_INTERFACE" != "" ]; then
wg-quick down $WG_INTERFACE 2> /dev/null
sleep 2
fi
}
killCurrentAndPickANewConfig() {
killTunnel
RANDOM_CONFIG=$(ls $WG_CONFIG_FOLDER | shuf -n 1)
cp $WG_CONFIG_FOLDER/$RANDOM_CONFIG $WG_LOCATION
sleep 1
wg-quick up $WG_INTERFACE
sleep 2
}
validateRuns=0
start(){
validateRuns=$((validateRuns+1))
if [ $validateRuns -gt 5 ]; then
echo "`date` Failed to start '$WG_INTERFACE'" >> $LOG_FILE 2> /dev/null
exit 0
fi
idx=0
HAS_WG_INTERFACE=$(ifconfig | grep $WG_INTERFACE)
if [ "$HAS_WG_INTERFACE" != "" ]; then
echo "`date` HAS_WG_INTERFACE" >> $LOG_FILE 2> /dev/null
while [ $idx -lt 2 ]; do
if /bin/ping -c 1 -I $WG_INTERFACE $PING_IP; then
[ "$RANDOM_CONFIG" ] && echo "`date` Successfully started with '$RANDOM_CONFIG'" >> $LOG_FILE 2> /dev/null
exit 0
fi
idx=$((idx+1))
done
else
[ "$RANDOM_CONFIG" ] && echo "`date` Failed to start with '$RANDOM_CONFIG'" >> $LOG_FILE 2> /dev/null
fi
killCurrentAndPickANewConfig
start
}
touch $LOG_FILE 2> /dev/null
if [ "$DEBUG" ]; then
killCurrentAndPickANewConfig
exit 0
else
[ "$FORCE_NEW" = "1" ] && killCurrentAndPickANewConfig
start
fi