Pi-Star How To: improve uPnP port forwarding

The Pi-Star default image includes a UPNP Service handler (/usr/local/sbin/pistar-upnp.service) which utilises the upnpc command to deploy port forwarding on the network router. Unfortunately the service handler in Pi-Star 4.1.2 has a few [minor] drawbacks:

  1. the port forwards are not deleted upon shutdown
  2. the port forwards not identified as originating from pi-star
  3. the port forwards do not expire 

I discovered this problem when the IP address of my pi-star changed after I changed the connection from wired to WiFi. When I checked the UPNP status on my router I found the port forwards for the old IP were still active.

To fix this problem I modified /usr/local/sbin/pistar-upnp.service as follows:

#!/bin/bash
#########################################################
#                                                       #
#            Pi-Star UPNP Service Handler               #
#                                                       #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
#               By Andy Taylor (MW0MWZ)                 #
#                                                       #
#                     Version 1.2                       #
#                                                       #
#########################################################

# Service Config
DAEMON=upnpc
DAEMON_PATH=/usr/bin/
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
ipVar=`hostname -I | cut -d' ' -f1`
DURATION=600
DESC=pistar

# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 1

# Check that the network is UP and die if its not
if [ "$(expr length `hostname -I | cut -d' ' -f1`x)" == "1" ]; then
    exit 0
fi

case "$1" in
    start)
#       $DAEMON -e $DESC -a $ipVar 22 22 TCP $DURATION > /dev/null 2>&1 &
        $DAEMON -e $DESC -a $ipVar 80 80 TCP $DURATION > /dev/null 2>&1 &
#       $DAEMON -e $DESC -a $ipVar 10022 10022 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 20001 20001 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30001 30001 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30051 30051 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30061 30061 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30062 30062 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30063 30063 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 30064 30064 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 40000 40000 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 42000 42000 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 42001 42001 UDP $DURATION > /dev/null 2>&1 &
                $DAEMON -e $DESC -a $ipVar 42010 42010 UDP $DURATION > /dev/null 2>&1 &
        ;;

    stop)
                $DAEMON -d 22 TCP > /dev/null 2>&1 &
                $DAEMON -d 80 TCP > /dev/null 2>&1 &
                $DAEMON -d 10022 UDP > /dev/null 2>&1 &
                $DAEMON -d 20001 UDP > /dev/null 2>&1 &
                $DAEMON -d 30001 UDP > /dev/null 2>&1 &
                $DAEMON -d 30051 UDP > /dev/null 2>&1 &
                $DAEMON -d 30061 UDP > /dev/null 2>&1 &
                $DAEMON -d 30062 UDP > /dev/null 2>&1 &
                $DAEMON -d 30063 UDP > /dev/null 2>&1 &
                $DAEMON -d 30064 UDP > /dev/null 2>&1 &
                $DAEMON -d 40000 UDP > /dev/null 2>&1 &
                $DAEMON -d 42000 UDP > /dev/null 2>&1 &
                $DAEMON -d 42001 UDP > /dev/null 2>&1 &
                $DAEMON -d 42010 UDP > /dev/null 2>&1 &
        ;;

    *)
        echo $"Usage: $0 {start|stop}"
        exit 1
esac

A word of warning:
Some routers require the UPNP parameter "DURATION" to be set to 0 (infinite rule expiry).

You can check your router's UPNP port mapping with the portmapper tool available for free from this location