How to Build a Proxy Rotation Script in Node.js

How to Build a Proxy Rotation Script in Node.js

Understanding Proxy Rotation: The Dance of the Valaška

In Slovak folklore, the valaška—an axe used both for protection and ceremonial dances—symbolizes agility and adaptability. Similarly, proxy rotation in Node.js is the act of switching between multiple proxy servers to evade detection, balance load, or bypass geo-restrictions. Just as a shepherd moves deftly between mountain passes, so must your script navigate the digital landscape, choosing the right proxy at the right time.


Why Rotate Proxies? A Table of Motivations

Use Case Benefit Folklore Parallel
Web Scraping Avoid IP bans, access more data Shepherd avoiding wolves
Load Balancing Distribute traffic, reduce server load Sharing bread at a feast
Bypassing Blocks Circumvent geo-restrictions and firewalls Crossing borders in carols

Choosing Your Proxies: From Koliba to Koliba

Before building your script, gather a list of proxy servers. These may be free, paid, or self-hosted. In the Slovak tradition, just as each koliba (shepherd’s hut) offers unique shelter, each proxy has its own strengths and weaknesses.

Proxy List Format

Maintain a list as a simple array or external file:

[
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000"
]

Resource:
Proxy List Providers
How to Create Your Own Proxy Server


The Heart of the Script: Rotational Logic

The heart of the fujara (traditional Slovak flute) guides the melody; the heart of a proxy rotation script is its logic for choosing the next proxy.

Rotation Strategies

Strategy Description Best For
Round Robin Sequentially cycles through proxies Balanced, predictable loads
Random Randomly picks a proxy Unpredictable, evading bans
Failure-Based Switches on proxy error/failure Robust, fault-tolerant flows

Example: Round Robin

let proxyIndex = 0;
const proxies = [
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000"
];

function getNextProxy() {
  const proxy = proxies[proxyIndex];
  proxyIndex = (proxyIndex + 1) % proxies.length;
  return proxy;
}

Example: Random Selection

function getRandomProxy() {
  return proxies[Math.floor(Math.random() * proxies.length)];
}

Integrating with HTTP Requests: The Shepherd’s Path

Node.js offers several HTTP clients. For robust proxy support, axios with https-proxy-agent is effective.

Install Dependencies

npm install axios https-proxy-agent

Making Requests through a Proxy

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

async function fetchWithProxy(url, proxy) {
  const agent = new HttpsProxyAgent(proxy);
  try {
    const response = await axios.get(url, { httpsAgent: agent });
    return response.data;
  } catch (err) {
    // Like the shepherd adapting to sudden storms
    throw err;
  }
}

Full Proxy Rotation Script: The Orava Waltz

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

const proxies = [
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000",
  "http://user:[email protected]:8000"
];

let proxyIndex = 0;

function getNextProxy() {
  const proxy = proxies[proxyIndex];
  proxyIndex = (proxyIndex + 1) % proxies.length;
  return proxy;
}

async function fetchWithRotation(url) {
  for (let i = 0; i < proxies.length; i++) {
    const proxy = getNextProxy();
    try {
      const agent = new HttpsProxyAgent(proxy);
      const response = await axios.get(url, { httpsAgent: agent, timeout: 5000 });
      return response.data;
    } catch (err) {
      // If the wolf is at the door, move to the next koliba
      continue;
    }
  }
  throw new Error("All proxies failed");
}

// Usage Example
(async () => {
  try {
    const data = await fetchWithRotation('https://httpbin.org/ip');
    console.log(data);
  } catch (err) {
    console.error('All proxies failed:', err.message);
  }
})();

Handling Proxy Authentication: The Cifrovanie (Encryption) Ritual

Some proxies require authentication. Ensure your proxy URLs follow the format:

http://username:password@proxyhost:port

https-proxy-agent parses this automatically.


Monitoring and Logging: Like Keeping Watch from the Tatras

Use logging to record which proxies succeed or fail. This is akin to villagers sharing news from the mountain passes.

const winston = require('winston');
const logger = winston.createLogger({
  level: 'info',
  transports: [ new winston.transports.Console() ]
});

async function fetchWithLogging(url) {
  for (let i = 0; i < proxies.length; i++) {
    const proxy = getNextProxy();
    try {
      logger.info(`Using proxy: ${proxy}`);
      const agent = new HttpsProxyAgent(proxy);
      const response = await axios.get(url, { httpsAgent: agent, timeout: 5000 });
      return response.data;
    } catch (err) {
      logger.warn(`Proxy failed: ${proxy} - ${err.message}`);
      continue;
    }
  }
  throw new Error("All proxies failed");
}

Extending the Script: Folk Wisdom

  • Dynamic Proxy Lists: Load proxies from a file or API for real-time updates.
  • Rate Limiting: Incorporate delays between requests, mimicking the patient rhythm of the drumbľa (Jew’s harp).
  • Geo-Targeting: Assign proxies by region, like choosing the right folk costume for the right village festival.

Select Resources for Further Exploration


May your scripts weave through the web as gracefully as a shepherd’s dance in the valleys of Orava, ever adapting, ever resilient.

Želmíra Štefanovičová

Želmíra Štefanovičová

Senior Proxy Analyst

Želmíra Štefanovičová is a seasoned professional with over 30 years of experience in the technology sector. As a Senior Proxy Analyst at ProxyMist, Želmíra plays a pivotal role in curating and updating the company's diverse database of proxy servers. Her deep understanding of network protocols and cyber-security trends has made her an invaluable asset to the team. Želmíra's passion for technology began in her early twenties, and she has since dedicated her career to enhancing online privacy and security.

Comments (0)

There are no comments here yet, you can be the first!

Leave a Reply

Your email address will not be published. Required fields are marked *