forked from denisix/wireguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addclient
executable file
·66 lines (56 loc) · 1.61 KB
/
addclient
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
#!/usr/bin/env sh
NAME=$1
if [ "$NAME" = "" ]
then
echo "Usage: script <clientname>"
exit 0
fi
[ -d /etc/wireguard/clients ] || mkdir -p /etc/wireguard/clients
if [ ! -e "/etc/wireguard/clients/$NAME.pub" ]
then
wg genkey | tee /etc/wireguard/clients/"$NAME".key | wg pubkey > /etc/wireguard/clients/"$NAME".pub
SERV_PUB=$(cat /etc/wireguard/wg0.pub)
CLIENT_KEY=$(cat /etc/wireguard/clients/"$NAME".key)
[ -e /etc/wireguard/clients.num ] || echo 2 > /etc/wireguard/clients.num
N=$(cat /etc/wireguard/clients.num)
N_READ=$N
# get public ip
if [ "$PUBLIC_IP" = "1.2.3.4" ]
then
PUBLIC_IP=$(curl -s ifconfig.co)
export PUBLIC_IP
fi
# Create client's address
C=$((N/254))
if [ "$C" -gt 0 ]
then
N=$((N%254+1))
fi
# Create config
cat > /etc/wireguard/clients/"$NAME".conf << EOF
[Interface]
PrivateKey = $CLIENT_KEY
Address = 10.88.$C.$N/16
DNS = $DNS
[Peer]
PublicKey = $SERV_PUB
AllowedIPs = 0.0.0.0/0
Endpoint = $PUBLIC_IP:$PORT
EOF
chmod go-rw /etc/wireguard/clients/"$NAME".key /etc/wireguard/clients/"$NAME".conf
echo $((N_READ+1)) > /etc/wireguard/clients.num
fi
echo
echo "Client \"$NAME\" config, you can create using following cmds:"
echo "cat > /etc/wireguard/wg0.conf << EOF"
cat /etc/wireguard/clients/"$NAME".conf
echo "EOF"
echo "systemctl enable wg-quick@wg0.service"
echo "systemctl start wg-quick@wg0.service"
echo
qrencode -t ansiutf8 < /etc/wireguard/clients/"$NAME".conf
if [ "$WG_CLIENTS_UNSAFE_PERMISSIONS" -eq 1 ]
then
chmod -R go+r /etc/wireguard/clients
chmod go+r /etc/wireguard/clients/"$NAME".conf
fi