forked from finzzz/wgzero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwgzero
executable file
·434 lines (369 loc) · 13.4 KB
/
wgzero
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#! /usr/bin/env bash
TEMPLATEURL="https://raw.githubusercontent.com/finzzz/wgzero/master/template"
GRAY="$(tput setaf 8)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
ORANGE="$(tput setaf 3)"
CYAN="$(tput setaf 6)"
NC="$(tput sgr0)"
Run(){
IsRoot
CheckPackages "wg iptables curl qrencode jq"
EnableIPForward
WG_INTERFACE="$3"
[ "$3" == "" ] && WG_INTERFACE="wg0"
case "$1" in
"install")
# wgzero install
Install ;;
"uninstall")
# wgzero uninstall <wg_interface>
ReadInput "Uninstall $2" "y/N"
if [ "$VAL" == "y" ]; then
wg-quick down "$2"
rm -rf "/etc/wireguard/$2.conf"
fi
;;
"import")
# wgzero import wg0.conf
WG_INTERFACE=$(basename "$2" .conf)
cp "$2" /etc/wireguard/
wg-quick up "$WG_INTERFACE"
systemctl enable wg-quick@"$WG_INTERFACE"
Print "Done, make sure $(ReadConfig ListenPort "$WG")/UDP is open\\n" "red"
;;
"list")
# wgzero list <wg_interface>
WG_INTERFACE="$2"
[ "$2" == "" ] && WG_INTERFACE="wg0"
Client "list" "$WG_INTERFACE" ;;
"add")
# wgzero add <client_name> <wg_interface=wg0>
Add "$2" "$WG_INTERFACE" ;;
"del")
# wgzero delete <client_name> <wg_interface=wg0>
Client "delete" "$2" "$WG_INTERFACE" ;;
"enable")
# wgzero enable <client_name> <wg_interface=wg0>
Client "enable" "$2" "$WG_INTERFACE" ;;
"disable")
# wgzero disable <client_name> <wg_interface=wg0>
Client "disable" "$2" "$WG_INTERFACE" ;;
"show")
# wgzero show <client_name> <wg_interface=wg0>
Client show "$2" "$WG_INTERFACE" ;;
"qr")
# wgzero qr <client_name> <wg_interface=wg0>
Client show "$2" "$WG_INTERFACE" | qrencode -t ansiutf8 ;;
"postup")
# wgzero postup <wg_interface>
WG_INTERFACE="$2"
INTERFACE=$(ReadConfig Interface "$WG_INTERFACE")
TYPE=$(ReadConfig Type "$WG_INTERFACE")
iptables -t nat -A POSTROUTING -o "$INTERFACE" -j MASQUERADE
if [ "$TYPE" == "nat" ]; then
ip6tables -t nat -A POSTROUTING -o "$INTERFACE" -j MASQUERADE
elif [ "$TYPE" == "fr" ]; then
ip6tables -A FORWARD -i "$WG_INTERFACE" -j ACCEPT
fi
;;
"postdown")
# wgzero postdown <wg_interface>
WG_INTERFACE="$2"
INTERFACE=$(ReadConfig Interface "$WG_INTERFACE")
TYPE=$(ReadConfig Type "$WG_INTERFACE")
iptables -t nat -D POSTROUTING -o "$INTERFACE" -j MASQUERADE
if [ "$TYPE" == "nat" ]; then
ip6tables -t nat -D POSTROUTING -o "$INTERFACE" -j MASQUERADE
elif [ "$TYPE" == "fr" ]; then
ip6tables -D FORWARD -i "$WG_INTERFACE" -j ACCEPT
fi
;;
*)
Print "Unknown command\\n" "red" ;;
esac
}
### PRECHECKS ###
IsRoot(){
if [[ ! "$EUID" -eq 0 ]]; then
Print "Must be run as root\\n" "red" && exit
fi
}
CheckPackages(){
for i in $1; do
if [[ ! $(command -v "$i") ]]; then
Print "$i: command not found, please check required packages.\\n" "red" && exit
fi
done
}
EnableIPForward(){
sed -i 's/\#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv4/ip_forward
# IPv6
sed -i 's/\#net.ipv6.conf.all.forwarding=1/net.ipv6.conf.all.forwarding=1/' /etc/sysctl.conf
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
/usr/sbin/sysctl -p -q
}
###
### UTILS ###
Print(){
case "$2" in
"red")
echo -en "${RED}$1${NC}";;
"cyan")
echo -en "${CYAN}$1${NC}";;
"orange")
echo -en "${ORANGE}$1${NC}";;
"gray")
echo -en "${GRAY}$1${NC}";;
*)
echo -en "${GREEN}$1${NC}";;
esac
}
######
### INSTALL ###
GenerateConfig(){
# GenerateConfig wg0
CFG="/etc/wireguard/$1.conf"
if [ -f "$CFG" ]; then
ReadInput "Config file $CFG already exists, do you want to overwrite" "y/N"
if [ "$VAL" == "y" ]; then
wg-quick down "$1"
rm "$CFG"
ip link del "$1" 2>/dev/null
else
exit
fi
fi
curl -so "$CFG" "$TEMPLATEURL/wg0.conf"
sed -i "s/WG_INTERFACE/$1/g" "$CFG"
chmod 600 "$CFG"
}
SetInterface(){
# SetInterface wg0
INTERFACES=$(ip -json l | jq -r '.[]|.ifname' | grep -v 'lo\|wg')
DEFAULT=$(echo "$INTERFACES" | head -1)
Print "Available interfaces :\\n" "cyan" && echo "$INTERFACES"
WriteConfig "Interface" "$DEFAULT" "$1"
}
Install(){
ReadInput "Wireguard Interface Name" "wg0" && WG="$VAL"
GenerateConfig "$WG"
SetInterface "$WG"
WriteConfig "Endpoint" "$(curl -s ip.me)" "$WG"
WriteConfig "ListenPort" "$(shuf -n 1 -i 10000-65535)" "$WG"
WriteConfig "Address" "10.10.0.1/24" "$WG"
WriteConfig "MTU" "1420" "$WG"
WriteConfig "Client MTU" "1384" "$WG"
WriteConfig "DNS" "unset" "$WG"
WriteConfig "KeepAlive" "unset" "$WG"
# Generate Private Key
PRIVKEY=$(/usr/bin/wg genkey)
ReadInput "Specify private key" "none"
[ "$VAL" != "none" ] && PRIVKEY="$VAL"
sed -i "s|PrivateKey =.*|PrivateKey = $PRIVKEY|g" "/etc/wireguard/$WG.conf"
TYPE="v4"
ReadInput "Enable IPv6" "y/N"
if [ "$VAL" == "y" ]; then
TYPE="nat"
WriteConfig "IPv6 Prefix" "fd00::" "$WG"
WriteConfig "IPv6 Subnet" "64" "$WG"
Interface=$(ReadConfig 'Interface' "$WG")
IPv6Prefix=$(ReadConfig 'IPv6 Prefix' "$WG")
IPv6Subnet=$(ReadConfig 'IPv6 Subnet' "$WG")
Print "External routing: \\n" "orange"
Print "[1] NAT\\n" "cyan"
Print "[2] Full Routing\\n" "cyan"
ReadInput "Selection" "1"
if [ "$VAL" == "2" ]; then
TYPE="fr"
ReadInput "Configure ndppd" "y/N"
if [ "$VAL" == "y" ]; then
CheckPackages ndppd
curl -so /etc/ndppd.conf "$TEMPLATEURL/ndppd.conf"
sed -i -e "s/IFACE/$Interface/g" /etc/ndppd.conf
sed -i -e "s/PREFIX/$IPv6Prefix/g" /etc/ndppd.conf
systemctl enable ndppd.service
systemctl restart ndppd.service
fi
fi
SetClientIPv6 "$IPv6Prefix" "$IPv6Subnet"
sed -i "s|Address =.*|&, $VAL|g" "/etc/wireguard/$WG.conf"
fi
sed -i "s/Type =.*/TYPE = $TYPE/g" "/etc/wireguard/$WG.conf"
wg-quick up "$WG" 2>/dev/null
systemctl enable wg-quick@"$WG"
Print "Done, make sure $(ReadConfig ListenPort "$WG")/UDP is open\\n" "red"
}
######
### SERVER CONFIG ###
ReadInput(){
read -rp "${ORANGE}$1 [$2]: ${NC}" VAL
VAL=${VAL:-"$2"}
}
ReadConfig(){
# ReadConfig section wg0
SECTION=$(grep -n "## SERVER ##" "/etc/wireguard/$2.conf" | cut -d ":" -f 1)
sed -n "1,$SECTION p" "/etc/wireguard/$2.conf" | grep -i "$1 = " | cut -d "=" -f 2- | tr -d " "
}
WriteConfig(){
# WriteConfig section default_val wg0
ReadInput "$1" "$2"
sed -i "s|$1 =.*|$1 = $VAL|g" "/etc/wireguard/$3.conf"
}
######
### Client Config ###
PrintIPs() {
# PrintIPs 10.0.0.0/24
BASE=${1%/*}
MASKSIZE=${1#*/}
[ "$MASKSIZE" -lt 8 ] && { echo "Max range is /8."; exit 1; }
MASK=$(( 0xFFFFFFFF << (32 - "$MASKSIZE") ))
IFS=. read -r a b c d <<< "$BASE"
IP=$(( (b << 16) + (c << 8) + d ))
IPSTART=$(( IP & MASK ))
IPEND=$(( (IPSTART | ~MASK ) & 0x7FFFFFFF ))
seq "$IPSTART" "$IPEND" | while read -r i; do
LAST=$(( i & 0x00FF ))
if [ $LAST -ne 0 ] && [ $LAST -ne 255 ]; then
echo "$a.$(( (i & 0xFF0000) >> 16 )).$(( (i & 0xFF00) >> 8 )).$LAST"
fi
done
}
SetClientIPv4(){
# SetClientIPv4 wg0
SUBNET=$(ReadConfig Address "$1" | cut -d "," -f 1)
if grep -qi ":" <<< "$SUBNET"; then
SUBNET=$(ReadConfig Address "$1" | cut -d "," -f 2)
fi
for i in $(PrintIPs "$SUBNET"); do
DEFAULT="$i"
grep -q "$i" "/etc/wireguard/$1.conf" || break
done
ReadInput "Choose client IPv4" "$DEFAULT"
}
SetClientIPv6(){
# SetClientIPv6 prefix subnet
VAL="$1$(head -c 2 /dev/random | od -A n -t x2 | tr -d ' ')/$2"
}
WriteClientConfig(){
# WriteClientConfig section value file uncomment
sed -i "s|$1 =.*|$1 = $2|g" "$3"
if [ "$4" == "uncomment" ];then
sed -i "s|^# $1|$1|g" "$3"
fi
}
ReadClientConfig(){
# ReadClientConfig section blocks
grep -i "$1" "$2" | cut -d '=' -f 2- | tr -d ' '
}
######
Add(){
[[ "$1" == "" ]] && exit # must specify client name
ALIAS="$1"
WG="$2"
if grep -qi "$ALIAS" "/etc/wireguard/$WG.conf" ; then
Print "Client already exists\\n" "red" && exit
fi
PRIVKEY="$(wg genkey)"
TMP=$(mktemp)
curl -so "$TMP" "$TEMPLATEURL/peer.conf"
WriteClientConfig Alias "$ALIAS" "$TMP"
WriteClientConfig PrivateKey "$PRIVKEY" "$TMP"
WriteClientConfig PublicKey "$(wg pubkey <<< "$PRIVKEY")" "$TMP"
SetClientIPv4 "$WG" && WriteClientConfig AllowedIPs "$VAL/32" "$TMP"
if [ "$(ReadConfig Type "$WG")" != "v4" ]; then
IPv6Prefix=$(ReadConfig 'IPv6 Prefix' "$WG")
SetClientIPv6 "$IPv6Prefix" "128"
ReadInput "Choose client IPv6" "$VAL"
sed -i "s|AllowedIPs =.*|&,$VAL|g" "$TMP"
fi
ReadInput "Generate Pre-Shared Key?" "y/N"
if [ "$VAL" == "y" ]; then
WriteClientConfig PreSharedKey "$(wg genpsk)" "$TMP" uncomment
fi
cat "$TMP"
ReadInput "Add the following client?" "Y/n"
if [ "$VAL" == "n" ]; then
rm -rf "$TMP" && exit
fi
cat "$TMP" >> "/etc/wireguard/$WG.conf" && rm -rf "$TMP"
wg syncconf "$WG" <(wg-quick strip "$WG")
}
Client(){
if [ "$1" == "list" ]; then
WG="$2"
else
[[ ! "$2" ]] && exit # must specify client name
ALIAS="$2"
WG="$3"
if ! grep -qi "Alias = $ALIAS" "/etc/wireguard/$WG.conf" ; then
Print "Client doesn't exist\\n" "red" && exit
fi
fi
mapfile -t CLIENTS < <(grep -nE '\[Peer\]$|## CLIENT ##' "/etc/wireguard/$WG.conf" | cut -d ':' -f 1)
for (( i=0;i<${#CLIENTS[@]}-1;i+=2 )); do
START="${CLIENTS[$i]}"
END="${CLIENTS[(($i+1))]}"
CLIENT_BLOCK=$(mktemp)
sed -n "$START,$END p" /etc/wireguard/"$WG".conf > "$CLIENT_BLOCK"
PUBKEY=$(grep PublicKey "$CLIENT_BLOCK" | cut -d '=' -f 2- | tr -d ' ')
if [ "$1" == list ]; then
ALIAS=$(grep Alias "$CLIENT_BLOCK" | cut -d '=' -f 2- | tr -d ' ')
IP=$(grep AllowedIPs "$CLIENT_BLOCK" | cut -d '=' -f 2- | tr -d ' ')
if grep -qE "^# \[Peer\]" "$CLIENT_BLOCK"; then
Print "$ALIAS,$PUBKEY,$IP" "gray" | column -t -s, 2>/dev/null
else
Print "$ALIAS,$PUBKEY,$IP" "orange" | column -t -s, 2>/dev/null
fi
elif grep -qi "$ALIAS" "$CLIENT_BLOCK" ; then
case "$1" in
"show")
TMP=$(mktemp)
curl -so "$TMP" "$TEMPLATEURL/client.conf"
# client
WriteClientConfig Address "$(ReadClientConfig AllowedIPs "$CLIENT_BLOCK")" "$TMP"
WriteClientConfig PrivateKey "$(ReadClientConfig PrivateKey "$CLIENT_BLOCK")" "$TMP"
WriteClientConfig MTU "$(ReadConfig 'Client MTU' "$WG")" "$TMP"
DNS="$(ReadConfig DNS "$WG")" && [ "$DNS" != "unset" ] && WriteClientConfig DNS "$DNS" "$TMP" uncomment
# server
WriteClientConfig PublicKey "$(ReadConfig PrivateKey "$WG" | wg pubkey)" "$TMP"
WriteClientConfig Endpoint "$(ReadConfig Endpoint "$WG"):$(ReadConfig ListenPort "$WG")" "$TMP"
WriteClientConfig AllowedIPs "0.0.0.0/0" "$TMP"
grep -qi '^# PresharedKey' "$CLIENT_BLOCK" || WriteClientConfig PresharedKey "$(ReadClientConfig PresharedKey "$CLIENT_BLOCK")" "$TMP" uncomment
KeepAlive=$(ReadConfig KeepAlive "$WG") && [ "$KeepAlive" != "unset" ] && WriteClientConfig PersistentKeepalive "$KeepAlive" "$TMP" uncomment
TYPE=$(ReadConfig Type "$WG") && [ "$TYPE" != "v4" ] && WriteClientConfig AllowedIPs "0.0.0.0/0,::/0" "$TMP"
cat "$TMP" && echo "" && rm -rf "$TMP" "$CLIENT_BLOCK" && exit
;;
"delete")
cat "$CLIENT_BLOCK"
ReadInput "Remove this client?" "Y/n"
[ "$VAL" == "n" ] && exit
sed -i "$START,$END d" "/etc/wireguard/$WG.conf"
;;
"disable")
if grep -qi '^# \[Peer\]' "$CLIENT_BLOCK"; then
Print "Client already disabled\\n" "red" && exit
fi
cat "$CLIENT_BLOCK"
ReadInput "Disable this client?" "Y/n"
[ "$VAL" == "n" ] && exit
sed -i -e "$START,$END s|^|# |g" "/etc/wireguard/$WG.conf"
;;
"enable")
if grep -qi '^\[Peer\]' "$CLIENT_BLOCK"; then
Print "Client already enabled\\n" "red" && exit
fi
sed -e "$START,$END s|^# ||g" "/etc/wireguard/$WG.conf" | sed -n "$START,$END p"
ReadInput "Enable this client?" "Y/n"
[ "$VAL" == "n" ] && exit
sed -i -e "$START,$END s|^# ||g" "/etc/wireguard/$WG.conf"
;;
esac
wg set "$WG" peer "$PUBKEY" remove
wg syncconf "$WG" <(wg-quick strip "$WG")
fi
rm -rf "$CLIENT_BLOCK"
done
}
Run "$@"