-
Notifications
You must be signed in to change notification settings - Fork 2
/
afp.js
63 lines (56 loc) · 1.91 KB
/
afp.js
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
54
55
56
57
58
59
60
61
62
63
var child_process = require('child_process');
var exec = child_process.exec;
var util = require('util');
var os = require('os');
var async = require('async');
function curlCommand(proxy, url, time) {
time = time || 5;
var outputFile = '/dev/null';
if (/^win/.test(os.platform())) {
outputFile = 'NUL';
}
return util.format('curl -X GET --silent --max-time %s --proxy %s -o %s --write-out %{speed_download} -L %s', time, proxy, outputFile, url);
}
var allProxies = [];
var proxySpeeds = {};
function getProxySpeed(proxy, time, url, cb) {
// TODO: Make platform independent
var CURL_INTERRUPTED_ERROR = 28;
exec(curlCommand(proxy, url, time), function(error, stdout, stderr) {
var speed = stdout;
if (error !== null) {
return cb(true, 0);
}
cb(null, speed);
});
}
function updateProxySpeed(proxy, time, url, cb) {
getProxySpeed(proxy, time, url, function(error, speed){
proxySpeeds[proxy] = {speed: parseInt(speed), updated: Date.now()};
cb();
});
}
exports.updateAllProxies = function(proxies, time, cb) {
allProxies = proxies;
// var IE_URL="http://download.microsoft.com/download/8/A/C/8AC7C482-BC74-492E-B978-7ED04900CEDE/IE10-Windows6.1-x86-en-us.exe" + "?randomFoo=" + Math.random();
var FB_URL="http://www.facebook.com/" + "?randomFoo=" + Math.random();
async.each(proxies,
function(proxy, cb) {
updateProxySpeed(proxy, time, FB_URL, cb);
}, cb);
};
exports.fastestProxy = function() {
var fp = allProxies[0];
for (var i = 0; i < allProxies.length; i++) {
var proxy = allProxies[i];
if (proxySpeeds.hasOwnProperty(proxy) &&
proxySpeeds[proxy].speed > proxySpeeds[fp].speed) {
fp = proxy;
}
}
if (proxySpeeds[fp] === 0) {
return null;
}
return fp;
};
exports.getProxySpeed = getProxySpeed;