-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathproxy_pool.js
171 lines (151 loc) · 4.13 KB
/
proxy_pool.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const request = require('request')
const cheerio = require('cheerio')
const sqlite3 = require('sqlite3')
const db = new sqlite3.Database('Proxy.db', (err) => {
if(!err){
console.log('打开成功')
} else {
console.log(err)
}
})
db.run('CREATE TABLE proxy(ip char(15), port char(15), type char(15))',(err) => {})
const useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
const headers = {
'User-Agent': useragent,
}
//添加数据文件
const insertDb = function(ip, port, type){
db.run("INSERT INTO proxy VALUES(?, ?, ?)",[ip,port,type])
}
//提取优化文件数据
const clearN = function(l){
let index = 0
for (let i = 0; i < l.length; i++) {
if(l[i] === '' || l[i] === '\n'){
}else{
let ips = l[i].replace('\n','')
if (index === 0){
var ip = ips
console.log('爬取ip:' + ip)
} else if(index === 1){
var port = ips
} else if(index === 4){
var type = ips
}
index += 1
}
}
insertDb(ip, port, type)
}
//分析网页内容
const loadHtml = function(data){
let l = []
let e = cheerio.load(data)
e('tr').each(function(i, elem){
l[i] = e(this).text()
})
for (let i = 1; i < l.length; i ++){
clearN(l[i].split(' '))
}
}
//链接网络
const requestProxy = function(options){
return new Promise((resolve, reject) => {
request(options, function(err, response, body){
if(err === null && response.statusCode === 200){
loadHtml(body)
resolve()
} else {
console.log('链接失败')
resolve()
}
})
})
}
//生成网址
const ipUrl = function(resolve){
const url = 'http://www.xicidaili.com/nn/'
let options = {
url:'http://www.xicidaili.com/nn/',
headers,
}
let arr = []
return new Promise((resolve, reject) => {
for (let i = 1; i <= 5; i++) {
options.url = url + i
arr.push(requestProxy(options))
}
Promise.all(arr).then(function(){
resolve()
})
})
}
//从数据库提取所有ip
const allIp = function(callback){
return db.all('select * from proxy', callback)
}
//代理ip对象
const Proxys = function(ip,port,type){
this.ip = ip
this.port = port
this.type = type
}
//提取所有ip,通过check函数检查
const runIp = async function(){
let arr = []
allIp((err,response) => {
for (let i = 0; i < response.length; i++) {
let ip = response[i]
let proxy = new Proxys(ip.ip, ip.port, ip.type)
arr.push(check(proxy, headers))
}
Promise.all(arr).then(function(){
allIp((err, response)=>{
console.log('\n\n可用ip为:')
console.log(response)
})
})
})
}
//检测ip
const check = function(proxy, headers){
return new Promise((resolve, reject) => {
request({
url:'http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js',
proxy: `${proxy.type.toLowerCase()}://${proxy.ip}:${proxy.port}`,
method:'GET',
timeout: 2000,
headers,}
,function(err, response,body){
if(!err && response.statusCode == 200){
console.log(proxy.ip+' 链接成功:')
resolve()
} else {
console.log(proxy.ip+' 链接失败')
removeIp(proxy.ip)
resolve()
}
}
)
})
}
//删除命令
const removeIp = function(ip){
db.run(`DELETE FROM proxy WHERE ip = '${ ip }'`, function(err){
if(err){
console.log(err)
}else {
console.log('成功删除:'+ip)
}
})
}
exports.run = async function(){
await ipUrl()
await runIp()
}
exports.check = function(){
runIp()
}
exports.ips = function(callback){
allIp(callback)
}