forked from ArcherN9/Wireguard-Interface-randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmullvad-merlin.sh
212 lines (189 loc) · 8.69 KB
/
mullvad-merlin.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
#!/bin/bash
# -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="wg.*"
wireguardConfigurationDirectory="/opt/etc/wireguard.d/"
connectedWireguardConfiguration=""
# A method to retrieve the current connected Mullvad interface.
checkMullvadConnectivity() {
# Check if Mullvad VPN is already connected.
connectedWireguardConfiguration=$(wg| grep wg | grep -v wg21 | cut -c 12-16)
# Return an arbitrary integer value | This value is not checked right now
return 0
}
case $1 in
toggle)
connectedStatus=$(curl -sSk https://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 https://am.i.mullvad.net/connected
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 https://am.i.mullvad.net/connected)
if [[ "$connectedStatus" == *"("* ]]; then
#curl https://am.i.mullvad.net/connected
#You are connected to Mullvad (server ca14-wireguard). Your IP address is 89.36.78.216
#curl https://am.i.mullvad.net/ip
#89.36.78.216
#curl https://am.i.mullvad.net/city
#Montreal
#curl https://am.i.mullvad.net/country
#Canada
#curl https://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 --silent https://am.i.mullvad.net/country | sed 's/ *$//g')
# This returns a city name
city=$(curl --silent https://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 {status|toggle [policy]|stop|start [policy]|restart [policy]}"
exit
;;
esac
checkMullvadConnectivity
# Debug log
# echo " ip addr command returned $connectedWireguardConfiguration"
# Extract the wireguard configuration list that is available in /etc/wireguard
# newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex")
if [ -z "$(ls $wireguardConfigurationDirectory | grep $mullvadVpnInterfaceRegex | grep .conf$ | grep -v "wg21.conf")" ]; then
echo "Wireguard Configuration files are missing. Expecting filed matching regex $mullvadVpnInterfaceRegex in $wireguardConfigurationDirectory. Exiting."
fi
if [ "$STOP" == "true" ];then
if [[ -n "$connectedWireguardConfiguration" ]]; then
echo "Disconnecting from $connectedWireguardConfiguration"
echo "Stopping $connectedWireguardConfiguration"
/opt/bin/wg_manager stop $interface
# Satisfies this condition if a connected interface was not found.
#elif [[ -z "$connectedWireguardConfiguration" ]]; then
#echo "Not currently connected to any VPN."
fi
curl -sSk https://am.i.mullvad.net/connected
exit
else
while : ; do
if [[ -n "$connectedWireguardConfiguration" ]]; then
fileCount=$(ls $wireguardConfigurationDirectory | grep -v "$connectedWireguardConfiguration".conf$ | grep .conf$ | grep -v "wg21.conf" | grep $mullvadVpnInterfaceRegex | wc -l)
if [ "$fileCount" == 0 ];then
echo "Not enough config files to randomize. Reconnecting to $connectedWireguardConfiguration"
else
newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep -v "$connectedWireguardConfiguration".conf$ | grep .conf$ | grep -v "wg21.conf" | grep $mullvadVpnInterfaceRegex)
newWireguardConfigurationListCount=$(ls $wireguardConfigurationDirectory | grep -v "$connectedWireguardConfiguration".conf$ | grep .conf$ | grep -v "wg21.conf" | grep $mullvadVpnInterfaceRegex | wc -l)
fi
elif [[ -z "$connectedWireguardConfiguration" ]]; then
newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep $mullvadVpnInterfaceRegex | grep .conf$ | grep -v "wg21.conf")
newWireguardConfigurationListCount=$(ls $wireguardConfigurationDirectory | grep $mullvadVpnInterfaceRegex | grep .conf$ | grep -v "wg21.conf" | wc -l)
fi
# Pick a wireguard interface at random to connect to next
if [ "$fileCount" == 0 ]; then
newWireguardConfiguration=$connectedWireguardConfiguration
else
random=$(awk -v count="$newWireguardConfigurationListCount" 'BEGIN { srand(); print int( rand() * (count-1)+1);}')
newWireguardConfiguration=$(echo "$newWireguardConfigurationList" | awk -v random="$random" 'FNR==random {print $1}' | grep -oE 'wg[0-9]{2,3}')
fi
# Satisfies this condition if a connected interface was found.
if [[ -n "$connectedWireguardConfiguration" ]]; then
echo "System is currently connected to $connectedWireguardConfiguration and will reconnect to $newWireguardConfiguration"
echo "Stopping $connectedWireguardConfiguration"
/opt/bin/wg_manager stop $connectedWireguardConfiguration
sleep 5
/opt/bin/wg_manager start $newWireguardConfiguration $2
# Satisfies this condition if a connected interface was not found.
elif [[ -z "$connectedWireguardConfiguration" ]]; then
echo "System will attempt to connect to $newWireguardConfiguration"
/opt/bin/wg_manager start $newWireguardConfiguration $2
fi
sleep 2
checkMullvadConnectivity
if [[ -n "$connectedWireguardConfiguration" ]]; then
echo "Connected to $connectedWireguardConfiguration"
elif [[ -z "$connectedWireguardConfiguration" ]]; then
echo "You are not connected."
fi
IP=$(curl -sSk https://am.i.mullvad.net/connected)
DELIMITER=':'
if [[ "$IP" == *"$DELIMITER"* ]]; then
echo "$IP"
echo "Connected to IPV6. Reconnecting."
checkMullvadConnectivity
# Debug log
# echo " ip addr command returned $connectedWireguardConfiguration"
# Extract the wireguard configuration list that is available in /etc/wireguard
# newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex")
#if [[ -n "$connectedWireguardConfiguration" ]]; then
# newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep -v "$connectedWireguardConfiguration".conf$ | grep --word-regexp "$mullvadVpnInterfaceRegex" | grep conf$)
#elif [[ -z "$connectedWireguardConfiguration" ]]; then
# newWireguardConfigurationList=$(ls $wireguardConfigurationDirectory | grep --word-regexp "$mullvadVpnInterfaceRegex" | grep conf$)
#fi
else
#curl -sSk https://am.i.mullvad.net/connected
exit
fi
[[ "$IP" == *"$DELIMITER"* ]] || break
done
fi