// OnTcpStop
// This script is executed when a TCP port is closed.

// $listeningPort is the port you specified in "ports" list
// $redirectPort is assigned at runtime when the tcp server is started. First port start at 9000 and is incremented for each server
// $isForward : true if is just port forwarding. False if it's reverse tethering
// $isSocks5 : if reverse tethering, indicate if socks5 must be used.
// $isAtRunTime : server is created at run time (global redirect)
// all theses variables are read only

$LogI --------- Script : Tcp port stop : $listeningPort / $redirectPort

if $isForward  then
   return
fi

if $isAtRunTime then 
   return
fi

// reverse tethering

if $isSocks5 = false then
   // send tcp connection to the tunnel (9000+)
   $iptables -t nat -D TETHER -o $myInterface -p tcp --dport $listeningPort -j REDIRECT --to $redirectPort  
else
   // Socks5 mode : redirection is done on redsocks client port : 42002
   // redsocks will communicate with Proxyserver (port 42003) that return the tunnel port (9000+)
   $iptables -t nat -D TETHER -o $myInterface -p tcp --dport $listeningPort -j REDIRECT --to 42002
fi

$LogI --------- Script : Tcp port stop done
