forked from Intika-Linux-Wireguard/Mullvad-WireGuard-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·197 lines (174 loc) · 5.83 KB
/
setup.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
#!/bin/bash
TARGET_HOME=$(eval echo ~"$SUDO_USER")
CONFIG_ROOT="${TARGET_HOME}/.config/wvpn"
SERVER_LIST="${CONFIG_ROOT}/servers"
DEFAULTS="${CONFIG_ROOT}/defaults"
check_root() {
if [[ ! $UID == 0 ]]; then
echo "Error: ${0##*/} must be run as root."
exit 1
fi
}
settings() {
while :; do
echo -en "Choose installation directory (default = /usr/local/bin/):\n> "
read -r ANS
if [[ ${ANS} =~ ^.+$ ]]; then
CMD_ROOT=${ANS}
else
CMD_ROOT="/usr/local/bin/"
fi
echo -n "Install to ${CMD_ROOT}? [Y/n] "
read -r ANS
[[ ${ANS} =~ ^(Y|y|^$)$ ]] && break
done
echo -e "\nChoose your provider: "
PS3="> "
options=("Azire" "Mullvad" "Other")
select opt in "${options[@]}"; do
if [[ ${REPLY} == 1 ]]; then
PROVIDER="https://www.azirevpn.com/dl/azirevpn-wg.sh"
elif [[ ${REPLY} == 2 ]]; then
PROVIDER="https://mullvad.net/media/files/mullvad-wg.sh"
elif [[ ${REPLY} == 3 ]]; then
while :; do
echo -en "Please enter the full url to your wireguard config file:\n> "
read -r ANS
[[ ${ANS} =~ ^.+$ ]] || continue
PROVIDER=${ANS}
echo -n "Download from ${PROVIDER}? [Y/n] "
read -r ANS
[[ ${ANS} =~ ^(Y|y|^$)$ ]] && break
done
fi
[[ ${PROVIDER} ]] && break
done
echo -e "\nChoose whether to use IPv4, IPv6 or both: "
PS3="> "
options=("Both (default)" "IPv4" "IPv6")
select opt in "${options[@]}"; do
[[ ${REPLY} == 1 ]] && IP="B"
[[ ${REPLY} == 2 ]] && IP=4
[[ ${REPLY} == 3 ]] && IP=6
[[ ${IP} ]] && break
done
echo -en "\nSet Cloudflare (1.1.1.1) as DNS? [Y/n/o(ther)] "
while :; do
read -r ANS
if [[ $ANS =~ ^(Y|y|^$)$ ]]; then
DNS="1.1.1.1"
elif [[ ${ANS} =~ ^(N|n)$ ]]; then
echo "No DNS will be set (or changed)."
elif [[ ${ANS} =~ ^o$ ]]; then
while :; do
echo -en "Please enter a correct DNS IP:> "
read -r ANS
[[ ${ANS} =~ [0-9]+.[0-9]+.[0-9]+.[0-9]+ ]] || continue
DNS=${ANS}
echo -n "Set ${DNS} as DNS? [Y/n] "
read -r ANS
[[ ${ANS} =~ ^(Y|y|^$)$ ]] && break
done
else
echo -n "Invalid input, please try again [Y/n/(o)ther] "
continue
fi
break
done
echo -en "\nTurn on kill-switch for all servers? [Y/n] "
while :; do
read -r ANS
[[ $ANS =~ ^(y|Y|^$)$ ]] && KILL_SWITCH=1 && break
[[ $ANS =~ ^(n|N)$ ]] && break
echo -n "Invalid input, please try again [Y/n] "
done
}
install() {
echo -e "\nFetching configuration script"
curl -sL "${PROVIDER}" -o wvpn-wg.sh
#Injecting server list construction
LINE=$(sed -n '/mkdir -p \/etc\/wireguard/=' ./wvpn-wg.sh)
SERVER="\\\techo \"\$CODE:\t\${SERVER_LOCATIONS[\"\$CODE\"]}\" >> ./servers.tmp"
sed -i "${LINE}i${SERVER}" ./wvpn-wg.sh
#Injecting IP version control
[[ $IP == "B" ]] && ADDRESS_SETTING='$ADDRESS' #Both
[[ $IP -eq 4 ]] && ADDRESS_SETTING='${ADDRESS\/,*\/}' #v4
[[ $IP -eq 6 ]] && ADDRESS_SETTING='${ADDRESS\/*,\/}' #v6
REGEX="s/\(Address = \).*/\1${ADDRESS_SETTING}/"
sed -i "$REGEX" ./wvpn-wg.sh
#Injecting config file renaming
REGEX="s/\(CONFIGURATION_FILE=.*wireguard\/\).*\(-\$CODE.conf\)/\1wvpn\2/"
sed -i "$REGEX" ./wvpn-wg.sh
$(bash ./wvpn-wg.sh) &>/dev/null
[[ $? -ne 0 ]] || exit 1
#Injecting DNS
if [[ ${DNS} ]]; then
for file in /etc/wireguard/wvpn*; do
LINE=$(sed -n '/DNS/=' "$file")
if [[ $LINE ]]; then
sed -i "s/DNS =.*/DNS = ${DNS}/g" "$file"
else
sed -i "${LINE}i DNS = ${DNS}" "$file"
fi
done
fi
#Injecting Kill switch
if [[ ${KILL_SWITCH} ]]; then
PostUp="iptables -I OUTPUT ! -o %i -m mark ! --mark \$(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT"
PostUp=${PostUp/*/"PostUp = $PostUp && ${PostUp//ip/ip6}"}
PreDown=${PostUp//-I/-D}
PreDown=${PreDown//PostUp/PreDown}
echo -e "Turning on kill-switch for servers\n"
for file in /etc/wireguard/wvpn*; do
sed -i "/Peer/i$PostUp\n$PreDown" "$file"
done
fi
#Setting defaults
mkdir -p "${CONFIG_ROOT}"
sort --version-sort ./servers.tmp >"${SERVER_LIST}"
cat "${SERVER_LIST}"
echo -e "\nFrom the above list, please select a default server:"
AVAILABLE_SERVERS=$(awk -F':' '{print $1}' "${SERVER_LIST}")
PS3="> "
select opt in $AVAILABLE_SERVERS; do
[[ $REPLY ]] || continue
echo "DEFAULT_SERVER=${opt}" >"${DEFAULTS}"
break
done
echo -e "PROVIDER=${PROVIDER}" "\nIP=${IP}" "\nDNS=${DNS}" >>"${DEFAULTS}"
chown -R "${SUDO_USER}": "${CONFIG_ROOT}"
cp ./wvpn ${CMD_ROOT}
cp ./completion/wvpn /usr/share/bash-completion/completions/
rm ./servers.tmp ./wvpn-wg.sh
echo -e "\nInstalled! Please wait up to 60 seconds for your public key to be added to the servers."
exit 0
}
uninstall() {
echo "Removing files..."
rm "${CMD_ROOT}/wvpn" 2>/dev/null
rm "/usr/share/bash-completion/completions/wvpn" 2>/dev/null
rm -r "${CONFIG_ROOT}" 2>/dev/null
for file in /etc/wireguard/wvpn*; do
rm "$file" 2>/dev/null
done
echo "Removed"
exit 0
}
case $1 in
install)
check_root
settings
install
;;
uninstall)
check_root
uninstall
;;
*)
echo "Usage: $(basename "$0") <cmd>"
echo
echo -e "install \t Begins the installation"
echo -e "uninstall \t Uninstalls and removes all files on the machine"
exit 0
;;
esac