#!/bin/sh

SUBNET=10.255.255.0
MASK=24

NETNS=hils-oregon-1
VETH0=hilsveth0
VETH1=hilsveth1

BR0_IP=10.255.255.1
VETH0_IP=10.255.255.10
VETH1_IP=10.255.255.15

FWD_VALUE=$(sysctl -n net.ipv4.ip_forward)

WGCONF="/etc/wireguard/yourwireguard.conf" 

# create bridge
ip link add br0 type bridge
ip address add $BR0_IP/$MASK dev br0
ip link set br0 up

# creating a virtual ethernet interface
ip netns add $NETNS
ip link add $VETH0 netns $NETNS type veth peer name $VETH1 netns 1
ip link set $VETH1 master br0

ip -n $NETNS address add $VETH0_IP/$MASK dev $VETH0
ip -n $NETNS address add 127.0.0.1/8 dev lo
ip -n $NETNS link set lo up
ip -n $NETNS link set $VETH0 up

# adding default routes
ip -n $NETNS route add $BR0_IP via $VETH0_IP
ip -n $NETNS route add default via $BR0_IP dev $VETH0

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -s $SUBNET/$MASK ! -o br0 -j MASQUERADE

ip netns exec $NETNS wg-quick up $WGCONF

ip netns exec $NETNS su -m $SUDO_USER $DOAS_USER

# undo everything after the shell session
ip netns exec $NETNS wg-quick down $WGCONF
ip link delete $VETH1
ip link delete br0
ip netns del $NETNS
iptables -t nat -D POSTROUTING -s 10.255.255.0/24 ! -o br0 -j MASQUERADE
echo $FWD_VALUE > /proc/sys/net/ipv4/ip_forward

