proxy-rotator-js 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/package.json +2 -1
- package/src/ProxyRotator.js +39 -1
- package/src/utils/makeRequestWithProxy.js +21 -0
- package/test/options.js +32 -0
- package/test/test_proxies.js +22 -0
package/README.md
CHANGED
|
@@ -77,6 +77,29 @@ remove(proxy) // Removes one or more proxies from the pool.
|
|
|
77
77
|
|
|
78
78
|
graveyard: Stores proxies that are currently dead or inactive.
|
|
79
79
|
|
|
80
|
+
## Options
|
|
81
|
+
```javascript
|
|
82
|
+
const proxies = ['proxy1.example.com', 'proxy2.example.com'];
|
|
83
|
+
const options = {
|
|
84
|
+
returnAs: 'object',
|
|
85
|
+
revive_timer: 1000 * 60 * 30,
|
|
86
|
+
shuffle: true,
|
|
87
|
+
protocol: 'http',
|
|
88
|
+
assume_aliveness: true,
|
|
89
|
+
check_on_next: true
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const proxyRotator = new ProxyRotator(proxies, options);
|
|
93
|
+
```
|
|
94
|
+
The following options can be passed to customize the behavior of the ProxyRotator:
|
|
95
|
+
|
|
96
|
+
- returnAs: Specifies the return type of proxies. Can be either 'string' or 'object'. Default: 'string'.
|
|
97
|
+
- revive_timer: Specifies the duration in milliseconds before a dead proxy is revived. Default: 1000 * 60 * 30 (30 minutes).
|
|
98
|
+
- protocol: Specifies a protocol for all proxies. Default: null.
|
|
99
|
+
- shuffle: Specifies whether to shuffle the proxies before adding them to the queue. Default: false.
|
|
100
|
+
- assume_aliveness: Specifies whether to assume all proxies are alive when first added instead of 'new'. Default: false.
|
|
101
|
+
- check_on_next: Specifies whether to check if proxies are alive when they are added to the queue. Default: false.
|
|
102
|
+
|
|
80
103
|
## Getting Started
|
|
81
104
|
|
|
82
105
|
To use the ProxyRotator class in your JavaScript project, follow these steps:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "proxy-rotator-js",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Proxy Rotator",
|
|
6
6
|
"main": "index.js",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"anymatch": "^3.1.3",
|
|
15
15
|
"argparse": "^2.0.1",
|
|
16
16
|
"assertion-error": "^1.1.0",
|
|
17
|
+
"axios": "^1.6.8",
|
|
17
18
|
"balanced-match": "^1.0.2",
|
|
18
19
|
"binary-extensions": "^2.2.0",
|
|
19
20
|
"brace-expansion": "^2.0.1",
|
package/src/ProxyRotator.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import Queue from './Queue.js';
|
|
3
3
|
import Proxy from './Proxy.js';
|
|
4
|
+
import makeRequestWithProxy from './utils/makeRequestWithProxy.js';
|
|
4
5
|
|
|
5
6
|
class ProxyRotator {
|
|
6
7
|
constructor(proxies, options={} ){
|
|
@@ -16,7 +17,7 @@ class ProxyRotator {
|
|
|
16
17
|
// assume a a protocol for all proxies
|
|
17
18
|
this.protocol = protocol ?? null;
|
|
18
19
|
// shuffle the proxies before adding them to the queue
|
|
19
|
-
this.shuffle = shuffle ??
|
|
20
|
+
this.shuffle = shuffle ?? false;
|
|
20
21
|
// assume all proxies are alive when first added instead of 'new'
|
|
21
22
|
this.assume_aliveness = assume_aliveness ?? false;
|
|
22
23
|
// check if proxies are alive when they are added to the queue
|
|
@@ -32,6 +33,12 @@ class ProxyRotator {
|
|
|
32
33
|
// add proxies to queue
|
|
33
34
|
proxies.forEach( p => this._add(p) );
|
|
34
35
|
}
|
|
36
|
+
// shuffle proxies
|
|
37
|
+
if(this.shuffle){
|
|
38
|
+
let shuffled = this._shuffleArray(this.pool.toArray());
|
|
39
|
+
this.pool = new Queue();
|
|
40
|
+
shuffled.forEach( p => this.pool.enqueue(p) );
|
|
41
|
+
}
|
|
35
42
|
}
|
|
36
43
|
|
|
37
44
|
getGraveyard(){ return this.graveyard.map(p => p.proxy) }
|
|
@@ -196,6 +203,37 @@ class ProxyRotator {
|
|
|
196
203
|
this.resurect(proxy);
|
|
197
204
|
}
|
|
198
205
|
|
|
206
|
+
async test_proxies(){
|
|
207
|
+
// test if proxies are working
|
|
208
|
+
let proxies = this.pool.toArray()
|
|
209
|
+
.map(p=>p.proxy)
|
|
210
|
+
.map(p=> ({
|
|
211
|
+
host:p.split(':')[0],
|
|
212
|
+
port:p.split(':')[1]
|
|
213
|
+
}));
|
|
214
|
+
console.log('--- Testing Proxies ---');
|
|
215
|
+
let workingCount = 0;
|
|
216
|
+
let notWorkingCount = 0;
|
|
217
|
+
// make request with proxy
|
|
218
|
+
for (const proxy of proxies) {
|
|
219
|
+
console.log(`Testing proxy ${proxy.host}:${proxy.port}...`);
|
|
220
|
+
const response = await makeRequestWithProxy(proxy);
|
|
221
|
+
if (response === proxy.host) {
|
|
222
|
+
console.log(`Proxy ${proxy.host}:${proxy.port} is working.`);
|
|
223
|
+
workingCount++;
|
|
224
|
+
} else {
|
|
225
|
+
console.log(`Proxy ${proxy.host}:${proxy.port} is not working.`);
|
|
226
|
+
notWorkingCount++;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
console.log('--- Statistics ---');
|
|
230
|
+
console.log(`Total proxies: ${proxies.length}`);
|
|
231
|
+
console.log(`Working proxies: ${workingCount}`);
|
|
232
|
+
console.log(`Not working proxies: ${notWorkingCount}`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
test = this.test_proxies;
|
|
236
|
+
|
|
199
237
|
}
|
|
200
238
|
|
|
201
239
|
export default ProxyRotator
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
// Description: Function to make an Axios request using a proxy
|
|
3
|
+
|
|
4
|
+
// Function to make an Axios request using a proxy
|
|
5
|
+
async function makeRequestWithProxy(proxy) {
|
|
6
|
+
try {
|
|
7
|
+
const response = await axios.get('https://api.ipify.org?format=json', {
|
|
8
|
+
proxy: {
|
|
9
|
+
host: proxy.host,
|
|
10
|
+
port: proxy.port,
|
|
11
|
+
protocol: 'http'
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
console.log(response.data);
|
|
15
|
+
return response.data;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default makeRequestWithProxy;
|
package/test/options.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import ProxyRotator from '../index.js'
|
|
2
|
+
import assert from 'assert';
|
|
3
|
+
import chai from 'chai';
|
|
4
|
+
const expect = chai.expect
|
|
5
|
+
|
|
6
|
+
// slogan: when you like a little risk with you proxies
|
|
7
|
+
// proxies to test with
|
|
8
|
+
let test_proxies = [ '139.59.1.14:8080', '94.45.74.60:8080',
|
|
9
|
+
'161.35.70.249:3128', '217.182.170.224:80', '222.138.76.6:9002',
|
|
10
|
+
'218.252.206.89:80', '18.214.66.210:80', '120.234.203.171:9002' ]
|
|
11
|
+
|
|
12
|
+
// check shuffle option
|
|
13
|
+
describe('shuffle option', () => {
|
|
14
|
+
it('shuffle proxies when true', () => {
|
|
15
|
+
// make rotator with shuffle option set to true
|
|
16
|
+
let rotator = new ProxyRotator(test_proxies, { shuffle: true });
|
|
17
|
+
// get pool
|
|
18
|
+
let pool = rotator.getPool();
|
|
19
|
+
// check if the proxies are shuffled
|
|
20
|
+
assert.notDeepEqual(test_proxies, pool);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('do not shuffle proxies when false', () => {
|
|
24
|
+
// make rotator with shuffle option set to false
|
|
25
|
+
let rotator = new ProxyRotator(test_proxies, { shuffle: false });
|
|
26
|
+
// get pool
|
|
27
|
+
let pool = rotator.getPool();
|
|
28
|
+
// check if the proxies are not shuffled
|
|
29
|
+
assert.deepEqual(test_proxies, pool);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import ProxyRotator from '../index.js'
|
|
2
|
+
import assert from 'assert';
|
|
3
|
+
import chai from 'chai';
|
|
4
|
+
const expect = chai.expect
|
|
5
|
+
|
|
6
|
+
// slogan: when you like a little risk with you proxies
|
|
7
|
+
// proxies to test with
|
|
8
|
+
let test_proxies = [ '139.59.1.14:8080', '94.45.74.60:8080',
|
|
9
|
+
'161.35.70.249:3128', '217.182.170.224:80', '222.138.76.6:9002',
|
|
10
|
+
'218.252.206.89:80', '18.214.66.210:80', '120.234.203.171:9002' ]
|
|
11
|
+
|
|
12
|
+
// check if it is able to read from difrent files
|
|
13
|
+
describe('testing testing proxies', () => {
|
|
14
|
+
// test if it can process a string point to a file
|
|
15
|
+
it('testing proxeis', () => {
|
|
16
|
+
// make rotator
|
|
17
|
+
let rotator = new ProxyRotator(test_proxies);
|
|
18
|
+
rotator.test()
|
|
19
|
+
assert.deepEqual(test_proxies,rotator.getPool())
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
|