GithubHelp home page GithubHelp logo

gorantopic / proxy-rotator-js Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 2.35 MB

A simple proxy rotator for nodejs

Home Page: https://www.npmjs.com/package/proxy-rotator-js

JavaScript 100.00%
manager proxy rotator javascript js node

proxy-rotator-js's Introduction

Proxy Rotator

Introduction

ProxyRotator is a JavaScript class that provides a mechanism for managing a pool of proxies and rotating them based on their availability and status.

Installation

npm install proxy-rotator-js

Usage

import ProxyRotator from 'proxy-rotator-js'

let proxies = ['proxy1', 'proxy2', 'proxy3']

let rotator = new ProxyRotator(proxies, options={})

console.log( rotator.next() ) // 'proxy1'
console.log( rotator.next() ) // 'proxy2'

or you can pass the path to a proxy file

import ProxyRotator from 'proxy-rotator-js'

let filename = '/path/to/proxy/file.txt'

let rotator = new ProxyRotator(filename, options={});


console.log( rotator.next() ) // 'proxy1'
console.log( rotator.next() ) // 'proxy2'

Initializes a new instance of ProxyRotator with the given proxies and options. The proxies parameter can be a file path or an array of proxies. The options parameter allows customization of various settings such as revive timer, shuffling, protocol assumption, and more. Methods

Methods

next() // Rotates the proxy by moving the front proxy to the end of the pool and returns it.

add(proxies) // Adds one or more proxies to the pool.

getAlive() //  Retrieves a random alive proxy from the pool.

setAlive(proxy) // Sets a specific proxy to an alive state.

setDead(proxy) // Sets a specific proxy to a dead state and moves it to the graveyard.

resurrect(proxy) // Moves a proxy from the graveyard back to the pool.

getPool() // Returns an array of proxies in the pool.

getPoolSize() // Returns the number of proxies in the pool.

getGraveyard() // Returns an array of proxies in the graveyard (dead proxies).

getGraveyardSize() // Returns the number of proxies in the graveyard.

remove(proxy) // Removes one or more proxies from the pool.

Properties

pool: Represents the pool of proxies as a queue.

graveyard: Stores proxies that are currently dead or inactive.

Options

const proxies = ['proxy1.example.com', 'proxy2.example.com'];
const options = {
    returnAs: 'object',
    revive_timer: 1000 * 60 * 30,
    shuffle: true,
    protocol: 'http',
    assume_aliveness: true,
    check_on_next: true
};

const proxyRotator = new ProxyRotator(proxies, options);
The following options can be passed to customize the behavior of the ProxyRotator:

- returnAs: Specifies the return type of proxies. Can be either 'string' or 'object'. Default: 'string'.
- revive_timer: Specifies the duration in milliseconds before a dead proxy is revived. Default: 1000 * 60 * 30 (30 minutes).
- protocol: Specifies a protocol for all proxies. Default: null.
- shuffle: Specifies whether to shuffle the proxies before adding them to the queue. Default: false.
- assume_aliveness: Specifies whether to assume all proxies are alive when first added instead of 'new'. Default: false.
- check_on_next: Specifies whether to check if proxies are alive when they are added to the queue. Default: false.

Testing your Proxies

import ProxyRotator from 'proxy-rotator-js'

let proxies = ['proxy1', 'proxy2', 'proxy3']

let rotator = new ProxyRotator(proxies, options={})

rotator.test()

Getting Started

To use the ProxyRotator class in your JavaScript project, follow these steps:

Make sure you have Node.js  and npm installed on your system. 
npm install proxy-rotator-js

Import the ProxyRotator class into your JavaScript file using the following line of code:

Create an instance of ProxyRotator by calling the constructor and providing the required parameters. For example:
const proxies = ['proxy1:8000', 'proxy2:322', 'proxy3:543'];
const rotator = new ProxyRotator(proxies);
Access the properties and methods of the ProxyRotator object using dot notation. For example:
rotator.next();
// 'proxy1:500'
const poolSize = proxyRotator.getPoolSize();
// 3
const aliveProxy = proxyRotator.getAlive();
// 'proxy2:500'
proxyRotator.setAlive( 'proxy2:500' );
// null 

Examples

import ProxyRotator from './ProxyRotator.js';

// Create an instance of ProxyRotator
const proxies = ['proxy1', 'proxy2', 'proxy3'];
const options = { revive_timer: 1800000, shuffle: true };
const proxyRotator = new ProxyRotator(proxies, options);

// Access the properties
console.log(proxyRotator.getGraveyard());  // Output: []
console.log(proxyRotator.getGraveyardSize());  // Output: 0
console.log(proxyRotator.getPool());  // Output: ['proxy1', 'proxy2', 'proxy3']
console.log(proxyRotator.getPoolSize());  // Output: 3

// Call the methods
proxyRotator.add('proxy4');
console.log(proxyRotator.getPool());  // Output: ['proxy1', 'proxy2', 'proxy3', 'proxy4']
proxyRotator.remove('proxy2');
console.log(proxyRotator.getPool());  // Output: ['proxy1', 'proxy3', 'proxy4']
const aliveProxy = proxyRotator.getAlive();
console.log(aliveProxy);  // Output: 'proxy1'
proxyRotator.setAlive('proxy3');
proxyRotator.next(); // prox1
proxyRotator.next(); // prox3
proxyRotator.next(); // prox4
console.log(proxyRotator.getPool());  // Output: ['proxy1', 'proxy4', 'proxy3']

Contributing

If you would like to contribute to the ProxyRotator project, you can fork the repository and make your desired changes. Feel free to submit a pull request with your improvements or bug fixes. We appreciate your contributions! License

The ProxyRotator class is released under the MIT License. You can freely use and modify it in your projects. Please refer to the license file for more information. Contact

If you have any questions, suggestions, or feedback regarding the ProxyRotator class, please me =) Goran Topic @ [email protected]. We appreciate your input and are happy to assist you.

Thank you for using the ProxyRotator class. We hope it helps simplify your proxy management and rotation tasks.

proxy-rotator-js's People

Contributors

gorantopic avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.