-
Notifications
You must be signed in to change notification settings - Fork 75
/
add-client.sh
executable file
·86 lines (68 loc) · 1.99 KB
/
add-client.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
#!/bin/bash
# We read from the input parameter the name of the client
if [ -z "$1" ]
then
read -p "Enter VPN user name: " USERNAME
if [ -z $USERNAME ]
then
echo "[#]Empty VPN user name. Exit"
exit 1;
fi
else USERNAME=$1
fi
cd /etc/wireguard/
read DNS < ./dns.var
read ENDPOINT < ./endpoint.var
read VPN_SUBNET < ./vpn_subnet.var
PRESHARED_KEY="_preshared.key"
PRIV_KEY="_private.key"
PUB_KEY="_public.key"
ALLOWED_IP="0.0.0.0/0, ::/0"
# Go to the wireguard directory and create a directory structure in which we will store client configuration files
mkdir -p ./clients
cd ./clients
mkdir ./$USERNAME
cd ./$USERNAME
umask 077
CLIENT_PRESHARED_KEY=$( wg genpsk )
CLIENT_PRIVKEY=$( wg genkey )
CLIENT_PUBLIC_KEY=$( echo $CLIENT_PRIVKEY | wg pubkey )
#echo $CLIENT_PRESHARED_KEY > ./"$USERNAME$PRESHARED_KEY"
#echo $CLIENT_PRIVKEY > ./"$USERNAME$PRIV_KEY"
#echo $CLIENT_PUBLIC_KEY > ./"$USERNAME$PUB_KEY"
read SERVER_PUBLIC_KEY < /etc/wireguard/server_public.key
# We get the following client IP address
read OCTET_IP < /etc/wireguard/last_used_ip.var
OCTET_IP=$(($OCTET_IP+1))
echo $OCTET_IP > /etc/wireguard/last_used_ip.var
CLIENT_IP="$VPN_SUBNET$OCTET_IP/32"
# Create a blank configuration file client
cat > /etc/wireguard/clients/$USERNAME/$USERNAME.conf << EOF
[Interface]
PrivateKey = $CLIENT_PRIVKEY
Address = $CLIENT_IP
DNS = $DNS
[Peer]
PublicKey = $SERVER_PUBLIC_KEY
PresharedKey = $CLIENT_PRESHARED_KEY
AllowedIPs = $ALLOWED_IP
Endpoint = $ENDPOINT
PersistentKeepalive=25
EOF
# Add new client data to the Wireguard configuration file
cat >> /etc/wireguard/wg0.conf << EOF
[Peer]
PublicKey = $CLIENT_PUBLIC_KEY
PresharedKey = $CLIENT_PRESHARED_KEY
AllowedIPs = $CLIENT_IP
EOF
# Restart Wireguard
systemctl stop wg-quick@wg0
systemctl start wg-quick@wg0
# Show QR config to display
qrencode -t ansiutf8 < ./$USERNAME.conf
# Show config file
echo "# Display $USERNAME.conf"
cat ./$USERNAME.conf
# Save QR config to png file
#qrencode -t png -o ./$USERNAME.png < ./$USERNAME.conf