diff options
| author | arpitchakladar <arpitchakladar@gmail.com> | 2026-09-09 23:29:20 +0530 |
|---|---|---|
| committer | arpitchakladar <arpitchakladar@gmail.com> | 2026-09-09 23:29:20 +0530 |
| commit | d2e6e6b5de1d56d53aee2da81c2e9ee553971c72 (patch) | |
| tree | feee7479f6b4574696cc6d780f55ce044cafa3bc /modules/networking/usque | |
| parent | 7879122dfc8e7ca7b905e10d5f55df09f9051295 (diff) | |
| download | home-manager-config-d2e6e6b5de1d56d53aee2da81c2e9ee553971c72.tar.gz home-manager-config-d2e6e6b5de1d56d53aee2da81c2e9ee553971c72.zip | |
refactor(modules): consistent message formats across all scripts
Diffstat (limited to 'modules/networking/usque')
| -rw-r--r-- | modules/networking/usque/usque-warp.sh | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/modules/networking/usque/usque-warp.sh b/modules/networking/usque/usque-warp.sh index 9d32872..8531859 100644 --- a/modules/networking/usque/usque-warp.sh +++ b/modules/networking/usque/usque-warp.sh @@ -1,5 +1,11 @@ #!/usr/bin/env bash set -euo pipefail + +info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } +warn() { printf '\033[1;33m==> warning:\033[0m %s\n' "$*" >&2; } +error() { printf '\033[1;31m==> error:\033[0m %s\n' "$*" >&2; } +die() { error "$*"; exit 1; } + CONFIG_DIR="$HOME/.cache/usque" CONFIG="$CONFIG_DIR/config.json" RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp}" @@ -29,22 +35,21 @@ is_running() { } ensure_config() { - echo "Creating $CONFIG_DIR..." + info "Creating $CONFIG_DIR..." mkdir -p "$CONFIG_DIR" - echo "Registering Cloudflare WARP account..." + info "Registering Cloudflare WARP account..." usque -c "$CONFIG" register < <(yes) if [[ ! -f "$CONFIG" ]]; then - echo "Failed to create config file: $CONFIG" - exit 1 + die "Failed to create config file: $CONFIG" fi - echo "Config created successfully." + info "Config created successfully." } remove_tun_default_routes() { local dev="$1" while ip route show | grep -qE "^default .*dev $dev"; do ROUTE=$(ip route show | grep -E "^default .*dev $dev" | head -n1) - echo "Removing route: $ROUTE" + info "Removing route: $ROUTE" sudo ip route del "$ROUTE" || break done } @@ -55,30 +60,28 @@ connect() { if [[ -f "$PID_FILE" ]]; then OLD_PID=$(cat "$PID_FILE") if sudo kill -0 "$OLD_PID" 2>/dev/null; then - echo "usque-warp is already running (PID $OLD_PID)" - exit 1 + die "usque-warp is already running (PID $OLD_PID)" else - echo "Removing stale PID file..." + info "Removing stale PID file..." rm -f "$PID_FILE" fi fi - echo "Saving current default route..." + info "Saving current default route..." DEFAULT_ROUTE=$(ip route show default | grep -vE 'dev tun[0-9]+' | head -n1) if [[ -z "$DEFAULT_ROUTE" ]]; then - echo "Could not determine current default route" - exit 1 + die "Could not determine current default route" fi echo "$DEFAULT_ROUTE" > "$STATE_FILE" - echo "Recording pre-existing tun interfaces..." + info "Recording pre-existing tun interfaces..." BEFORE_IFACES=$(list_tun_ifaces) - echo "Starting usque..." + info "Starting usque..." sudo usque nativetun -c "$CONFIG" 2>&1 | sudo tee "$LOG_FILE" >/dev/null & echo $! > "$PID_FILE" - echo "Waiting for MASQUE connection..." + info "Waiting for MASQUE connection..." MASQUE_IP="" for _ in {1..30}; do MASQUE_IP=$(grep -oP 'MASQUE connection to \K[0-9.]+(?=:443)' "$LOG_FILE" 2>/dev/null || true) @@ -88,13 +91,13 @@ connect() { sleep 1 done if [[ -z "$MASQUE_IP" ]]; then - echo "Failed to detect MASQUE endpoint" + error "Failed to detect MASQUE endpoint" sudo kill "$(cat "$PID_FILE")" 2>/dev/null || true rm -f "$PID_FILE" exit 1 fi - echo "Waiting for usque interface..." + info "Waiting for usque interface..." TUN_DEV="" for _ in {1..30}; do AFTER_IFACES=$(list_tun_ifaces) @@ -103,39 +106,38 @@ connect() { sleep 1 done if [[ -z "$TUN_DEV" ]]; then - echo "Failed to detect usque interface" + error "Failed to detect usque interface" sudo kill "$(cat "$PID_FILE")" 2>/dev/null || true rm -f "$PID_FILE" exit 1 fi echo "$TUN_DEV" > "$IFACE_FILE" - echo "Detected interface: $TUN_DEV" + info "Detected interface: $TUN_DEV" GATEWAY=$(echo "$DEFAULT_ROUTE" | awk '{for(i=1;i<=NF;i++) if($i=="via") print $(i+1)}') INTERFACE=$(echo "$DEFAULT_ROUTE" | awk '{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}') if [[ -z "$GATEWAY" || -z "$INTERFACE" ]]; then - echo "Cannot determine gateway/interface" - exit 1 + die "Cannot determine gateway/interface" fi echo "MASQUE_IP=$MASQUE_IP GATEWAY=$GATEWAY INTERFACE=$INTERFACE" >> "$STATE_FILE" - echo "Allowing MASQUE endpoint outside tunnel..." + info "Allowing MASQUE endpoint outside tunnel..." sudo ip route replace \ "$MASQUE_IP" \ via "$GATEWAY" \ dev "$INTERFACE" - echo "Removing old tun routes..." + info "Removing old tun routes..." remove_tun_default_routes "$TUN_DEV" - echo "Switching default route to $TUN_DEV..." + info "Switching default route to $TUN_DEV..." sudo ip route add default dev "$TUN_DEV" metric 1 - echo "Connected" + info "Connected." } disconnect() { sudo -v - echo "Disconnecting..." + info "Disconnecting..." local dev if [[ -f "$IFACE_FILE" ]]; then dev=$(cat "$IFACE_FILE") @@ -149,7 +151,7 @@ disconnect() { if [[ -f "$PID_FILE" ]]; then PID=$(cat "$PID_FILE") if sudo kill -0 "$PID" 2>/dev/null; then - echo "Stopping usque..." + info "Stopping usque..." sudo kill "$PID" 2>/dev/null || true for _ in {1..20}; do sudo kill -0 "$PID" 2>/dev/null || break @@ -162,7 +164,7 @@ disconnect() { # Belt-and-braces: if the interface (or any of its routes) somehow # survived, clean them up explicitly. No-ops if tun0 is already gone. if [[ -n "${dev:-}" ]]; then - echo "Flushing $dev routes..." + info "Flushing $dev routes..." sudo ip route flush dev "$dev" 2>/dev/null || true remove_tun_default_routes "$dev" fi @@ -170,7 +172,7 @@ disconnect() { if [[ -f "$STATE_FILE" ]]; then MASQUE_IP=$(grep -oP 'MASQUE_IP=\K[0-9.]+' "$STATE_FILE" || true) if [[ -n "$MASQUE_IP" ]]; then - echo "Removing MASQUE route: $MASQUE_IP" + info "Removing MASQUE route: $MASQUE_IP" sudo ip route del "$MASQUE_IP" 2>/dev/null || true fi @@ -178,16 +180,16 @@ disconnect() { # assuming it's still intact. 'replace' is idempotent. ORIGINAL_DEFAULT=$(head -n1 "$STATE_FILE") if [[ "$ORIGINAL_DEFAULT" == default* ]]; then - echo "Restoring original default route..." + info "Restoring original default route..." sudo ip route replace "$ORIGINAL_DEFAULT" \ - || echo "Warning: could not restore original default route" + || warn "could not restore original default route" fi rm -f "$STATE_FILE" fi rm -f "$IFACE_FILE" - echo "Disconnected" + info "Disconnected." } status() { @@ -217,7 +219,12 @@ case "${1:-}" in status ;; *) - echo "Usage: $0 {connect|disconnect|status}" + echo "Usage: $0 <connect|disconnect|status>" + echo "" + echo "Commands:" + echo " connect Start the WARP tunnel" + echo " disconnect Stop the WARP tunnel and restore routes" + echo " status Print Waybar status JSON" exit 1 ;; -esac +esac
\ No newline at end of file |
