Files
luci-app-owrt-remote/files/etc/hotplug.d/iface/99-owrt-remote
T

85 lines
2.2 KiB
Bash
Executable File

#!/bin/sh
PATH="/usr/sbin:/usr/bin:/sbin:/bin"
TAG="owrt-remote"
STATE_DIR="${TMPDIR:-/tmp}/owrt-remote-hotplug"
STAMP_FILE="$STATE_DIR/wan.stamp"
LAST_RESTART_FILE="$STATE_DIR/last-restart"
QUIET_SECONDS=18
RESTART_COOLDOWN=45
[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifupdate" ] || [ "$ACTION" = "ifrestart" ] || exit 0
case "$(uci -q get owrtremote.main.enabled 2>/dev/null || echo 0)" in
1|yes|true|on|enabled) ;;
*) exit 0 ;;
esac
log() {
logger -t "$TAG" "$*" 2>/dev/null || true
}
proto=""
if [ -n "$INTERFACE" ] && command -v ubus >/dev/null 2>&1; then
proto="$(
ubus -S call "network.interface.$INTERFACE" status 2>/dev/null | sed -n 's/.*"proto"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1
)"
fi
case "$INTERFACE:$DEVICE:$proto" in
wan:*:*|wan6:*:*|*:pppoe*:*|*:ppp*:*|*:*:pppoe|*:*:ppp) ;;
*) exit 0 ;;
esac
network_ready() {
if command -v ip >/dev/null 2>&1 && ip route get 1.1.1.1 >/dev/null 2>&1; then
return 0
fi
route -n 2>/dev/null | awk '$4 ~ /G/ { found = 1 } END { exit found ? 0 : 1 }'
}
safe_num() {
case "$1" in
''|*[!0-9]*) printf '%s' "$2" ;;
*) printf '%s' "$1" ;;
esac
}
mkdir -p "$STATE_DIR"
stamp="$(date +%s 2>/dev/null || echo 0)-$$-${ACTION:-event}-${INTERFACE:-iface}-${DEVICE:-dev}"
printf '%s\n' "$stamp" >"$STAMP_FILE"
(
i=0
sleep "$QUIET_SECONDS"
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
while [ "$i" -lt 30 ]; do
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
network_ready && break
i=$((i + 1))
sleep 1
done
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
sleep 2
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
now="$(safe_num "$(date +%s 2>/dev/null || echo 0)" 0)"
last_restart="$(safe_num "$(cat "$LAST_RESTART_FILE" 2>/dev/null)" 0)"
if [ "$now" -gt 0 ] && [ $((now - last_restart)) -lt "$RESTART_COOLDOWN" ]; then
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restart skipped by cooldown"
exit 0
fi
printf '%s\n' "$now" >"$LAST_RESTART_FILE"
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel"
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
/usr/sbin/owrt-remote heartbeat >/dev/null 2>&1 || true
else
log "restart failed"
fi
) &
exit 0