Add Harbor Gateway auto-detection for client routing
This commit is contained in:
46
scripts/harbor-network-monitor.sh
Normal file
46
scripts/harbor-network-monitor.sh
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
export LC_ALL=C
|
||||
|
||||
RUNTIME_DIR="${HARBOR_RUNTIME_DIR:-$HOME/.vpn-proxy-client/.runtime}"
|
||||
STATE_FILE="$RUNTIME_DIR/network.json"
|
||||
ROUTE_BIN="${HARBOR_ROUTE_BIN:-/sbin/route}"
|
||||
ARP_BIN="${HARBOR_ARP_BIN:-/usr/sbin/arp}"
|
||||
NETSTAT_BIN="${HARBOR_NETSTAT_BIN:-/usr/sbin/netstat}"
|
||||
|
||||
route_info="$($ROUTE_BIN -n get default 2>/dev/null || true)"
|
||||
gateway="$(awk '/^[[:space:]]*gateway:/{print $2; exit}' <<<"$route_info")"
|
||||
network_interface="$(awk '/^[[:space:]]*interface:/{print $2; exit}' <<<"$route_info")"
|
||||
|
||||
if [[ -z "$gateway" || -z "$network_interface" ]]; then
|
||||
read -r gateway network_interface < <(
|
||||
"$NETSTAT_BIN" -rn -f inet 2>/dev/null \
|
||||
| awk '$1 == "default" && $2 ~ /^[0-9]+\./ { print $2, $4; exit }'
|
||||
) || true
|
||||
fi
|
||||
|
||||
if [[ ! "$gateway" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
|
||||
gateway=""
|
||||
network_interface=""
|
||||
fi
|
||||
if [[ ! "$network_interface" =~ ^[a-zA-Z0-9._-]{1,32}$ ]]; then
|
||||
network_interface=""
|
||||
fi
|
||||
|
||||
mac=""
|
||||
if [[ -n "$gateway" ]]; then
|
||||
mac="$($ARP_BIN -n "$gateway" 2>/dev/null | awk '/ at /{print $4; exit}' || true)"
|
||||
if [[ ! "$mac" =~ ^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$ ]]; then
|
||||
mac=""
|
||||
else
|
||||
mac="$(printf '%s' "$mac" | tr '[:upper:]' '[:lower:]')"
|
||||
fi
|
||||
fi
|
||||
|
||||
mkdir -p "$RUNTIME_DIR"
|
||||
tmp="$(mktemp "${STATE_FILE}.XXXXXX")"
|
||||
trap 'rm -f "$tmp"' EXIT
|
||||
printf '{"gateway":"%s","interface":"%s","mac":"%s","observedAt":"%s"}\n' \
|
||||
"$gateway" "$network_interface" "$mac" "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" > "$tmp"
|
||||
mv "$tmp" "$STATE_FILE"
|
||||
Reference in New Issue
Block a user