#!/data/data/com.termux/files/usr/bin/bash set -e echo "" echo "====================================" echo " CLI-Wrangler (Termux)" echo "====================================" echo "" # ----------------------------- # Check dependencies # ----------------------------- check_cmd() { command -v "$1" >/dev/null 2>&1 } install_node() { echo "[*] Installing Node.js..." pkg update -y pkg install nodejs -y } install_wrangler() { echo "[*] Installing Wrangler..." npm install -g wrangler@latest } echo "[*] Checking dependencies..." if ! check_cmd node; then echo "[!] Node.js not found" install_node fi if ! check_cmd npm; then echo "[!] npm not found (installing node again)" install_node fi if ! check_cmd wrangler; then echo "[!] Wrangler not found" install_wrangler fi echo "[✓] Dependencies ready" echo "" # ----------------------------- # Temp workspace # ----------------------------- BASE_DIR="$HOME/cli-wrangler" mkdir -p "$BASE_DIR" # ----------------------------- # Input helper # ----------------------------- ask() { echo -n "$1" read -r value echo "$value" } # ----------------------------- # Mode selector # ----------------------------- echo "Select Deployment Mode:" echo "1) Standard Redirect" echo "2) Fetch as Text" echo "3) CLI Only (no browser detection)" echo "4) HTML Inline Fetch" echo "" MODE=$(ask "Mode [1-4]: ") if [[ "$MODE" != "1" && "$MODE" != "2" && "$MODE" != "3" && "$MODE" != "4" ]]; then echo "[!] Invalid mode" exit 1 fi PROXY=$(ask "Use proxy worker? (y/n): ") echo "" # ----------------------------- # Project setup # ----------------------------- PROJECT=$(ask "Project name: ") URL=$(ask "Target URL: ") BRANCH=$(ask "Branch (default: production): ") if [ -z "$BRANCH" ]; then BRANCH="production" fi PROJECT_DIR="$BASE_DIR/$PROJECT" FUNC_DIR="$PROJECT_DIR/functions" rm -rf "$PROJECT_DIR" mkdir -p "$FUNC_DIR" # ----------------------------- # Proxy URL (optional) # ----------------------------- PROXY_URL="" if [[ "$PROXY" == "y" || "$PROXY" == "Y" ]]; then echo "" echo "[*] Proxy Worker setup" WORKER_NAME=$(ask "Worker name (default proxy): ") if [ -z "$WORKER_NAME" ]; then WORKER_NAME="proxy" fi echo "[*] Deploying proxy worker..." WORKER_DIR="$BASE_DIR/worker_$WORKER_NAME" rm -rf "$WORKER_DIR" mkdir -p "$WORKER_DIR" cat > "$WORKER_DIR/worker.js" << 'EOF' export default { async fetch(request) { const url = new URL(request.url).searchParams.get("url"); if (!url) { return new Response("Usage: ?url=YOUR_URL", { status: 400 }); } try { const res = await fetch(url, { cache: "no-store" }); const text = await res.text(); return new Response(text, { headers: { "Content-Type": "text/plain; charset=utf-8" } }); } catch (e) { return new Response("Error: " + e.message, { status: 500 }); } } } EOF cat > "$WORKER_DIR/wrangler.toml" << EOF name = "$WORKER_NAME" main = "worker.js" compatibility_date = "$(date +%Y-%m-%d)" EOF cd "$WORKER_DIR" wrangler deploy PROXY_URL="https://${WORKER_NAME}.workers.dev" echo "[✓] Proxy deployed: $PROXY_URL" cd "$BASE_DIR" fi # ----------------------------- # Generate index.js # ----------------------------- ENCODED_URL=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$URL'''))" 2>/dev/null || echo "$URL") TARGET_URL="$URL" if [ -n "$PROXY_URL" ]; then TARGET_URL="$PROXY_URL/?url=$ENCODED_URL" fi echo "[*] Generating function..." mkdir -p "$FUNC_DIR" if [ "$MODE" = "1" ]; then cat > "$FUNC_DIR/index.js" << EOF export async function onRequest() { const r = await fetch("$TARGET_URL"); return Response.redirect(r.url, 302); } EOF elif [ "$MODE" = "2" ]; then cat > "$FUNC_DIR/index.js" << EOF export async function onRequest() { const r = await fetch("$TARGET_URL"); return new Response(await r.text(), { headers: { "Content-Type": "text/plain; charset=utf-8" } }); } EOF elif [ "$MODE" = "3" ]; then cat > "$FUNC_DIR/index.js" << EOF export async function onRequest(request) { const ua = request.headers.get("user-agent") || ""; if (/Mozilla|Chrome|Safari|Firefox/i.test(ua)) { return new Response(null, { status: 204 }); } const r = await fetch("$TARGET_URL"); return new Response(await r.text(), { headers: { "Content-Type": "text/plain; charset=utf-8" } }); } EOF elif [ "$MODE" = "4" ]; then cat > "$FUNC_DIR/index.js" << EOF export async function onRequest() { const r = await fetch("$TARGET_URL"); return new Response(await r.text(), { headers: { "Content-Type": "text/html; charset=utf-8" } }); } EOF fi echo "[✓] index.js created" # ----------------------------- # Deploy Pages # ----------------------------- cd "$PROJECT_DIR" cat > wrangler.toml << EOF name = "$PROJECT" compatibility_date = "$(date +%Y-%m-%d)" pages_build_output_dir = "functions" EOF echo "" echo "[*] Deploying to Cloudflare Pages..." wrangler pages deploy . --project-name "$PROJECT" --branch "$BRANCH" echo "" echo "====================================" echo " DEPLOY COMPLETE 🚀" echo "====================================" echo "" echo "Project: $PROJECT" echo "URL: https://$PROJECT.pages.dev" if [ -n "$PROXY_URL" ]; then echo "Proxy: $PROXY_URL" fi echo ""