| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | #!/bin/bash# Setup keys, psk and config for a central wireguard server including peers# Dependencies# apk add wireguard-tools libqrencode### References# https://wiki.archlinux.org/title/WireGuard# Source global variablesconfdir='./config'source ./config/server.cfg# ---------Functions-----------## Create key-pairwgkeypair() { wg genkey | (umask 0077 && tee   $1.key) | wg pubkey > $1.pub; }## Create pskwgpsk()     { wg genpsk | (umask 0077 && cat > $1.psk) } ## Create peer config filewgconfig() {  peer=$1  ip=$2  cat >$peer.conf <<EOL# Client config for $peer[Interface]PrivateKey = `cat $peer.key`Address = $ipDNS = $dns [Peer]PublicKey = `cat $servername.pub`AllowedIPs = $networkEndpoint = $servername:$serverportPersistentKeepalive = 15PresharedKey = `cat $peer.psk`EOL  qrencode  -t ANSI256 -r $peer.conf > $peer-qr.txt  qrencode  -t png -r $peer.conf -o $peer.png}# ---------Functions-----------## Create and move to output foldermkdir -p $confdircd $confdir## Create server keyswgkeypair $servername## Create server configcat >$servername.conf <<EOL# Config for $peer[Interface]PrivateKey = `cat $servername.key`Address = $serveripDNS = $dnsEOL## Create peers' key-pair, psk and config from peers.cfgwhile read peer; do     ip=`echo $peer | cut -d' ' -f1`   host=`echo $peer | cut -d' ' -f2`      wgkeypair $host   wgpsk $host   wgconfig $host $ip   # Add peers to server config      cat >>$servername.conf <<EOL[Peer]PublicKey = `cat $host.pub`AllowedIPs = $ipPresharedKey = `cat $host.psk`EOL done < peers.cfg
 |