-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy path_lib.request.js
107 lines (97 loc) · 2.92 KB
/
_lib.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
const iconv = require('iconv-lite');
async function requestHtml(url, options) {
try {
let html = (await req(url, options)).content;
// log(html);
return html
} catch (e) {
log(`requestHtml error:${e.message}`);
return ''
}
}
async function requestJson(url, options) {
try {
let html = (await req(url, options)).content;
return JSON.parse(html)
} catch (e) {
log(`requestJson error:${e.message}`);
return {}
}
}
async function getPublicIp() {
let ip_obj = await requestJson('http://httpbin.org/ip');
// log('ip_obj:',ip_obj);
return ip_obj.origin
}
async function getHtml(config) {
try {
return await axios.request(typeof config === "string" ? config : {
url: config.url,
method: config.method || 'GET',
headers: config.headers || {
'User-Agent': PC_UA
},
data: config.data || '',
responseType: config.responseType || '',//'arraybuffer'
})
} catch (e) {
return e.response
}
}
async function req_(reqUrl, mt, headers, data) {
let config = {
method: mt || 'Get',
url: reqUrl,
headers: headers || {
'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
},
data: data || '',
};
let res = await axios.request(config);
return res.data;
}
async function req_encoding(reqUrl, mt, headers, encoding, data) {
let config = {
method: mt || 'Get',
url: reqUrl,
headers: headers || {
'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
},
data: data || '',
responseType: 'arraybuffer'
};
let res = await axios.request(config);
if (encoding) {
res.data = iconv.decode(res.data, encoding);
}
return res.data;
}
async function req_proxy(reqUrl, mt, headers, data) {
let config = {
method: mt || 'Get',
url: reqUrl,
headers: headers || {
'User-Agent': 'Mozilla/5.0 (Linux; Android 9; RMX1931 Build/PQ3A.190605.05081124; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36 QSTAPP/1.6.9 Html5Plus/1.0',
},
proxy: {
protocol: 'http',
host: '127.0.0.1',
port: "7890"
}
};
if (data) {
config.data = data;
}
let res = await axios.request(config);
return res.data;
}
$.exports = {
requestHtml,
requestJson,
getPublicIp,
getHtml,
req_,
req_encoding,
req_proxy,
// axios // 没法import系统库
}