scripts/wireguard-generator.sh

44 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
base_config="wg0.conf"
base_ip="192.168.57."
start_ip=110
server_config="wg0-server.conf"
# First create server base config
cat > "$server_config" << 'EOF'
[Interface]
Address = 192.168.57.2/24
SaveConfig = false
PostUp = ip route add 192.168.56.0/24 dev vboxnet0; iptables -A FORWARD -i wg0 -o vboxnet0 -j ACCEPT; iptables -A FORWARD -i vboxnet0 -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o vboxnet0 -j MASQUERADE
PostDown = ip route del 192.168.56.0/24 dev vboxnet0; iptables -D FORWARD -i wg0 -o vboxnet0 -j ACCEPT; iptables -D FORWARD -i vboxnet0 -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o vboxnet0 -j MASQUERADE
ListenPort = 51820
PrivateKey = SJuOEDojaWclA3vyq1tI3fKv+Phs9QdvUFG7mZj23VU=
EOF
# Create 30 configs
for i in {1..30}; do
ip=$((start_ip + i))
new_config="wg0_$ip.conf"
wg genkey | tee "${ip}privatekey" | wg pubkey | tee "${ip}publickey"
pub=$(cat ${ip}publickey)
priv=$(cat "${ip}privatekey")
echo $pub
# Create new client config with all replacements
sed -E "s#(Address = ${base_ip})[0-9]+#\1${ip}#;
s#(PrivateKey = ).*#\1${priv}#" "$base_config" > "$new_config"
# Add peer to server config with the new public key
cat >> "$server_config" << EOF
[Peer]
PublicKey = $pub
AllowedIPs = 192.168.57.${ip}/32
EOF
echo "Created $new_config with IP ${base_ip}${ip}"
done
echo "Created $server_config with all peer public keys"