#!/bin/sh
chmod +x add_client.sh

echo "Setting up Wireguard ..."
apk add wireguard-tools wireguard-tools-openrc

# Generate server private and public keys
mkdir -p /etc/wireguard
wg genkey | tee /etc/wireguard/server_priv.key | wg pubkey > /etc/wireguard/server_pub.key

# Generate configuration
cat <<EOF > /etc/wireguard/wg0.conf
[Interface]
PrivateKey = $(cat /etc/wireguard/server_priv.key)
Address = 10.0.0.1/24 # Server has IP in the wg network
ListenPort = 51820
EOF

# Kernel module
modprobe wireguard
echo wireguard >> /etc/modules

# Enable IP forwarding, persistent
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.d/ip_forward.conf
echo "net.ipv6.conf.all.forwarding=1" >> /etc/sysctl.d/ip_forward.conf
sysctl -p /etc/sysctl.d/ip_forward.conf

# Auto-start Wireguard on boot
ln -s /etc/init.d/wg-quick /etc/init.d/wg-quick.wg0
rc-update add wg-quick.wg0
rc-service wg-quick.wg0 start