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