jean-cloud-services/provisioning/roles/deploy_all/files/bin/gen_wgconf.sh
2024-10-16 11:41:44 +02:00

73 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# This script will generate a single config for a given host and privkey
# Limits:
# 253 peers 1 -> 254
# Lib
. /usr/local/bin/driglibash-base
# Arg parsing
if [ "$#" -ne 4 ] ; then
die "Usage: $0 <name> <privkey_filepath> <port> <net_prefix>\nNet is a /24"
fi
thisname="$1"
privkey="$2"
port="$3"
net="$4"
# script
content="$(mktemp)"
clean "rm $content"
while IFS=";" read id name location isp note pubkey ip1 ip2 ip3 ip4 ip5 ; do
# Skip header
[ "$id" = "id" ] && continue
# Add local config
if [ "$name" = "$thisname" ] ; then
cat <<-EOF
[Interface] # $name
PrivateKey = $(cat "$privkey")
ListenPort = $port
Address = $net.$id/32
EOF
else
# Create list of endpoints
endpoints=""
for i in $(seq 1 5) ; do
varname="ip$i"
ip="${!varname}"
echo "$ip" | grep -q ':' && ip="[$ip]"
if [ -n "$ip" ] ; then
endpoints="$endpoints"$'\n'"Endpoint = $ip:$port"
fi
done
# Correct endpoint value or add keepalive for endpointless hosts
if [ -n "$endpoints" ] ; then
endpoints="${endpoints:1}"
else
endpoints="PersistentKeepalive = 30"
fi
# Peer config
cat >> "$content" <<-EOF
[Peer] # $name
PublicKey = $pubkey
AllowedIPs = $net.$id/32
$endpoints
EOF
fi
done < $DOCKER_DIR/servers.csv
cat "$content"
rm "$content"