forked from ArcherN9/Wireguard-Interface-randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmullvad.sh
executable file
·216 lines (191 loc) · 8.65 KB
/
mullvad.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
max_timeout=3
connect_timeout=3
/home/pi/WaitForInternet.sh
# Check if another instance of script is running
pidof -o %PPID -x "$0" >/dev/null && echo "ERROR: Script $0 already running" && exit 1
echo $$ > /run/Mullvad.pid
# -e option instructs bash to immediately exit if any command [1] has a non-zero exit status
# we don't want to continue execution of the script if something is broken. This may potentially
# complicate IP routing table entries which may require manual intervention to fix thereafter.
#set -e
# Declare global variables here
# Modify the variables in this section in conformity with the naming convention of your Mullvad
# configuration files in /etc/wireguard
mullvadVpnInterfaceRegex="mullvad-\w*"
wireguardConfigurationDirectory="/etc/wireguard/"
connectedWireguardConfiguration=""
# A method to retrieve the current connected Mullvad interface.
checkWireguardConnectivity()
{
# Check if Mullvad VPN is already connected.
#connectedWireguardConfiguration=$(ip addr | grep --word-regexp "$1" | cut -d " " -f 2 | tr -d ":")
connectedWireguardConfiguration=$(wg| grep mullvad | grep -v wg0 | sed "s/interface:// ")
# Return an arbitrary integer value | This value is not checked right now
return 0
}
checkMullvadConnectivity()
{
response=$(curl --max-time $max_timeout --connect-timeout $connect_timeout --write-out %{http_code} --silent --output /dev/null https://am.i.mullvad.net/connected)
if [ "$response" = "200" ]; then
echo "Connection is healthy"
return 0
else
echo "Connection is not healthy (response code - $response )"
if [ "$1" = true ]; then
exit 1
fi
fi
}
case $1 in
toggle)
connectedStatus=$(curl -sSk https://ipv4.am.i.mullvad.net/connected)
if [[ "$connectedStatus" == *"("* ]]; then
#status=$(echo $connectedStatus | cut -d "(" -f 1 | sed 's/ *$//g')
STOP="true"
else
#echo "$(echo $connectedStatus | cut -d "." -f 1 | sed 's/ *$//g')." #1> /tmp/.checkip
STOP="false"
fi
;;
stop)
STOP="true"
;;
status)
curl -sSk --ipv4 --max-time $max_timeout --connect-timeout $connect_timeout https://ipv4.am.i.mullvad.net/connected
curl -sSk --ipv4 --max-time $max_timeout --connect-timeout $connect_timeout https://ipinfo.io/
exit
;;
#checkIP)
# This shell script is used to keep a track of which server the VPN is current connected to
# in a format that is easily readable. Let's face it, mullvad-lv1 isn\'t very descriptive.
# This script on its own is a little useless but particularly useful when queried from an
# external system.
# A good use case to use this script with is in the shortcuts iOS application or with IFTTT to query the
# client that runs the VPN to check which server it is connected to. My previous implementation
# was to keep this logic on the shortcuts application but realised that executing all curl commands
# may take sometime and I did not always have the patience to wait for a response.
# Retrieve the current status of the VPN client. The general output of this is
# You are connected to Mullvad (server us107-wireguard). Your IP address is 86.106.121.248
# We strip out the sections we don't require and use the rest.
#connectedStatus=$(curl -sSk --ipv4 --connect-timeout 5 https://ipv4.am.i.mullvad.net/connected)
#if [[ "$connectedStatus" == *"("* ]]; then
#curl --sS https://ipv4.am.i.mullvad.net/connected
#You are connected to Mullvad (server ca14-wireguard). Your IP address is 89.36.78.216
#curl --sS https://ipv4.am.i.mullvad.net/ip
#89.36.78.216
#curl --sS https://ipv4.am.i.mullvad.net/city
#Montreal
#curl --sS https://ipv4.am.i.mullvad.net/country
#Canada
#curl --sS https://ipv4.am.i.mullvad.net/json
#{
# "ip": "89.36.78.216",
# "country": "Canada",
# "city": "Montreal",
# "longitude": -73.5525,
# "latitude": 45.5053,
# "mullvad_exit_ip": true,
# "mullvad_exit_ip_hostname": "ca14-wireguard",
# "mullvad_server_type": "WireGuard",
# "blacklisted": {
# "blacklisted": false,
# "results": [
# {
# "name": "Project Honeypot",
# "link": "https://www.projecthoneypot.org/about_us.php",
# "blacklisted": false
# },
# {
# "name": "Spamhaus",
# "link": "https://www.spamhaus.org/organization/",
# "blacklisted": false
# }
# ]
# },
# "organization": "M247"
#}
# status=$(echo $connectedStatus | cut -d "(" -f 1 | sed 's/ *$//g')
# ip=$(echo $connectedStatus | cut -d "." -f 2-5 | sed 's/ *$//g')
# # This returns a country name.
# country=$(curl -sSk --ipv4 https://ipv4.am.i.mullvad.net/country | sed 's/ *$//g')
# # This returns a city name
# city=$(curl -sSk --ipv4 https://ipv4.am.i.mullvad.net/city | sed 's/ *$//g')
# echo "$status server in $city, $country.$ip" #1> /tmp/.checkip
# else
# echo "$(echo $connectedStatus | cut -d "." -f 1 | sed 's/ *$//g')." #1> /tmp/.checkip
# fi
# exit
#;;
start)
STOP="false"
;;
restart)
STOP="false"
;;
*)
echo "Usage: $0 {start|stop|toggle|restart|status}"
exit
;;
esac
# checkMullvadConnectivity
checkWireguardConnectivity "$mullvadVpnInterfaceRegex"
# Debug log
# echo " ip addr command returned $connectedWireguardConfiguration"
# Extract the wireguard configuration list that is available in /etc/wireguard
# newWireguardConfigurationList=$(sudo ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex")
if [ -z "$(sudo ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex" | grep conf$)" ]; then
echo "Wireguard Configuration files are missing. Expecting filed matching regex $mullvadVpnInterfaceRegex in $wireguardConfigurationDirectory. Exiting."
fi
if [[ -n "$connectedWireguardConfiguration" ]]; then
newWireguardConfigurationList=$(sudo ls $wireguardConfigurationDirectory | grep -v "$connectedWireguardConfiguration" | grep --word-regexp "$mullvadVpnInterfaceRegex" | grep conf$)
elif [[ -z "$connectedWireguardConfiguration" ]]; then
newWireguardConfigurationList=$(sudo ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex" | grep conf$)
fi
if [ "$STOP" == "true" ];then
if [[ -n "$connectedWireguardConfiguration" ]]; then
echo "Disconnecting currently connected to $connectedWireguardConfiguration"
sudo wg-quick down $connectedWireguardConfiguration # 2> /dev/null
# Satisfies this condition if a connected interface was not found.
#This is redundant because the output of the curl says "You are not connected to Mullvad"
#elif [[ -z "$connectedWireguardConfiguration" ]]; then
#echo "Not currently connected to any VPN."
fi
curl -sSk --ipv4 --max-time $max_timeout --connect-timeout $connect_timeout https://ipv4.am.i.mullvad.net/connected
exit
else
while : ; do
# Pick a wireguard interface at random to connect to next
newWireguardConfiguration=$(shuf -n 1 -e $newWireguardConfigurationList)
# Satisfies this condition if a connected interface was found.
if [[ -n "$connectedWireguardConfiguration" ]]; then
echo "System is currently connected to $connectedWireguardConfiguration and will be switching over to $newWireguardConfiguration"
echo "Disconnecting from $connectedWireguardConfiguration"
sudo wg-quick down $connectedWireguardConfiguration # 2> /dev/null
echo "Connecting to $newWireguardConfiguration"
sudo wg-quick up $wireguardConfigurationDirectory$newWireguardConfiguration # 2> /dev/null
# Satisfies this condition if a connected interface was not found.
elif [[ -z "$connectedWireguardConfiguration" ]]; then
echo "System will attempt to connect to $newWireguardConfiguration"
sudo wg-quick up $wireguardConfigurationDirectory$newWireguardConfiguration # 2> /dev/null
fi
sleep 2
checkMullvadConnectivity
IP=$(curl -sSk --ipv4 --max-time $max_timeout --connect-timeout $connect_timeout https://ipv4.am.i.mullvad.net/connected)
successDelimiter='.'
ipv6Delimiter=':'
if [[ "$IP" == *"$ipv6Delimiter"* ]]; then
echo "$IP"
echo "Connected to IPV6. Reconnecting."
checkWireguardConnectivity "$mullvadVpnInterfaceRegex"
elif [[ "$IP" != *"$successDelimiter"* ]]; then
echo "$IP"
echo "Failed to connect."
checkWireguardConnectivity "$mullvadVpnInterfaceRegex"
else
curl -sSk --ipv4 --max-time $max_timeout --connect-timeout $connect_timeout https://ipv4.am.i.mullvad.net/connected
exit
fi
[[ "$IP" != *"$successDelimiter"* ]] || [[ "$IP" == *"$ipv6Delimiter"* ]] || break
done
fi