-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwg-add-client
executable file
·45 lines (36 loc) · 1.38 KB
/
wg-add-client
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
#!/bin/bash
if [ $# -eq 0 ]; then
echo "usage: ${0} client-name"
exit 1
fi
echo "Creating client config for: $1"
source /etc/wireguard/config
mkdir -p /etc/wireguard/clients/$1
key="$(wg genkey)"
echo "${key}" > /etc/wireguard/clients/$1/$1.priv
peer="$(echo ${key} | wg pubkey)"
echo ${peer} > /etc/wireguard/clients/$1/$1.pub
this_ip=$(($(cat /etc/wireguard/last-ip.txt)+1))
echo "${this_ip}" > /etc/wireguard/last-ip.txt
ip="$(echo ${vpn_network} | cut -d '.' -f -3).${this_ip}"
echo "${1} ${peer}" >> /etc/wireguard/clientlist.txt
cat /etc/wireguard/wg0-client.example.conf | \
sed -e "s/VPN_IP/${ip}/" | \
sed -e "s|PRIVKEY|${key}|" | \
sed -e "s|SERVER_PUBLIC|${server_public}|" | \
sed -e "s#ALLOWEDIPS#$(echo ${vpn_network} | cut -d '.' -f -3).0/24, ${cust_network}#" | \
sed -e "s/SERVER/${server_addr}/" \
> /etc/wireguard/clients/$1/wg0.conf
echo "Created config!"
echo "Adding peer"
wg set wg0 peer $(cat /etc/wireguard/clients/$1/$1.pub) allowed-ips $ip/32
echo "Copy config to /home/${linux_user}/${cust_name}-${1}.conf"
cp /etc/wireguard/clients/$1/wg0.conf /home/${linux_user}/${cust_name}-${1}.conf
chown ${linux_user}:${linux_user} /home/${linux_user}/${cust_name}-${1}.conf
echo "Get the config via WinSCP"
echo "Clients:"
/usr/bin/wg-list-clients
echo "Restart wireguard:"
systemctl restart wg-quick@wg0 && echo "restart completed" && exit 0
echo "restart FAILED"
exit 1