#!/bin/bash
# Ensure SSH tunnel to msd5685 vLLM (Qwen3-8B) is running on localhost:8000
# Port 8000 is firewalled on the box — tunnel is required.

HOST="msd5685.mjhst.com"
LOCAL_PORT=8000
REMOTE_PORT=8000
SSH_KEY="$HOME/.ssh/id_rsa"
SSH_USER="ubuntu"

# Check if tunnel is already up
if lsof -i ":$LOCAL_PORT" -sTCP:LISTEN &>/dev/null; then
  echo "vLLM tunnel already active on localhost:$LOCAL_PORT"
else
  echo "Starting SSH tunnel to $HOST..."
  ssh -f -N -L "$LOCAL_PORT:localhost:$REMOTE_PORT" \
    -i "$SSH_KEY" \
    -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null \
    -o ServerAliveInterval=60 \
    -o ServerAliveCountMax=3 \
    "$SSH_USER@$HOST"

  if lsof -i ":$LOCAL_PORT" -sTCP:LISTEN &>/dev/null; then
    echo "vLLM tunnel active → localhost:$LOCAL_PORT"
  else
    echo "Failed to start tunnel" >&2
    exit 1
  fi
fi
