|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Auto Proxy Rotation Script |
| 4 | +# Automatically tests proxies and updates config if primary proxies fail |
| 5 | +# Run this via cron every hour or when bots disconnect |
| 6 | + |
| 7 | +PROXIES=( |
| 8 | + "31.59.20.176:6754" |
| 9 | + "45.38.107.97:6014" |
| 10 | + "198.23.239.134:6540" |
| 11 | + "107.172.163.27:6543" |
| 12 | + "64.137.96.74:6641" |
| 13 | + "216.10.27.159:6837" |
| 14 | + "142.111.67.146:5611" |
| 15 | + "142.147.128.93:6593" |
| 16 | +) |
| 17 | + |
| 18 | +PROXY_USER="pjukyfij" |
| 19 | +PROXY_PASS="3q9caatdky72" |
| 20 | +CONFIG_FILE="/var/www/csfloat-api/config.js" |
| 21 | +WORKING_PROXIES=() |
| 22 | +TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') |
| 23 | + |
| 24 | +echo "[$TIMESTAMP] === Auto Proxy Rotation Check ===" |
| 25 | + |
| 26 | +# Test each proxy |
| 27 | +for proxy in "${PROXIES[@]}"; do |
| 28 | + echo -n "Testing $proxy... " |
| 29 | + |
| 30 | + # Test proxy with 5 second timeout |
| 31 | + RESULT=$(curl --connect-timeout 5 --max-time 5 \ |
| 32 | + --proxy "http://$PROXY_USER:$PROXY_PASS@$proxy/" \ |
| 33 | + https://ipv4.webshare.io/ 2>&1) |
| 34 | + |
| 35 | + if echo "$RESULT" | grep -qE "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$"; then |
| 36 | + echo "✅ OK" |
| 37 | + WORKING_PROXIES+=("$proxy") |
| 38 | + else |
| 39 | + echo "❌ FAILED" |
| 40 | + fi |
| 41 | +done |
| 42 | + |
| 43 | +echo "" |
| 44 | +echo "Working proxies: ${#WORKING_PROXIES[@]}/${#PROXIES[@]}" |
| 45 | + |
| 46 | +# Ensure we have at least 3 working proxies |
| 47 | +if [ ${#WORKING_PROXIES[@]} -lt 3 ]; then |
| 48 | + echo "⚠️ ERROR: Only ${#WORKING_PROXIES[@]} working proxies found (need minimum 3)" |
| 49 | + echo "Service may experience issues. Manual intervention required." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Read current proxies from config - simpler extraction |
| 54 | +CURRENT_PROXIES=$(grep "http://$PROXY_USER:$PROXY_PASS@" "$CONFIG_FILE" | grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+" | head -3) |
| 55 | +CURRENT_PROXY_1=$(echo "$CURRENT_PROXIES" | sed -n '1p') |
| 56 | +CURRENT_PROXY_2=$(echo "$CURRENT_PROXIES" | sed -n '2p') |
| 57 | +CURRENT_PROXY_3=$(echo "$CURRENT_PROXIES" | sed -n '3p') |
| 58 | + |
| 59 | +echo "" |
| 60 | +echo "Current config proxies (first 3):" |
| 61 | +echo " Bot 0: $CURRENT_PROXY_1" |
| 62 | +echo " Bot 1: $CURRENT_PROXY_2" |
| 63 | +echo " Bot 2: $CURRENT_PROXY_3" |
| 64 | + |
| 65 | +# Check if current proxies are still working |
| 66 | +NEEDS_UPDATE=false |
| 67 | + |
| 68 | +if ! [[ " ${WORKING_PROXIES[@]} " =~ " ${CURRENT_PROXY_1} " ]]; then |
| 69 | + echo "⚠️ Bot 0 proxy ($CURRENT_PROXY_1) is DOWN - needs rotation" |
| 70 | + NEEDS_UPDATE=true |
| 71 | +fi |
| 72 | + |
| 73 | +if ! [[ " ${WORKING_PROXIES[@]} " =~ " ${CURRENT_PROXY_2} " ]]; then |
| 74 | + echo "⚠️ Bot 1 proxy ($CURRENT_PROXY_2) is DOWN - needs rotation" |
| 75 | + NEEDS_UPDATE=true |
| 76 | +fi |
| 77 | + |
| 78 | +if ! [[ " ${WORKING_PROXIES[@]} " =~ " ${CURRENT_PROXY_3} " ]]; then |
| 79 | + echo "⚠️ Bot 2 proxy ($CURRENT_PROXY_3) is DOWN - needs rotation" |
| 80 | + NEEDS_UPDATE=true |
| 81 | +fi |
| 82 | + |
| 83 | +if [ "$NEEDS_UPDATE" = false ]; then |
| 84 | + echo "✅ All current proxies are working - no rotation needed" |
| 85 | + exit 0 |
| 86 | +fi |
| 87 | + |
| 88 | +# Backup current config |
| 89 | +cp "$CONFIG_FILE" "${CONFIG_FILE}.backup.$(date +%s)" |
| 90 | +echo "" |
| 91 | +echo "📝 Updating proxy configuration..." |
| 92 | + |
| 93 | +# Build new proxy array |
| 94 | +PROXY_LINES="" |
| 95 | +for i in "${!WORKING_PROXIES[@]}"; do |
| 96 | + proxy="${WORKING_PROXIES[$i]}" |
| 97 | + |
| 98 | + # Add comment based on position |
| 99 | + if [ $i -eq 0 ]; then |
| 100 | + comment="Bot 0 (xgamingserver3)" |
| 101 | + elif [ $i -eq 1 ]; then |
| 102 | + comment="Bot 1 (xgamingserver1)" |
| 103 | + elif [ $i -eq 2 ]; then |
| 104 | + comment="Bot 2 (johnsonk01)" |
| 105 | + else |
| 106 | + comment="Backup proxy $((i+1))" |
| 107 | + fi |
| 108 | + |
| 109 | + PROXY_LINES+=" 'http://$PROXY_USER:$PROXY_PASS@$proxy/', // $comment ✅"$'\n' |
| 110 | +done |
| 111 | + |
| 112 | +# Use awk to replace the proxies section |
| 113 | +awk -v proxies="$PROXY_LINES" ' |
| 114 | + /^ .proxies.: \[/ { |
| 115 | + print |
| 116 | + print proxies |
| 117 | + # Skip until closing bracket |
| 118 | + while (getline > 0) { |
| 119 | + if (/^ \],/) { |
| 120 | + print |
| 121 | + break |
| 122 | + } |
| 123 | + } |
| 124 | + next |
| 125 | + } |
| 126 | + { print } |
| 127 | +' "$CONFIG_FILE" > "${CONFIG_FILE}.tmp" && mv "${CONFIG_FILE}.tmp" "$CONFIG_FILE" |
| 128 | + |
| 129 | +echo "✅ Config updated with working proxies" |
| 130 | +echo "" |
| 131 | +echo "🔄 Restarting CSFloat API service..." |
| 132 | + |
| 133 | +# Restart the pm2 service (correct name: cs2-float-api) |
| 134 | +pm2 restart cs2-float-api |
| 135 | + |
| 136 | +echo "✅ Service restarted" |
| 137 | +echo "" |
| 138 | +echo "[$TIMESTAMP] Proxy rotation completed successfully" |
| 139 | + |
| 140 | +# Wait a few seconds and check bot status |
| 141 | +sleep 5 |
| 142 | +echo "" |
| 143 | +echo "Bot status after rotation:" |
| 144 | +curl -s http://localhost:3002/stats | grep -o '"bots_online":[0-9]*' || echo "Could not fetch status" |
0 commit comments