#!/bin/sh
# Starchaser route guard: a network came, went or changed its address, so the rules that keep traffic to the
# networks this computer is on out its own cable or Wi-Fi (ahead of any VPN's route to the same network) are
# brought up to date (route_guard.py). NetworkManager runs this as root with: $1 = interface, $2 = action.
# Also linked from pre-up.d, so the rule is there before NetworkManager reports the network connected.
# A second pass a few seconds later catches an address that lands just after the event (a reapply did that in a
# test: its IPv4 address was set after this ran); the guard is idempotent, so a pass with nothing new does nothing.
G=/usr/local/lib/starchaser/starchaser-admin
case "$2" in
    pre-up)
        exec "$G" route-guard-refresh >/dev/null ;;
    up|down|vpn-up|vpn-down|dhcp4-change|dhcp6-change|reapply)
        "$G" route-guard-refresh >/dev/null
        setsid sh -c "sleep 3; exec $G route-guard-refresh >/dev/null" </dev/null >/dev/null 2>&1 &
        ;;
esac
exit 0
