ProxyChooser
A simple proxy chooser to choose proxies from a list, which supports http/https proxies.
Installation
Install the proxychooser
package using npm:
npm install proxychooser
Usage
Import the ProxyChooser
class from the proxychooser
package:
const ProxyChooser = require("proxychooser");
Create an instance of the ProxyChooser
class with a list of proxies and options:
const proxyList = ["proxy1:port1", "proxy2:port2", ...];
const options = {
verbose: true,
maxTimeout: 2000,
forceRetry: false,
verboseIdentifier: "[proxyChooser]"
};
const proxyChooser = new ProxyChooser(proxyList, options);
Choosing a Proxy
To choose a proxy from the list, use the getProxy()
method. It returns a promise that resolves to a randomly selected working proxy from the list:
proxyChooser.getProxy()
.then(proxy => {
console.log(proxy);
})
.catch(error => {
console.error(error);
});
Examples
Here's an example of how you can use the ProxyChooser
class:
const ProxyChooser = require("proxychooser");
const proxyList = ["proxy1:port1", "proxy2:port2", ...];
const options = {
verbose: true,
maxTimeout: 1000
};
const proxyChooser = new ProxyChooser(proxyList, options);
proxyChooser.getProxy()
.then(proxy => {
console.log(proxy);
})
.catch(error => {
console.error(error);
});
API
ProxyChooser
The ProxyChooser
class provides methods to choose a proxy from a list.
Constructor
Creates an instance of the ProxyChooser
class.
new ProxyChooser(proxyList, options);
proxyList
(Array): List of proxies to use.
options
(Object): Options to configure the proxy chooser.
verbose
(boolean?): If true
, enables logging to the console. Default: false
.
verboseIdentifier
(string?): Identifier for verbose logging. Default: "[proxyChooser]".
maxTimeout
(number?): Maximum timeout for a request in milliseconds. Default: 1000
.
pingUrl
(string?): URL to use for checking the proxy. Default: "http://myexternalip.com/raw".
forceRetry
(boolean?): Whether the getProxy()
function should continue searching even on error. Default: false
.
validateOptions()
Validates the provided options and ensures they have the correct format. This method is called internally and doesn't need to be called directly.
proxyChooser.validateOptions();
proxyType(proxy)
Determines the type of the proxy based on its format.
const proxyType = proxyChooser.proxyType(proxy);
proxy
(string): Proxy to check.
- Returns: Proxy type as a string. Possible values: "direct", "auth", "invalid".
convertProxy(proxy)
Converts the proxy to a specific format.
const convertedProxy = await proxyChooser.convertProxy(proxy);
proxy
(string): Proxy to convert.
- Returns: Promise that resolves to the converted proxy as a string.
getPing()
Gets the ping between the client and the pingUrl
specified in the options.
const ping = await proxyChooser.getPing();
- Returns: Promise that resolves to the ping in milliseconds.
getProxy()
Chooses a working proxy from the list based on ping and availability.
proxyChooser.getProxy()
.then(proxy => {
console.log(proxy);
})
.catch(error => {
console.error(error);
});
- Returns: Promise that resolves to the selected working proxy as a string.
License
This project is licensed under the MIT License.