forked from kyungw00k/node-ping-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping-proxy.js
117 lines (97 loc) · 3.23 KB
/
ping-proxy.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var Socks = require('socks')
, URL = require('url')
, https = require('https')
, HttpsProxyAgent = require('https-proxy-agent')
, validUrl = require('valid-url');
// Object.assign Polifill
if (typeof Object.assign != 'function') {
Object.assign = function(target, varArgs) { // .length of function is 2
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
}
var defaults = {
secureProtocol: 'SSLv23_method',
};
function pingProxyAsync(_options, callback) {
'use strict';
var options = Object.assign({}, defaults, _options);
if (options.secureProtocol) {
https.globalAgent.options.secureProtocol = options.secureProtocol;
}
var httpOptions = {hostname: 'www.google.com', port: '443'};
try {
['proxyHost', 'proxyPort'].forEach(function (required) {
if (!options[required]) {
throw required;
}
});
} catch (missing) {
callback(new Error("Missing required params: " + missing));
return;
}
options.proxyHost = "" + options.proxyHost;
if (!!options.proxyTestUrl && validUrl.isWebUri(options.proxyTestUrl)) {
options.proxyTestUrl = URL.parse(options.proxyTestUrl);
httpOptions.hostname = options.proxyTestUrl.hostname;
httpOptions.port = options.proxyTestUrl.port || options.proxyTestUrl.protocol.indexOf('https') > -1 ? 443 : 80;
}
if (!!options.proxySocks) {
if ([4, 5].indexOf(options.proxySocks) == -1) {
callback(new Error("Invalid SOCKS Protocol. Use '4' or '5' instead."));
return;
}
httpOptions.agent = new Socks.Agent(
{
proxy: {
ipaddress: options.proxyHost
, port: +options.proxyPort
, type: +options.proxySocks
}
}
, true
, false
);
} else {
httpOptions.agent = new HttpsProxyAgent(
{
host: options.proxyHost
, port: options.proxyPort
}
);
}
https.get(httpOptions, function (res) {
res.on("data", function (/*chunk*/) {
/* LET IT GO */
});
res.on("end", function () {
if (httpOptions.agent.encryptedSocket) {
httpOptions.agent.encryptedSocket.end();
}
callback(null, options, res.statusCode);
});
res.on("error", function (err) {
callback(err, options, res.statusCode);
});
})
.on("error", function (err) {
// catch errors like ECONNREFUSED
callback(err, options);
});
}
module.exports = pingProxyAsync;