#!/usr/bin/env bash SERVER="$1" USERNAME="$2" PASSWORD="$3" PORT="443" VPN_DNS="10.10.10.10" PPP_INTERFACE="ppp0" if [ -z "$SERVER" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then echo "Usage: sudo $0 SERVER USERNAME PASSWORD" echo echo "Example:" echo "sudo $0 pl2v.secwhapi.net sec1925733 YOUR_PASSWORD" exit 1 fi # Resolve VPN server hostname to IPv4 address SERVER_IP=$(getent ahostsv4 "$SERVER" | awk 'NR==1 {print $1}') if [ -z "$SERVER_IP" ]; then echo "Error: Could not resolve VPN server: $SERVER" exit 1 fi # Detect current default route DEFAULT_ROUTE=$(ip route show default | head -n1) 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 "Error: Could not determine current gateway or network interface." exit 1 fi echo echo "WhoVPN SSTP connection" echo "----------------------" echo "Server: $SERVER" echo "Server IP: $SERVER_IP" echo "Port: $PORT" echo "Physical interface: $INTERFACE" echo "Gateway: $GATEWAY" echo "VPN DNS: $VPN_DNS" echo # Keep VPN server reachable through the original connection. # This prevents the SSTP tunnel from trying to route itself through ppp0. ip route replace "$SERVER_IP" via "$GATEWAY" dev "$INTERFACE" SSTP_PID="" cleanup() { echo echo "Disconnecting WhoVPN..." # Stop SSTP if it is still running if [ -n "$SSTP_PID" ]; then kill "$SSTP_PID" 2>/dev/null || true wait "$SSTP_PID" 2>/dev/null || true fi # Restore original default route ip route replace default via "$GATEWAY" dev "$INTERFACE" # Remove temporary route to VPN server ip route del "$SERVER_IP" via "$GATEWAY" dev "$INTERFACE" 2>/dev/null || true # Remove DNS configuration from ppp0 if it still exists if ip link show "$PPP_INTERFACE" >/dev/null 2>&1; then resolvectl revert "$PPP_INTERFACE" 2>/dev/null || true fi echo "Network settings restored." } trap cleanup EXIT INT TERM # Start SSTP connection sstpc \ "$SERVER:$PORT" \ --user "$USERNAME" \ --password "$PASSWORD" \ require-mschap-v2 \ noauth \ noipdefault & SSTP_PID=$! echo "Connecting to WhoVPN..." echo "Waiting for PPP interface..." # Wait up to 30 seconds for ppp0 for i in $(seq 1 30); do if ip link show "$PPP_INTERFACE" >/dev/null 2>&1; then break fi # Check if sstpc exited unexpectedly if ! kill -0 "$SSTP_PID" 2>/dev/null; then echo "Error: SSTP connection terminated before PPP interface was created." exit 1 fi sleep 1 done if ! ip link show "$PPP_INTERFACE" >/dev/null 2>&1; then echo "Error: PPP interface was not created." exit 1 fi echo "PPP interface detected: $PPP_INTERFACE" # Route all normal traffic through SSTP ip route replace default dev "$PPP_INTERFACE" # Configure WhoVPN DNS resolvectl dns "$PPP_INTERFACE" "$VPN_DNS" # Make ppp0 the preferred interface for all DNS queries resolvectl domain "$PPP_INTERFACE" '~.' echo echo "WhoVPN connected successfully." echo echo "VPN interface: $PPP_INTERFACE" echo "DNS server: $VPN_DNS" echo echo "Check your IP with:" echo "curl -4 https://api.ipify.org" echo echo "Check DNS configuration with:" echo "resolvectl status $PPP_INTERFACE" echo echo "Press Ctrl+C to disconnect." # Keep script running while SSTP is connected wait "$SSTP_PID"