wireguard/templates/add_client.sh.j2
2022-07-27 12:10:21 +02:00

45 lines
No EOL
1.1 KiB
Django/Jinja

# add_client <IP>
IP=$1
# If a client config file is already present, return
if [[ -f /etc/wireguard/client/wg0.conf ]]
then
echo "client/wg0.conf already exists. Please consider moving it to another place or deleting it"
return 1
fi
# generate client key pair
private_key=$(wg genkey)
public_key=$(echo $private_key | wg pubkey)
# write updated server config to temp-file
cp /etc/wireguard/wg0.conf /etc/wireguard/wg0-temp.conf
echo "
[Peer]
PublicKey = $public_key
AllowedIPs = $IP/32
" >> /etc/wireguard/wg0-temp.conf
# restart wireguard with updated conf
wg-quick down wg0
mv /etc/wireguard/wg0-temp.conf /etc/wireguard/wg0.conf
wg-quick up wg0
mkdir /etc/wireguard/client
# generate client config file
echo "[Interface]
Address = $IP/24
PrivateKey = $private_key
MTU = 1280
DNS = 192.168.2.1
[Peer]
PublicKey = {{ wireguard_server_pubkey }}
AllowedIPs = 0.0.0.0/0
Endpoint = {{ wireguard_server_endpoint }}:51900
PersistentKeepalive = 30
" > /etc/wireguard/client/wg0.conf
# output client conf as QR-Code
qrencode -r /etc/wireguard/client/wg0.conf -t ansiutf8