ToolsProxy Rotator Script Generator
Proxy Rotator Script Generator
Generate production-ready rotating proxy scripts for Node.js and Python.
Proxy List0
npm install axios http-proxy-agent https-proxy-agent1/**2 * Proxy Rotator Script3 * Generated by ProxyHorizon (6/20/2026)4 *5 * Install dependencies:6 * npm install axios http-proxy-agent https-proxy-agent7 *8 * Usage:9 * const { ProxyRotator } = require('./proxy-rotator');10 * const rotator = new ProxyRotator();11 * const res = await rotator.fetch('https://httpbin.org/ip');12 */1314const axios = require('axios');15const { HttpProxyAgent } = require('http-proxy-agent');16const { HttpsProxyAgent } = require('https-proxy-agent');1718// ── Configuration ──────────────────────────────────────────────1920const PROXIES = [21 'http://1.2.3.4:8080',22 'http://5.6.7.8:3128',23 // Add your proxies here24];2526const CONFIG = {27 strategy: 'round_robin',28 rotateTrigger: 'request',29 interval: 5,30 timeout: 10000,31 maxRetries: 3,32 removeDead: true,33};3435// ── ProxyRotator Class ─────────────────────────────────────────3637class ProxyRotator {38 constructor(proxies = PROXIES, config = CONFIG) {39 this.proxies = [...proxies];40 this.config = config;41 this.index = 0;42 this.reqCount = 0;43 this.lastRotateTime = Date.now();44 this.usage = new Map();45 this.proxies.forEach(p => this.usage.set(p, 0));46 }4748 // Pick next proxy based on strategy49 getNextProxy() {50 if (this.proxies.length === 0) throw new Error('No proxies available');5152 let proxy;53 switch (this.config.strategy) {54 case 'random':55 proxy = this.proxies[Math.floor(Math.random() * this.proxies.length)];56 break;57 case 'least_used':58 proxy = this.proxies.reduce((min, p) =>59 (this.usage.get(p) || 0) < (this.usage.get(min) || 0) ? p : min60 );61 break;62 default: // round_robin63 proxy = this.proxies[this.index % this.proxies.length];64 this.index++;65 }6667 this.usage.set(proxy, (this.usage.get(proxy) || 0) + 1);68 return proxy;69 }7071 // Check if it's time to rotate72 shouldRotate() {73 this.reqCount++;74 switch (this.config.rotateTrigger) {75 case 'n_requests':76 return this.reqCount % this.config.interval === 0;77 case 'n_seconds': {78 const elapsed = (Date.now() - this.lastRotateTime) / 1000;79 if (elapsed >= this.config.interval) {80 this.lastRotateTime = Date.now();81 return true;82 }83 return false;84 }85 default:86 return true; // every request87 }88 }8990 // Remove a failed proxy from the pool91 removeProxy(proxy) {92 this.proxies = this.proxies.filter(p => p !== proxy);93 this.usage.delete(proxy);94 console.log(`[Rotator] Removed ${proxy}. ${this.proxies.length} remaining.`);95 }9697 // Make an HTTP request through a rotating proxy98 async fetch(url, options = {}) {99 let lastError;100101 for (let attempt = 0; attempt < this.config.maxRetries; attempt++) {102 const proxy = (this.shouldRotate() || attempt > 0)103 ? this.getNextProxy()104 : this.proxies[this.index % this.proxies.length];105106 try {107 const agent = url.startsWith('https')108 ? new HttpsProxyAgent(proxy)109 : new HttpProxyAgent(proxy);110111 const response = await axios({112 url,113 ...options,114 httpAgent: agent,115 httpsAgent: agent,116 timeout: this.config.timeout,117 });118119 console.log(`[Rotator] ${proxy} -> ${response.status}`);120 return response;121 } catch (error) {122 lastError = error;123 console.warn(`[Rotator] ${proxy} failed (attempt ${attempt + 1}): ${error.message}`);124125 if (this.config.removeDead && this.proxies.length > 1) {126 this.removeProxy(proxy);127 }128 }129 }130131 throw lastError;132 }133134 // Get usage statistics135 getStats() {136 return {137 totalProxies: this.proxies.length,138 totalRequests: this.reqCount,139 usage: Object.fromEntries(this.usage),140 };141 }142}143144// ── Usage Example ──────────────────────────────────────────────145146(async () => {147 const rotator = new ProxyRotator();148149 try {150 const res = await rotator.fetch('https://httpbin.org/ip');151 console.log('Response:', res.data);152 console.log('Stats:', rotator.getStats());153 } catch (err) {154 console.error('All proxies failed:', err.message);155 }156})();157158module.exports = { ProxyRotator };159