Files
openclaw/bin/chromium-browser-eth1

68 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -euo pipefail
# Run chromium-browser with temporary per-host routes via eth1.
# For a passed URL, resolves target host (and www-host variant),
# adds temporary IPv4 routes via eth1, then removes them on exit.
VIA_GW="192.168.0.1"
DEV="eth1"
ROUTES=()
cleanup() {
for ip in "${ROUTES[@]:-}"; do
sudo ip route del "$ip" via "$VIA_GW" dev "$DEV" >/dev/null 2>&1 || true
done
}
trap cleanup EXIT INT TERM
extract_host() {
local url="$1"
python3 - "$url" <<'PY'
import sys
from urllib.parse import urlparse
u = sys.argv[1]
p = urlparse(u)
print((p.hostname or '').strip())
PY
}
add_routes_for_host() {
local host="$1"
[[ -z "$host" ]] && return 0
local ips
ips=$(getent ahostsv4 "$host" 2>/dev/null | awk '{print $1}' | sort -u || true)
[[ -z "$ips" ]] && return 0
while IFS= read -r ip; do
[[ -z "$ip" ]] && continue
sudo ip route replace "$ip" via "$VIA_GW" dev "$DEV"
ROUTES+=("$ip")
done <<< "$ips"
}
TARGET_URL=""
for arg in "$@"; do
if [[ "$arg" =~ ^https?:// ]]; then
TARGET_URL="$arg"
break
fi
done
if [[ -n "$TARGET_URL" ]]; then
HOST=$(extract_host "$TARGET_URL")
if [[ -n "$HOST" ]]; then
add_routes_for_host "$HOST"
# add alternate host variant for common redirects
if [[ "$HOST" == www.* ]]; then
add_routes_for_host "${HOST#www.}"
else
add_routes_for_host "www.$HOST"
fi
fi
fi
chromium-browser "$@"