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-agent
1/**
2 * Proxy Rotator Script
3 * Generated by ProxyHorizon (6/20/2026)
4 *
5 * Install dependencies:
6 * npm install axios http-proxy-agent https-proxy-agent
7 *
8 * Usage:
9 * const { ProxyRotator } = require('./proxy-rotator');
10 * const rotator = new ProxyRotator();
11 * const res = await rotator.fetch('https://httpbin.org/ip');
12 */
13
14const axios = require('axios');
15const { HttpProxyAgent } = require('http-proxy-agent');
16const { HttpsProxyAgent } = require('https-proxy-agent');
17
18// ── Configuration ──────────────────────────────────────────────
19
20const PROXIES = [
21 'http://1.2.3.4:8080',
22 'http://5.6.7.8:3128',
23 // Add your proxies here
24];
25
26const CONFIG = {
27 strategy: 'round_robin',
28 rotateTrigger: 'request',
29 interval: 5,
30 timeout: 10000,
31 maxRetries: 3,
32 removeDead: true,
33};
34
35// ── ProxyRotator Class ─────────────────────────────────────────
36
37class 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 }
47
48 // Pick next proxy based on strategy
49 getNextProxy() {
50 if (this.proxies.length === 0) throw new Error('No proxies available');
51
52 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 : min
60 );
61 break;
62 default: // round_robin
63 proxy = this.proxies[this.index % this.proxies.length];
64 this.index++;
65 }
66
67 this.usage.set(proxy, (this.usage.get(proxy) || 0) + 1);
68 return proxy;
69 }
70
71 // Check if it's time to rotate
72 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 request
87 }
88 }
89
90 // Remove a failed proxy from the pool
91 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 }
96
97 // Make an HTTP request through a rotating proxy
98 async fetch(url, options = {}) {
99 let lastError;
100
101 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];
105
106 try {
107 const agent = url.startsWith('https')
108 ? new HttpsProxyAgent(proxy)
109 : new HttpProxyAgent(proxy);
110
111 const response = await axios({
112 url,
113 ...options,
114 httpAgent: agent,
115 httpsAgent: agent,
116 timeout: this.config.timeout,
117 });
118
119 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}`);
124
125 if (this.config.removeDead && this.proxies.length > 1) {
126 this.removeProxy(proxy);
127 }
128 }
129 }
130
131 throw lastError;
132 }
133
134 // Get usage statistics
135 getStats() {
136 return {
137 totalProxies: this.proxies.length,
138 totalRequests: this.reqCount,
139 usage: Object.fromEntries(this.usage),
140 };
141 }
142}
143
144// ── Usage Example ──────────────────────────────────────────────
145
146(async () => {
147 const rotator = new ProxyRotator();
148
149 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})();
157
158module.exports = { ProxyRotator };
159