-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswitch-proxies-every-n-seconds-or-round-robin.txt
53 lines (41 loc) · 1.69 KB
/
switch-proxies-every-n-seconds-or-round-robin.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Switch proxies after X seconds
// Rotate through a list of proxies, switching every _secondsToSwitchAfter_,
// one for each URL (not for each website -- but for each URL that is loaded)
// Define proxy list first
var proxies = ["PROXY myproxy.foo.com:9050", "PROXY myproxy.bar.com:8080",
"PROXY myproxy.baz.com:6667"], t1 = new Date().getTime(), counter = 1;
var secondsToSwitchAfter = 3;
function FindProxyForURL(url, host) {
var t2 = new Date.getTime();
if (t2 - t1 > secondsToSwitchAfter * 1000)
counter++;
t1 = t2;
return proxies[counter % proxies.length];
}
=================
// Round robin
// Rotate through a list of proxies, in order, one for each URL (not for each website -- but for each URL that is loaded)
// Define proxy list first
var proxies = ["PROXY myproxy.foo.com:9050", "PROXY myproxy.bar.com:8080", "PROXY myproxy.baz.com:6667"];
var counter = 0;
function FindProxyForURL(url, host) {
if (counter > proxies.length)
counter = 0;
return proxies[counter++];
}
=================
// Random
// Randomly rotate through a list of proxies, one for each URL (not for each website -- but for each URL that is loaded)
// Define proxy list first
var proxies = ["PROXY myproxy.foo.com:9050", "PROXY myproxy.bar.com:8080", "PROXY myproxy.baz.com:6667"];
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
* http://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function FindProxyForURL(url, host) {
return proxies[getRandomInt(0, proxies.length-1)];
}