#!/usr/bin/env bash
apt update
apt install -y wireguard-tools
# apt install -y wireguard-dkms


LISTENPORT=51820
PEERPORT=52037
SAVECONFIG=true
# ADDRESS="10.0.0.5/24"
ADDRESS="192.168.99.2/24"
# ALLOWED="10.0.0.1/32"
# ALLOWED="192.168.99.0/32"
ALLOWED="0.0.0.0/0"
DNS="1.1.1.1"
# PEER="tagmar.net"
PEER="ring-zero.co.uk"
KEEPALIVE=25
# PRIVATEKEY=$(wg genkey)
PRIVATEKEY=/etc/wireguard/private.key
# PUBLICKEY=$(echo ${PRIVATEKEY} | wg pubkey)
PUBLICKEY=/etc/wireguard/public.key
PEERPUBLIC="IUV3V1SwE1b+6HbJPoVg5XGrkSS8re3oV4JamDbmrC0="

#(umask 077 && printf "[Interface]\nPrivateKey = " | sudo tee /etc/wireguard/wg0.conf > /dev/null)
#wg genkey | sudo tee -a /etc/wireguard/wg0.conf | wg pubkey | sudo tee /etc/wireguard/publickey

mkdir -p /etc/wireguard
chmod 700 /etc/wireguard
cd /etc/wireguard
umask 077
sudo wg genkey | sudo tee private.key | wg pubkey | sudo tee public.key

cat > /etc/wireguard/wg0.conf<<"EOF"
[Interface]
PrivateKey = ${PRIVATEKEY}
ListenPort = ${LISTENPORT}
DNS = ${DNS}
SaveConfig = ${SAVECONFIG}

Address = ${ADDRESS}


[Peer]
PublicKey = ${PEERPUBLIC}
Endpoint = ${PEER}:${PEERPORT}
AllowedIPs = ${ALLOWED}
PersistantKeepalive = ${KEEPALIVE}
EOF

echo ${PUBLICKEY} > /etc/wireguard/publickey


