#!/bin/bash
# Aranym Point2Point with Internet

#read config file
source /etc/net-aranymp2p.conf

start() {
echo $host_if
      if [ "$ip4_fwd" == "enable" ];then
      sysctl -w net.ipv4.ip_forward=1 2>/dev/null 
      fi
       
      ip tuntap add tap0 mode tap group netdev
      ip link set tap0 up
      ip a add $host_ip peer $aranym_ip dev tap0
      
      if [ "$fw_set" == "enable" ];then
      iptables -A FORWARD -o $host_if -i tap0 -m conntrack --ctstate NEW -j ACCEPT
      iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT 
      iptables -t nat -A POSTROUTING -o $host_if -j MASQUERADE
      fi
}

stop() {
      ip link set dev tap0 down
      ip link delete tap0
      if [ "$fw_set" == "enable" ];then
      iptables -D FORWARD -o $host_if -i tap0 -m conntrack --ctstate NEW -j ACCEPT
      iptables -D FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
      iptables -t nat -D POSTROUTING -o $host_if -j MASQUERADE
      fi
}

case "$1" in 
    start)
      start	
      ;;
    stop)
      stop
      ;;
    reload)
       stop
       start
       ;;
    status)
      ;;
    *)
      echo "Usage: $0 {start|stop|status|reload}"
esac

exit 0 
