docker-wireguard/root/app/add-peer

46 lines
1.4 KiB
Plaintext
Raw Normal View History

2020-03-31 03:59:44 +00:00
#!/usr/bin/with-contenv bash
2020-04-08 14:46:14 +00:00
if [ ! -f /config/wg0.conf ] || [ -z "$PEERS" ]; then
2020-03-31 03:59:44 +00:00
echo "Wireguard is not set up in server mode"
exit 0
fi
2020-04-08 14:46:14 +00:00
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
SERVERURL=$(curl icanhazip.com)
fi
SERVERPORT=${SERVERPORT:-51820}
2020-04-08 17:32:43 +00:00
if [ -z "$PEERDNS" ] || [ "$PEERDNS" = "auto" ]; then
PEERDNS="${INTERFACE}.1"
fi
2020-04-08 14:46:14 +00:00
2020-03-31 03:59:44 +00:00
for i in {1..254}; do
2020-04-08 14:46:14 +00:00
if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then
2020-03-31 03:59:44 +00:00
echo "Peer $i exists"
else
echo "Adding new Peer $i"
mkdir -p /config/peer${i}
if [ ! -f /config/peer${i}/privatekey-peer${i} ]; then
umask 077
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i}
fi
2020-04-08 14:46:14 +00:00
eval "`printf %s`
2020-03-31 03:59:44 +00:00
cat <<DUDE > /config/peer${i}/peer${i}.conf
2020-04-08 14:46:14 +00:00
`cat /config/templates/peer.conf`
DUDE"
2020-03-31 03:59:44 +00:00
cat <<DUDE >> /config/wg0.conf
[Peer]
PublicKey = $(cat /config/peer${i}/publickey-peer${i})
2020-04-08 14:46:14 +00:00
AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32
2020-03-31 03:59:44 +00:00
DUDE
echo "PEER ${i} QR code:"
qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf
qrencode -o /config/peer${i}/peer${i}.png < /config/peer${i}/peer${i}.conf
chown -R abc:abc /config/peer${i}
s6-svc -t /var/run/s6/services/wireguard
break
fi
done