Skip to content

Instantly share code, notes, and snippets.

@kokoye2007
Created January 10, 2025 14:51
Show Gist options
  • Save kokoye2007/08cec2704132ec7ce247ce58d79967ac to your computer and use it in GitHub Desktop.
Save kokoye2007/08cec2704132ec7ce247ce58d79967ac to your computer and use it in GitHub Desktop.
This Bash script automates connecting to free VPNBook servers by fetching available servers, extracting configurations, and handling authentication, allowing you to securely connect with just a few selections.
#!/bin/bash
# Ko Ko Ye - [email protected]
# vpnbook.com
# deepseek.com
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check for required commands (wget, openvpn, gocr)
for cmd in wget openvpn gocr; do
if ! command_exists "$cmd"; then
echo "Error: $cmd is not installed."
exit 1
fi
done
# Generate timestamp for password image URL
TIMESTAMP=$(date +"%Y%m%d%H%M")
# Base URL for VPNBook
BASE_URL="https://www.vpnbook.com"
VPN_PAGE_URL="${BASE_URL}/freevpn"
# Download the VPN page HTML to fetch server links
echo "Fetching available VPN servers from $VPN_PAGE_URL..."
VPN_PAGE_HTML=$(wget -qO- "$VPN_PAGE_URL")
# Extract server links from the HTML using regex
SERVER_LINKS=$(echo "$VPN_PAGE_HTML" | grep -oP 'href="\K/free-openvpn-account/vpnbook-openvpn-[^"]+\.zip')
# Check if any server links were found
if [[ -z "$SERVER_LINKS" ]]; then
echo "No VPN servers found. Please check the website manually."
exit 1
fi
# Country code to flag and name mapping for display
declare -A COUNTRY_MAP=(
["ca"]="πŸ‡¨πŸ‡¦ CANADA"
["de"]="πŸ‡©πŸ‡ͺ GERMANY"
["fr"]="πŸ‡«πŸ‡· FRANCE"
["pl"]="πŸ‡΅πŸ‡± POLAND"
["uk"]="πŸ‡¬πŸ‡§ UNITED KINGDOM"
["us"]="πŸ‡ΊπŸ‡Έ UNITED STATES"
)
# Display available servers with country flags and server IDs
echo "Available VPN Servers:"
index=1
declare -A SERVERS
for link in $SERVER_LINKS; do
SERVER_CODE=$(echo "$link" | grep -oP 'vpnbook-openvpn-\K[^\.]+')
COUNTRY_CODE=$(echo "$SERVER_CODE" | grep -oP '^[a-z]+')
SERVER_NUMBER=$(echo "$SERVER_CODE" | grep -oP '[0-9]+')
# Map country code to flag and name
if [[ -n "${COUNTRY_MAP[$COUNTRY_CODE]}" ]]; then
COUNTRY_INFO="${COUNTRY_MAP[$COUNTRY_CODE]}"
else
COUNTRY_INFO="🌍 UNKNOWN"
fi
SERVERS["$index"]="$SERVER_CODE"
echo "$index. $COUNTRY_INFO $SERVER_NUMBER"
index=$((index + 1))
done
# Prompt user to select a server by number
read -p "Select a server by number (0 to exit): " SELECTION
# Validate server selection
if [[ "$SELECTION" == "0" ]]; then
echo "Exiting."
exit 0
elif [[ -z "${SERVERS[$SELECTION]}" ]]; then
echo "Invalid selection."
exit 1
fi
# Get the selected server code
SELECTED_SERVER=${SERVERS[$SELECTION]}
ZIP_URL="${BASE_URL}/free-openvpn-account/vpnbook-openvpn-${SELECTED_SERVER}.zip"
ZIP_FILE="vpnbook-openvpn-${SELECTED_SERVER}.zip"
# Download the selected VPN configuration .zip file
echo "Downloading $ZIP_URL..."
wget -O "$ZIP_FILE" "$ZIP_URL"
# Extract the .zip file to a directory
EXTRACT_DIR="vpnbook-openvpn-${SELECTED_SERVER}"
mkdir -p "$EXTRACT_DIR"
unzip -o "$ZIP_FILE" -d "$EXTRACT_DIR"
# List extracted .ovpn files with protocol and port prefixes
echo "Available configurations for $SELECTED_SERVER:"
index=1
declare -A CONFIG_FILES
for ovpn_file in "$EXTRACT_DIR"/*/*.ovpn; do
CONFIG_NAME=$(basename "$ovpn_file")
PROTOCOL=$(echo "$CONFIG_NAME" | grep -oP 'tcp|udp')
PORT=$(echo "$CONFIG_NAME" | grep -oP '(tcp|udp)(443|80|25000|53)' | grep -oP '\d+')
# Map port to service name for better readability
case "$PORT" in
443) SERVICE="HTTPS" ;;
80) SERVICE="HTTP" ;;
25000) SERVICE="ICL-TWOBASE1" ;;
53) SERVICE="DNS" ;;
*) SERVICE="UNKNOWN" ;;
esac
CONFIG_FILES["$index"]="$ovpn_file"
echo "$index. $PROTOCOL - $SERVICE ($PORT) - $CONFIG_NAME"
index=$((index + 1))
done
# Add exit option for configuration selection
echo "0. Exit"
# Prompt user to select a configuration by number
read -p "Select a configuration by number (0 to exit): " CONFIG_SELECTION
# Validate configuration selection
if [[ "$CONFIG_SELECTION" == "0" ]]; then
echo "Exiting."
exit 0
elif [[ -z "${CONFIG_FILES[$CONFIG_SELECTION]}" ]]; then
echo "Invalid selection."
exit 1
fi
# Get the selected configuration file
SELECTED_CONFIG="${CONFIG_FILES[$CONFIG_SELECTION]}"
# Download the password image from VPNBook
PASSWORD_URL="https://www.vpnbook.com/password.php?t=${TIMESTAMP}&bg=undefined"
PASSWORD_IMAGE="VPNBOOK_${TIMESTAMP}.png"
echo "Downloading password image..."
wget -O "$PASSWORD_IMAGE" "$PASSWORD_URL"
# Extract password from the image using gocr (OCR tool)
VPNBOOK_PASSWORD=$(gocr "$PASSWORD_IMAGE")
echo "Extracted Password: $VPNBOOK_PASSWORD"
# Create a temporary file for OpenVPN authentication
AUTH_FILE=$(mktemp)
echo -e "vpnbook\n$VPNBOOK_PASSWORD" > "$AUTH_FILE"
# Prompt user to connect to the VPN
read -p "Do you want to connect to the VPN? (y/n): " CONNECT
if [[ $CONNECT == "y" || $CONNECT == "Y" ]]; then
echo "Connecting to VPN using $SELECTED_CONFIG..."
sudo openvpn --config "$SELECTED_CONFIG" --auth-user-pass "$AUTH_FILE"
else
echo "Exiting without connecting to the VPN."
fi
# Securely delete the temporary authentication file
shred -u "$AUTH_FILE" "$PASSWORD_IMAGE"
rm -rf "$ZIP_FILE" "$EXTRACT_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment