-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.js
108 lines (90 loc) · 2.89 KB
/
request.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
'use strict';
const fetch = require('node-fetch')
const ProxyAgent = require('simple-proxy-agent')
const UserAgent = require('user-agents')
async function request(url, options, timeout = 5) {
try {
const res = await fetch(url, {
...options,
timeout: timeout * 1000
})
if(!res.ok) {
throw new Error('Invalid Response');
}
return res;
} catch (err) {
throw err;
}
}
async function singleRequestExecute(url, proxyAddr, options) {
return new Promise((resolve) => {
let res = {};
res.name = proxyAddr;
res.startMs = Date.now();
options.verboseLog('Queueing %s', proxyAddr)
let agent = ProxyAgent(options.protocol + '://' + proxyAddr, {
timeout: options.timeout * 1000,
tunnel: true
})
let headers = {}
if (typeof options['user-agent'] == 'undefined') {
headers['user-agent'] = new UserAgent().toString()
} else {
headers['user-agent'] = options['user-agent']
}
if (typeof options.header !== 'undefined' && options.header.length) {
options.header.forEach(elem => {
let el = elem.split(':').map(e => e.trim())
headers[el[0]] = el[1]
});
}
request(url, {agent, timeout: options.timeout, headers }, options.timeout)
.then(async (resp) => {
res = await requestProcess(res, resp, options)
})
.catch(err => {
res.success = false;
res.error = err.message.substring(0, 30).trim();
options.verboseLog('Failed response from %s', proxyAddr)
}).finally(() => {
res.time = ((Date.now() - res.startMs)/1000).toFixed(1);
resolve(res);
})
})
}
async function requestProcess(res, response, options) {
res.success = true;
let responseText = (await response.text());
options.verboseLog('Received response from %s, http code: %d', res.name, response.status)
if (options.code) {
let r = new RegExp(options.code);
if (r.test(response.status)) {
options.verboseLog('Code check for %s: success, http code: %d', res.name, response.status)
} else {
res.success = false;
res.error = 'Bad code:' + response.status;
options.verboseLog('Code check for %s: fail, http code: %d', res.name, response.status)
}
}
if (options.text) {
if (!responseText.includes(options.text)) {
res.success = false;
res.error = 'Expected text not found';
options.verboseLog('Text check for %s: fail', res.name)
} else {
options.verboseLog('Text check for %s: success', res.name)
}
}
if (options.notext) {
if (responseText.includes(options.notext)) {
res.success = false;
res.error = 'Not expected text found'
options.verboseLog('Notext check for %s: fail', res.name)
} else {
options.verboseLog('Notext check for %s: success', res.name)
}
}
res.response = responseText.substring(0, 30).trim();
return res;
}
module.exports = singleRequestExecute;