Created
February 14, 2025 10:37
-
-
Save boidushya/e5a9d1ebc814d4a28bc7abe40aaf53c7 to your computer and use it in GitHub Desktop.
Cloudflare Tunnel with QR Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Default values | |
| PORT=3000 | |
| PROTOCOL="http" | |
| HOST="localhost" | |
| ADDITIONAL_PARAMS="" | |
| # Function to display usage | |
| show_help() { | |
| echo "Usage: $(basename $0) [OPTIONS]" | |
| echo "Generate a QR code for Cloudflare tunnel URL" | |
| echo | |
| echo "Options:" | |
| echo " -p, --port PORT Port number (default: 3000)" | |
| echo " -P, --protocol PROTO Protocol (http/https) (default: http)" | |
| echo " -H, --host HOST Host (default: localhost)" | |
| echo " -a, --params PARAMS Additional cloudflared parameters" | |
| echo " -h, --help Show this help message" | |
| } | |
| # Parse command line arguments | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -p | --port) | |
| PORT="$2" | |
| shift 2 | |
| ;; | |
| -P | --protocol) | |
| PROTOCOL="$2" | |
| shift 2 | |
| ;; | |
| -H | --host) | |
| HOST="$2" | |
| shift 2 | |
| ;; | |
| -a | --params) | |
| ADDITIONAL_PARAMS="$2" | |
| shift 2 | |
| ;; | |
| -h | --help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| echo "Unknown option: $1" | |
| show_help | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # Check if required commands exist | |
| check_dependencies() { | |
| local missing_deps=() | |
| if ! command -v cloudflared >/dev/null 2>&1; then | |
| missing_deps+=("cloudflared") | |
| fi | |
| if ! command -v qrencode >/dev/null 2>&1; then | |
| missing_deps+=("qrencode") | |
| fi | |
| if [ ${#missing_deps[@]} -ne 0 ]; then | |
| echo "Error: Missing required dependencies:" | |
| for dep in "${missing_deps[@]}"; do | |
| echo " - $dep" | |
| done | |
| echo -e "\nInstall missing dependencies with:" | |
| echo "brew install cloudflare/cloudflare/cloudflared qrencode" | |
| exit 1 | |
| fi | |
| } | |
| # Main execution | |
| check_dependencies | |
| # Create a named pipe for URL monitoring | |
| URL_PIPE=$(mktemp -u) | |
| mkfifo "$URL_PIPE" | |
| # Cleanup function | |
| cleanup() { | |
| rm -f "$URL_PIPE" | |
| kill $(jobs -p) 2>/dev/null | |
| } | |
| trap cleanup EXIT | |
| # Run cloudflared and monitor its output in a subshell | |
| (script -q /dev/null cloudflared tunnel --url "$PROTOCOL://$HOST:$PORT" $ADDITIONAL_PARAMS 2>&1 | tee "$URL_PIPE") & | |
| # Process the output to generate QR code | |
| while IFS= read -r line; do | |
| if [[ $line == *"Your quick Tunnel has been created!"* ]]; then | |
| # Read next line | |
| read -r url_line | |
| url=$(echo "$url_line" | grep -o 'https://[a-zA-Z0-9.-]*\.trycloudflare\.com') | |
| if [ ! -z "$url" ]; then | |
| printf "\nTunnel URL: %s\n" "$url" | |
| printf "\nScan this QR code:\n" | |
| qrencode -t UTF8 "$url" | |
| printf "\n" | |
| fi | |
| fi | |
| done <"$URL_PIPE" & | |
| # Wait for background processes | |
| wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment