#!/bin/bash
apt update && apt install wireguard-dkms wireguard-tools net-tools openresolv mawk grep iproute2 qrencode iptables-persistent -y
umask 077
echo "wireguard" | sudo tee -a /etc/modules 
modprobe wireguard
systemctl enable wg-quick@wg0.service 
cd /etc/wireguard
wget https://raw.githubusercontent.com/burghardt/easy-wg-quick/master/easy-wg-quick
chmod +x easy-wg-quick
./easy-wg-quick
ln -s wghub.conf wg0.conf
./easy-wg-quick client1
./easy-wg-quick client2
./easy-wg-quick client3
./easy-wg-quick client4
./easy-wg-quick client5
systemctl restart wg-quick@wg0.service
mkdir /opt/wg-conf 
mv wgclient_client1.conf /opt/wg-conf/client1.conf
mv wgclient_client2.conf /opt/wg-conf/client2.conf
mv wgclient_client3.conf /opt/wg-conf/client3.conf
mv wgclient_client4.conf /opt/wg-conf/client4.conf
mv wgclient_client5.conf /opt/wg-conf/client5.conf
echo "das ist der UDP Port der geoeffnet werden muss"
wg show | grep port 
sed -i '/net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf
sed -i '/net.ipv6.conf.all.forwarding=1/s/^#//g' /etc/sysctl.conf
iptables -t nat -A POSTROUTING -o wg0 -j MASQUERADE 
iptables -A FORWARD -i wg0 -o $(arp -n -H ether | perl -ne 'print $1 if /(\S+\n)$/' | sort | uniq -c | grep -v Iface | sort -n | tail -1 | perl -pe 's/.* //') -m state --state RELATED,ESTABLISHED -j ACCEPT 
iptables -A FORWARD -i $(arp -n -H ether | perl -ne 'print $1 if /(\S+\n)$/' | sort | uniq -c | grep -v Iface | sort -n | tail -1 | perl -pe 's/.* //') -o wg0 -j ACCEPT
netfilter-persistent save

exit 0