-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
136 lines (130 loc) · 4.16 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Created by WILL on 2016/8/17.
*/
var http = require('http');
var jsdom = require('jsdom');
var getnt = function (options) {
http.request({
host: 'www.xicidaili.com',
path: '/nt/' + options.page,
method: 'GET',
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}
}, function (res) {
var html = '';
res.on('data', function (data) {
html += data;
});
res.on('end', function () {
options.callback(html);
});
}).on('error', function (err) {
console.log(err);
}).end();
};
var getnn = function (options) {
http.request({
host: 'www.xicidaili.com',
path: '/nn/' + options.page,
method: 'GET',
headers: {
"user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}
}, function (res) {
var html = '';
res.on('data', function (data) {
html += data;
});
res.on('end', function () {
options.callback(html);
});
}).on('error', function (err) {
console.log(err);
}).end();
};
var domproxies = function (pagelength, callback) {
var proxyPages = [];
for (var page = 1; page <= pagelength; ++page) {
getnt({
page: page,
callback: function (html) {
jsdom.env(html, [], function (error, window) {
if (!error) {
var table = window.document.getElementById('ip_list');
var rows = table.getElementsByTagName('tr');
var proxies = [];
for (var i = 1; i < rows.length; ++i) {
var row = rows[i];
var td = row.getElementsByTagName('td');
if (td.length >= 3) {
proxies.push({
ip: td[1].innerHTML,
port: +td[2].innerHTML
});
}
}
proxyPages.push(proxies);
var result = [];
if (proxyPages.length >= pagelength) {
for (i in proxyPages) {
var item = proxyPages[i];
result = result.concat(item);
}
callback(result);
}
}
});
}
});
}
};
var validProxy = function (proxies, callback) {
var count = 0;
var istime = function () {
if (++count >= proxies.length) {
var result = [];
for(var i in proxies) {
var item = proxies[i];
if(item.valid)
result.push(item);
}
callback(result);
}
};
for (var i in proxies) {
var proxy = proxies[i];
(function (proxy) {
var req = http.request({
host: proxy.ip,
port: proxy.port,
path: 'http://www.baidu.com',
method: 'GET'
}, function (res) {
res.on('data', function () {
});
res.on('end', function () {
proxy.valid = true;
istime();
});
}).on('error', function () {
proxy.valid = false;
istime();
}).on('socket', function(socket) {
socket.setTimeout(15000);
socket.on('timeout', function() {
proxy.valid = false;
req.abort();
});
});
req.end();
})(proxy);
}
};
exports.test = function (callback) {
domproxies(5, function (proxies) {
validProxy(proxies, function (proxies) {
console.log(proxies);
});
});
};