-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireguard.sh
371 lines (331 loc) · 11.2 KB
/
wireguard.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
if [ -f "index.html" ]; then
rm index.html
fi
cat >index.html <<INDEX_EOF
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h1>WireGuard</h1>
<ul>
<li><a href="https://wireguard.sengshinlee.com/mirrors/wireguard-ubuntu-latest.sh">wireguard-ubuntu-latest.sh</a></li>
<li><a href="https://wireguard.sengshinlee.com/mirrors/wireguard-android-latest.apk">wireguard-android-latest.apk</a></li>
<li><a href="https://wireguard.sengshinlee.com/mirrors/wireguard-windows-latest-amd64.msi">wireguard-windows-latest-amd64.msi</a></li>
<li><a href="https://wireguard.sengshinlee.com/mirrors/wireguard-windows-latest-arm64.msi">wireguard-windows-latest-arm64.msi</a></li>
<li><a href="https://wireguard.sengshinlee.com/mirrors/wireguard-windows-latest-x86.msi">wireguard-windows-latest-x86.msi</a></li>
<li><a href="https://apps.apple.com/us/app/wireguard/id1451685025">wireguard-macos-latest (us)</a></li>
<li><a href="https://apps.apple.com/us/app/wireguard/id1441195209">wireguard-ios/ipados-latest (us)</a></li>
</ul>
INDEX_EOF
if [ -d "mirrors" ]; then
rm -rf mirrors
fi
mkdir mirrors
cd mirrors
WG_WINDOWS_PATH="https://download.wireguard.com/windows-client/"
WG_WINDOWS_AMD64=$(curl -s ${WG_WINDOWS_PATH} | cut -d '"' -f 4)
wget -t 0 ${WG_WINDOWS_PATH}${WG_WINDOWS_AMD64} -O wireguard-windows-latest-amd64.msi
WG_WINDOWS_ARM64=$(curl -s ${WG_WINDOWS_PATH} | cut -d '"' -f 6)
wget -t 0 ${WG_WINDOWS_PATH}${WG_WINDOWS_ARM64} -O wireguard-windows-latest-arm64.msi
WG_WINDOWS_X86=$(curl -s ${WG_WINDOWS_PATH} | cut -d '"' -f 6)
wget -t 0 ${WG_WINDOWS_PATH}${WG_WINDOWS_X86} -O wireguard-windows-latest-x86.msi
WG_ANDROID_PATH="https://download.wireguard.com/android-client/"
WG_ANDROID_VER=$(curl -s ${WG_ANDROID_PATH} | \
awk -F '.apk' '{print $2}' | \
cut -d '-' -f 2)
WG_ANDROID_URL=${WG_ANDROID_PATH}"com.wireguard.android-"${WG_ANDROID_VER}".apk"
wget -t 0 ${WG_ANDROID_URL} -O wireguard-android-latest.apk
cat >wireguard-ubuntu-latest.sh <<WG_UBUNTU_EO'F'
#!/bin/bash
function distrib() {
local DISTRIB=""
if [ -f "/etc/debian_version" ]; then
source /etc/os-release
DISTRIB="${ID}"
else
echo "> Distribution must be ubuntu!"
exit 1
fi
}
function kernel() {
local VER="$(uname -r | cut -d- -f1)"
if num1.num2.num3_lt ${VER} "5.6.0"; then
echo "> Kernel version must be >= 5.6.0!"
exit 1
fi
}
function num1.num2.num3_lt() {
test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"
}
function is_root() {
if [ "$(echo ${USER})" != "root" ]; then
echo "> You need to be root to run this script!"
exit 1
fi
}
function is_installed() {
if [ -f "/usr/bin/wg" ] && [ -f "/usr/bin/wg-quick" ]; then
return 0
else
return 1
fi
}
function version() {
if [ ${IS_INSTALLED} -eq 0 ]; then
wg -v
else
echo "> Not installed!"
fi
}
function install_wg_tools() {
if [ ${IS_INSTALLED} -eq 0 ]; then
echo "> Installed!"
else
apt-get install wireguard-tools -y >/dev/null 2>&1
fi
}
function create_wg_if() {
if [ ${IS_INSTALLED} -eq 0 ]; then
local SERVER_WG_NIC_NUM
while true; do
read -p "< Input a number [0,255]: " ARG
if [ -z "${ARG}" ]; then
echo "> Not be empty!"
elif [[ ${ARG} == *[!0-9]* ]]; then
echo "> Not a number!"
elif [ ${ARG} -gt 255 ]; then
echo "> Must be between [0,255]!"
elif [ -f "/etc/wireguard/wg${ARG}*" ]; then
echo "> Existed!"
else
SERVER_WG_NIC_NUM=${ARG} >/dev/null 2>&1
break
fi
done
local SERVER_PUBLIC_IPV4="$(wget -qO- -t1 -T2 ipv4.icanhazip.com)"
local SERVER_PUBLIC_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
local SERVER_WG_PORT=$(expr 10000 + ${SERVER_WG_NIC_NUM})
local SERVER_WG_NIC="wg${SERVER_WG_NIC_NUM}"
local SERVER_WG_IF="/etc/wireguard/${SERVER_WG_NIC}.conf"
local CLIENT_WG_IF="/etc/wireguard/${SERVER_WG_NIC}-client.conf"
local SERVER_WG_IPV4="10.0.${SERVER_WG_NIC_NUM}.0"
local SERVER_PRIVATE_KEY="$(wg genkey)"
local SERVER_PUBLIC_KEY="$(echo ${SERVER_PRIVATE_KEY} | wg pubkey)"
local CLIENT_WG_IPV4="10.0.${SERVER_WG_NIC_NUM}.1"
local CLIENT_PRIVATE_KEY="$(wg genkey)"
local CLIENT_PUBLIC_KEY="$(echo ${CLIENT_PRIVATE_KEY} | wg pubkey)"
create_server_if
create_client_if
else
echo "> Not installed!"
fi
}
function create_server_if() {
cat >${SERVER_WG_IF} <<EOF
[Interface]
PrivateKey = ${SERVER_PRIVATE_KEY}
Address = ${SERVER_WG_IPV4}
ListenPort = ${SERVER_WG_PORT}
PostUp = ufw route allow in on ${SERVER_WG_NIC} out on ${SERVER_PUBLIC_NIC}
PostUp = iptables -t nat -I POSTROUTING -o ${SERVER_PUBLIC_NIC} -j MASQUERADE
PostUp = ufw allow ${SERVER_WG_PORT}/udp && ufw reload
PreDown = ufw route delete allow in on ${SERVER_WG_NIC} out on ${SERVER_PUBLIC_NIC}
PreDown = iptables -t nat -D POSTROUTING -o ${SERVER_PUBLIC_NIC} -j MASQUERADE
PreDown = ufw delete allow ${SERVER_WG_PORT}/udp && ufw reload
[Peer]
PublicKey = ${CLIENT_PUBLIC_KEY}
AllowedIPs = ${CLIENT_WG_IPV4}
EOF
}
function create_client_if() {
cat >${CLIENT_WG_IF} <<EOF
[Interface]
PrivateKey = ${CLIENT_PRIVATE_KEY}
Address = ${CLIENT_WG_IPV4}
DNS = 1.1.1.1, 1.0.0.1
[Peer]
PublicKey = ${SERVER_PUBLIC_KEY}
AllowedIPs = 0.0.0.0/0
Endpoint = ${SERVER_PUBLIC_IPV4}:${SERVER_WG_PORT}
PersistentKeepalive = 25
EOF
}
function show() {
if [ ${IS_INSTALLED} -eq 0 ]; then
wg | grep "interface" | awk '{print $2}'
else
echo "> Not installed!"
fi
}
function up_wg() {
if [ ${IS_INSTALLED} -eq 0 ]; then
while true; do
read -p "< Input a number [0,255]: " ARG
if [ -z "${ARG}" ]; then
echo "> Not be empty!"
elif [[ ${ARG} == *[!0-9]* ]]; then
echo "> Not a number!"
elif [ ${ARG} -gt 255 ]; then
echo "> Must be between [0,255]!"
elif [ ! -f "/etc/wireguard/wg${ARG}.conf" ]; then
echo "> Not be created!"
else
if [ "$(show | grep wg${ARG})" == "wg${ARG}" ]; then
echo "> Already up!"
else
up_net
wg-quick up wg${ARG} >/dev/null 2>&1
fi
break
fi
done
else
echo "> Not installed!"
fi
}
function up_net() {
[ $(wg | wc -l) -eq 0 ] && sysctl -w net.ipv4.ip_forward=1 >/dev/null 2>&1
}
function down_wg() {
local COUNT=1
local SERVER_WG_ACTIVE_NIC_NUM
if [ ${IS_INSTALLED} -eq 0 ]; then
while [ ${COUNT} -le 256 ]; do
local SERVER_WG_ACTIVE_NIC_NUM=$(wg | grep "interface" | awk '{print $2}' | cut -c 3- | head -${COUNT} | tail -1)
if [ -z "${SERVER_WG_ACTIVE_NIC_NUM}" ]; then
if [ ! -f "/etc/wireguard/wg${SERVER_WG_ACTIVE_NIC_NUM}.conf" ]; then
touch /etc/wireguard/wg${SERVER_WG_ACTIVE_NIC_NUM}.conf
fi
fi
COUNT=$(expr ${COUNT} + 1)
done
while true; do
read -p "< Input a number [0,255]: " ARG
if [ -z "${ARG}" ]; then
rm /etc/wireguard/wg.conf >/dev/null 2>&1
echo "> Not be empty!"
elif [[ ${ARG} == *[!0-9]* ]]; then
echo "> Not a number!"
elif [ ${ARG} -gt 255 ]; then
echo "> Must be between [0,255]!"
else
if [ "$(show | grep wg${ARG})" == "wg${ARG}" ]; then
wg-quick down wg${ARG} >/dev/null 2>&1
if [ ! -f "/etc/wireguard/wg${ARG}.conf" ] && [ ! -f "/etc/wireguard/wg${ARG}-client.conf" ]; then
rm /etc/wireguard/wg${ARG}.conf /etc/wireguard/wg${ARG}-client.conf >/dev/null 2>&1
fi
down_net
else
echo "> No need to down!"
fi
break
fi
done
else
echo "> Not installed!"
fi
}
function down_net() {
[ $(wg | wc -l) -eq 0 ] && sysctl -w net.ipv4.ip_forward=0 >/dev/null 2>&1
}
function remove_wg_if() {
if [ ${IS_INSTALLED} -eq 0 ]; then
while true; do
read -p "< Input a number [0,255]: " ARG
if [ -z "${ARG}" ]; then
echo "> Not be empty!"
elif [[ ${ARG} == *[!0-9]* ]]; then
echo "> Not a number!"
elif [ ${ARG} -gt 255 ]; then
echo "> Must be between [0,255]!"
elif [ ! -f "/etc/wireguard/wg${ARG}.conf" ] && [ ! -f "/etc/wireguard/wg${ARG}-client.conf" ]; then
echo "> Not exist!"
else
rm /etc/wireguard/wg${ARG}.conf /etc/wireguard/wg${ARG}-client.conf >/dev/null 2>&1
break
fi
done
else
echo "> Not installed!"
fi
}
function uninstall_wg_tools() {
if [ ${IS_INSTALLED} -eq 0 ]; then
local SERVER_WG_ACTIVE_NIC=$(wg | grep "interface" | awk '{print $2}' | head -1)
while [ "${SERVER_WG_ACTIVE_NIC}" != "" ]; do
if [ ! -f "/etc/wireguard/${SERVER_WG_ACTIVE_NIC}.conf" ]; then
touch /etc/wireguard/${SERVER_WG_ACTIVE_NIC}.conf
fi
if [ ! -f "/etc/wireguard/${SERVER_WG_ACTIVE_NIC}-client.conf" ]; then
touch /etc/wireguard/${SERVER_WG_ACTIVE_NIC}-client.conf
fi
wg-quick down ${SERVER_WG_ACTIVE_NIC} >/dev/null 2>&1
SERVER_WG_ACTIVE_NIC=$(wg | grep "interface" | awk '{print $2}' | head -1)
done
apt-get remove wireguard-tools -y >/dev/null 2>&1
rm -rf /etc/wireguard >/dev/null 2>&1
else
echo "> Not installed!"
fi
}
function help() {
echo '
USAGE
bash wireguard-ubuntu-latest.sh [OPTION]
OPTION
-h, --help Show help manual
-v, --version Show "wireguard-tools" version
-i, --install Install "wireguard-tools"
-c, --create Create a new WireGuard interface configuration file
-s, --show Show the current active interface
-u, --up Enable IPv4 forward and up a specified interface
-d, --down Down a specified interface and disable IPv4 forward
-r, --remove Remove WireGuard a specified interface configuration file
-p, --purge Remove "wireguard-tools" and delete any associated configuration files
'
}
function main() {
distrib
kernel
is_root
is_installed
local IS_INSTALLED=$?
local ARG="$1"
if [ -z "${ARG}" ]; then
help
exit 0
fi
case "${ARG}" in
-h | --help)
help
;;
-v | --version)
version
;;
-i | --install)
install_wg_tools
;;
-c | --create)
create_wg_if
;;
-s | --show)
show
;;
-u | --up)
up_wg
;;
-d | --down)
down_wg
;;
-r | --remove)
remove_wg_if
;;
-p | --purge)
uninstall_wg_tools
;;
*)
help
;;
esac
}
main "$@"
WG_UBUNTU_EOF