mm_https 1.4.4 → 1.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +243 -315
- package/package.json +38 -37
- package/test.js +100 -22
package/index.js
CHANGED
|
@@ -3,324 +3,132 @@
|
|
|
3
3
|
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
4
|
* @version 1.2
|
|
5
5
|
*/
|
|
6
|
+
const http = require('http');
|
|
7
|
+
const https = require('https');
|
|
8
|
+
const HttpProxyAgent = require('https-proxy-agent');
|
|
6
9
|
const fs = require("fs");
|
|
7
10
|
const Cookie = require('./cookie');
|
|
8
11
|
const {
|
|
9
|
-
|
|
10
|
-
inflate
|
|
12
|
+
unzipSync
|
|
11
13
|
} = require('zlib');
|
|
12
|
-
const http = require('http');
|
|
13
|
-
const https = require('https');
|
|
14
14
|
const iconv = require('iconv-lite');
|
|
15
|
+
const {
|
|
16
|
+
hostname
|
|
17
|
+
} = require('os');
|
|
18
|
+
const {
|
|
19
|
+
header
|
|
20
|
+
} = require('request/lib/hawk');
|
|
21
|
+
const {
|
|
22
|
+
HttpsProxyAgent
|
|
23
|
+
} = require('https-proxy-agent');
|
|
15
24
|
|
|
16
|
-
/**
|
|
17
|
-
* @description 获取参数项
|
|
18
|
-
* @param {String} url 网址
|
|
19
|
-
* @return {Object} http参数对象
|
|
20
|
-
*/
|
|
21
|
-
function getInfo(url) {
|
|
22
|
-
var host = url.between("//", "/", true);
|
|
23
|
-
var path = "/" + url.replace("//").right("/");
|
|
24
|
-
var index = host.indexOf(':');
|
|
25
|
-
var port;
|
|
26
|
-
|
|
27
|
-
if (index !== -1) {
|
|
28
|
-
port = host.substr(index + 1);
|
|
29
|
-
host = host.substr(0, index);
|
|
30
|
-
} else {
|
|
31
|
-
if (url.indexOf('https') !== -1) {
|
|
32
|
-
port = 443;
|
|
33
|
-
} else {
|
|
34
|
-
port = 80;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
host: host,
|
|
39
|
-
port: port,
|
|
40
|
-
path: path
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* http类函数
|
|
46
|
-
*/
|
|
47
25
|
class Http {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
26
|
+
constructor(config) {
|
|
27
|
+
this.config = Object.assign({
|
|
28
|
+
headers: {
|
|
29
|
+
'accept': 'text/html,application/json,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
30
|
+
// 'accept-encoding': 'gzip, deflate, br',
|
|
31
|
+
'accept-encoding': 'deflate',
|
|
32
|
+
'accept-language': 'zh-CN,zh;q=0.9',
|
|
33
|
+
'cache-control': 'no-cache',
|
|
34
|
+
'pragma': 'no-cache',
|
|
35
|
+
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
|
|
36
|
+
'sec-ch-ua-mobile': '?0',
|
|
37
|
+
'sec-ch-ua-platform': '"Windows"',
|
|
38
|
+
'sec-fetch-dest': 'document',
|
|
39
|
+
'sec-fetch-mode': 'navigate',
|
|
40
|
+
'sec-fetch-site': 'none',
|
|
41
|
+
'sec-fetch-user': '?1',
|
|
42
|
+
'upgrade-insecure-requests': '1',
|
|
43
|
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
|
|
44
|
+
},
|
|
45
|
+
rejectUnauthorized: false,
|
|
46
|
+
cookie: "",
|
|
47
|
+
proxy: null
|
|
48
|
+
// proxy: "http://127.0.0.1:10809"
|
|
49
|
+
}, config);
|
|
61
50
|
|
|
62
51
|
/**
|
|
63
|
-
*
|
|
52
|
+
* cookie缓存
|
|
64
53
|
*/
|
|
65
|
-
this.
|
|
54
|
+
this.cookies = {
|
|
55
|
+
|
|
56
|
+
}
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.list.del({
|
|
78
|
-
name: name,
|
|
79
|
-
path: path,
|
|
80
|
-
domain: domain
|
|
81
|
-
});
|
|
82
|
-
} else if (path) {
|
|
83
|
-
this.list.del({
|
|
84
|
-
name: name,
|
|
85
|
-
path: path
|
|
86
|
-
});
|
|
87
|
-
} else {
|
|
88
|
-
this.list.del({
|
|
89
|
-
name: name
|
|
90
|
-
});
|
|
60
|
+
Http.prototype.set_cookie = function(cookieStr) {
|
|
61
|
+
var arr = cookieStr.split('; ');
|
|
62
|
+
var name;
|
|
63
|
+
if (arr.length > 1) {
|
|
64
|
+
var kv = arr[0];
|
|
65
|
+
var arr_kv = kv.split("=");
|
|
66
|
+
name = arr_kv[0];
|
|
67
|
+
this.cookies[name] = encodeURIComponent(arr_kv[1]);
|
|
91
68
|
}
|
|
92
|
-
}
|
|
69
|
+
}
|
|
93
70
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*/
|
|
101
|
-
Http.prototype.option = function(method, url, cookie) {
|
|
102
|
-
var {
|
|
103
|
-
host,
|
|
104
|
-
port,
|
|
105
|
-
path
|
|
106
|
-
} = getInfo(url);
|
|
107
|
-
var op = {
|
|
108
|
-
url: url,
|
|
109
|
-
hostname: host,
|
|
110
|
-
path: path,
|
|
111
|
-
port: port,
|
|
112
|
-
method: method,
|
|
113
|
-
headers: {
|
|
114
|
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
|
|
115
|
-
'Accept-Encoding': 'gzip, deflate',
|
|
116
|
-
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
117
|
-
'Cache-Control': 'max-age=0',
|
|
118
|
-
'Connection': 'keep-alive',
|
|
119
|
-
'Upgrade-Insecure-Requests': '1',
|
|
120
|
-
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
|
|
121
|
-
'X-Requested-With': 'XMLHttpRequest'
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
if (cookie) {
|
|
125
|
-
op.cookie = cookie;
|
|
126
|
-
} else if (this.cookie) {
|
|
127
|
-
op.cookie = this.cookie.str();
|
|
128
|
-
}
|
|
129
|
-
if (this.referer) {
|
|
130
|
-
op.Referer = this.referer;
|
|
71
|
+
Http.prototype.get_cookie = function() {
|
|
72
|
+
var dist = this.cookies;
|
|
73
|
+
var str = "";
|
|
74
|
+
for(var k in dist)
|
|
75
|
+
{
|
|
76
|
+
str += "; " + k + "=" + dist[k];
|
|
131
77
|
}
|
|
132
|
-
return
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* http请求
|
|
137
|
-
* @param {Object} options 请求配置
|
|
138
|
-
* @param {Object} param 请求参数
|
|
139
|
-
* @return {Promise|Object} 请求的结果
|
|
140
|
-
*/
|
|
141
|
-
Http.prototype.req = function(options, param) {
|
|
142
|
-
return new Promise((resolve, reject) => {
|
|
143
|
-
var hp;
|
|
144
|
-
var url = options.url;
|
|
145
|
-
if (url && url.indexOf('https') !== -1) {
|
|
146
|
-
hp = https;
|
|
147
|
-
} else {
|
|
148
|
-
hp = http;
|
|
149
|
-
}
|
|
150
|
-
var req = hp.request(options, function(res) {
|
|
151
|
-
var code = res.statusCode;
|
|
152
|
-
if (code === 200) {
|
|
153
|
-
var chunks = [];
|
|
154
|
-
var encoding = res.headers['content-encoding'];
|
|
155
|
-
// 非gzip/deflate要转成utf-8格式
|
|
156
|
-
if (encoding === 'undefined') {
|
|
157
|
-
res.setEncoding(this.encoding);
|
|
158
|
-
}
|
|
159
|
-
res.on('data', function(chunk) {
|
|
160
|
-
chunks.push(chunk);
|
|
161
|
-
});
|
|
162
|
-
res.on('end', function() {
|
|
163
|
-
var buffer = Buffer.concat(chunks);
|
|
164
|
-
if (encoding === 'gzip') {
|
|
165
|
-
gunzip(buffer, function(err, decoded) {
|
|
166
|
-
if (err) {
|
|
167
|
-
reject(err);
|
|
168
|
-
} else {
|
|
169
|
-
var body = decoded.toString();
|
|
170
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
171
|
-
if (mh) {
|
|
172
|
-
body = iconv.decode(decoded, 'GBK');
|
|
173
|
-
}
|
|
174
|
-
resolve({
|
|
175
|
-
headers: res.headers,
|
|
176
|
-
binary: buffer,
|
|
177
|
-
body: body
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
} else if (encoding === 'deflate') {
|
|
182
|
-
inflate(buffer, function(err, decoded) {
|
|
183
|
-
if (err) {
|
|
184
|
-
reject(err);
|
|
185
|
-
} else {
|
|
186
|
-
var body = decoded.toString();
|
|
187
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
188
|
-
if (mh) {
|
|
189
|
-
body = iconv.decode(decoded, 'GBK');
|
|
190
|
-
}
|
|
191
|
-
resolve({
|
|
192
|
-
headers: res.headers,
|
|
193
|
-
binary: buffer,
|
|
194
|
-
body: body
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
} else {
|
|
199
|
-
var body = buffer.toString();
|
|
200
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
201
|
-
if (mh) {
|
|
202
|
-
body = iconv.decode(buffer, 'GBK');
|
|
203
|
-
}
|
|
204
|
-
resolve({
|
|
205
|
-
headers: res.headers,
|
|
206
|
-
binary: buffer,
|
|
207
|
-
body: body
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
} else {
|
|
212
|
-
resolve({
|
|
213
|
-
status: code,
|
|
214
|
-
message: res.statusMessage,
|
|
215
|
-
headers: res.headers
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
if (param) {
|
|
220
|
-
req.write(param);
|
|
221
|
-
}
|
|
222
|
-
req.on("error", function(err) {
|
|
223
|
-
resolve({
|
|
224
|
-
status: 0,
|
|
225
|
-
message: err.message
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
req.end();
|
|
229
|
-
});
|
|
230
|
-
};
|
|
78
|
+
return str;
|
|
79
|
+
}
|
|
231
80
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
var res = await this.req(options, param);
|
|
240
|
-
if (res) {
|
|
241
|
-
if (res.status === 302) {
|
|
242
|
-
var url = res.headers.location;
|
|
243
|
-
var op = this.option(options.method, url);
|
|
244
|
-
res = await this.req(op, param);
|
|
245
|
-
if (res.status === 302) {
|
|
246
|
-
var url = res.headers.location;
|
|
247
|
-
op = this.option(options.method, url);
|
|
248
|
-
res = await this.req(op, param);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
var hd = res.headers;
|
|
252
|
-
if (hd) {
|
|
253
|
-
var lt = hd['set-cookie'];
|
|
254
|
-
if (lt) {
|
|
255
|
-
var len = lt.length;
|
|
256
|
-
for (var i = 0; i < len; i++) {
|
|
257
|
-
this.cookie.str(lt[i]);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
this.referer = options.url;
|
|
261
|
-
}
|
|
81
|
+
Http.prototype.options = function(options) {
|
|
82
|
+
return {
|
|
83
|
+
url: "",
|
|
84
|
+
method: "GET",
|
|
85
|
+
body: {},
|
|
86
|
+
proxy: "http://127.0.0.1:10809",
|
|
87
|
+
headers: Object.assign({}, this.config.headers)
|
|
262
88
|
}
|
|
263
|
-
|
|
264
|
-
};
|
|
89
|
+
}
|
|
265
90
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
91
|
+
Http.prototype.http_options = function(options) {
|
|
92
|
+
var cg = this.config;
|
|
93
|
+
var {
|
|
94
|
+
url,
|
|
95
|
+
body,
|
|
96
|
+
headers,
|
|
97
|
+
method,
|
|
98
|
+
cookie
|
|
99
|
+
} = options;
|
|
100
|
+
var protocol = url.left(":/") || 'http';
|
|
101
|
+
// 移除协议头 取端口号
|
|
102
|
+
var u = url.replace(protocol + "://", "");
|
|
103
|
+
var port = u.between(":", "/") || u.right(":") || (protocol === 'http' ? 80 : 443);
|
|
104
|
+
// 移除端口号,取主机地址
|
|
105
|
+
u = u.replace(":" + port, "");
|
|
106
|
+
var hostname = u.left("/", true).left("?", true);
|
|
107
|
+
// 移除主机地址,取路由路径
|
|
108
|
+
u = u.replace(hostname, "");
|
|
109
|
+
var path = u;
|
|
110
|
+
if(cookie){
|
|
111
|
+
this.set_cookie(cookie);
|
|
285
112
|
}
|
|
286
|
-
return
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
return await this.run(op);
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* @description GET请求 —— 快速
|
|
304
|
-
* @param {String} url 请求地址
|
|
305
|
-
* @return {Object} 请求的结果
|
|
306
|
-
*/
|
|
307
|
-
Http.prototype.get_fast = async function(url) {
|
|
308
|
-
var op = this.option('GET', url, {});
|
|
309
|
-
return await this.run_fast(op);
|
|
310
|
-
};
|
|
113
|
+
return {
|
|
114
|
+
url,
|
|
115
|
+
protocol,
|
|
116
|
+
hostname,
|
|
117
|
+
body,
|
|
118
|
+
port: Number(port),
|
|
119
|
+
path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
120
|
+
method: method || "GET",
|
|
121
|
+
headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
|
|
122
|
+
agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
|
|
123
|
+
cookie: this.get_cookie()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
311
126
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
* @param {String} url 请求地址
|
|
315
|
-
* @param {Object} param 正文参数
|
|
316
|
-
* @param {Object} headers 请求头
|
|
317
|
-
* @param {String} type 请求类型
|
|
318
|
-
* @param {String} cookie Cookie缓存
|
|
319
|
-
* @return {Object} 请求的结果
|
|
320
|
-
*/
|
|
321
|
-
Http.prototype.post = async function(url, param, headers, type, cookie) {
|
|
127
|
+
Http.prototype.post_option = function(op) {
|
|
128
|
+
var param = op.body;
|
|
322
129
|
var body = "";
|
|
323
130
|
var tp = typeof(param);
|
|
131
|
+
var type = op.type;
|
|
324
132
|
if (tp === "object") {
|
|
325
133
|
if (!type) {
|
|
326
134
|
type = 'application/json; charset=UTF-8';
|
|
@@ -354,30 +162,150 @@ Http.prototype.post = async function(url, param, headers, type, cookie) {
|
|
|
354
162
|
}
|
|
355
163
|
}
|
|
356
164
|
}
|
|
357
|
-
var op = this.option('POST', url, cookie);
|
|
358
165
|
op.body = body;
|
|
359
166
|
op.headers['Content-Type'] = type;
|
|
360
167
|
op.headers['Content-Length'] = Buffer.byteLength(body);
|
|
361
|
-
|
|
362
|
-
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* http请求
|
|
172
|
+
* @param {Object} options 配置
|
|
173
|
+
* @return {Object} 返回执行结果
|
|
174
|
+
*/
|
|
175
|
+
Http.prototype.req = async function(options) {
|
|
176
|
+
var op = this.http_options(options);
|
|
177
|
+
if (op.method.toLocaleUpperCase() === "POST") {
|
|
178
|
+
this.post_option(op);
|
|
179
|
+
}
|
|
180
|
+
var res = await this.request(op);
|
|
181
|
+
if (res.statusCode == 302) {
|
|
182
|
+
var url = headers.location;
|
|
183
|
+
var lt = headers['set-cookie'];
|
|
184
|
+
if (lt) {
|
|
185
|
+
var len = lt.length;
|
|
186
|
+
for (var i = 0; i < len; i++) {
|
|
187
|
+
_this.set_cookie(lt[i]);
|
|
188
|
+
}
|
|
189
|
+
options.url = url;
|
|
190
|
+
op = this.http_options(options);
|
|
191
|
+
op.referer = options.url;
|
|
192
|
+
res = await this.request(op);
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
return res;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 请求
|
|
201
|
+
* @param {Object} options 配置
|
|
202
|
+
*/
|
|
203
|
+
Http.prototype.request = function(options) {
|
|
204
|
+
var hp = options.protocol === "https" ? https : http;
|
|
205
|
+
delete options.protocol;
|
|
206
|
+
var _this = this;
|
|
207
|
+
return new Promise(function(resolve, reject) {
|
|
208
|
+
var body = '';
|
|
209
|
+
var req = hp.request(options, (res) => {
|
|
210
|
+
if (res.statusCode == 200) {
|
|
211
|
+
var { headers } = res;
|
|
212
|
+
var buffer = '';
|
|
213
|
+
var encoding = headers['content-encoding'];
|
|
214
|
+
if (encoding) {
|
|
215
|
+
res.setEncoding('utf-8');
|
|
216
|
+
} else {
|
|
217
|
+
res.setEncoding("binary");
|
|
218
|
+
}
|
|
219
|
+
res.on('data', function(d) {
|
|
220
|
+
buffer += d;
|
|
221
|
+
}).on('end', function(d) {
|
|
222
|
+
switch (encoding) {
|
|
223
|
+
case 'br':
|
|
224
|
+
case 'gzip':
|
|
225
|
+
case 'deflate':
|
|
226
|
+
body = unzipSync(buffer);
|
|
227
|
+
break;
|
|
228
|
+
default:
|
|
229
|
+
body = buffer;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
233
|
+
if (mh) {
|
|
234
|
+
body = iconv.decode(buffer, 'GBK');
|
|
235
|
+
}
|
|
236
|
+
var lt = headers['set-cookie'];
|
|
237
|
+
if (lt) {
|
|
238
|
+
var len = lt.length;
|
|
239
|
+
for (var i = 0; i < len; i++) {
|
|
240
|
+
_this.set_cookie(lt[i]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
resolve({
|
|
244
|
+
status: 200,
|
|
245
|
+
headers,
|
|
246
|
+
binary: buffer,
|
|
247
|
+
body: body
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
resolve({
|
|
252
|
+
status: {
|
|
253
|
+
code: res.statusCode,
|
|
254
|
+
message: res.statusMessage
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
reject(res);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
req.on("error", function(err) {
|
|
262
|
+
resolve({
|
|
263
|
+
status: 0,
|
|
264
|
+
message: err.message
|
|
265
|
+
});
|
|
266
|
+
reject(err);
|
|
267
|
+
});
|
|
268
|
+
if (options.method.toLocaleUpperCase() == "POST") {
|
|
269
|
+
req.write(options.body);
|
|
270
|
+
}
|
|
271
|
+
req.end();
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* @description GET请求
|
|
277
|
+
* @param {String} url 请求地址
|
|
278
|
+
* @param {Object} headers 请求头
|
|
279
|
+
* @param {Object} cookie 服务缓存
|
|
280
|
+
* @return {Object} 响应的结果
|
|
281
|
+
*/
|
|
282
|
+
Http.prototype.get = async function(url, headers, cookie) {
|
|
283
|
+
return await this.req({
|
|
284
|
+
method: "GET",
|
|
285
|
+
url,
|
|
286
|
+
headers,
|
|
287
|
+
cookie
|
|
288
|
+
});
|
|
363
289
|
};
|
|
364
290
|
|
|
291
|
+
|
|
365
292
|
/**
|
|
366
|
-
* @description POST
|
|
293
|
+
* @description POST提交
|
|
367
294
|
* @param {String} url 请求地址
|
|
368
|
-
* @param {
|
|
369
|
-
* @param {
|
|
370
|
-
* @
|
|
295
|
+
* @param {Object} body 提交参
|
|
296
|
+
* @param {Object} headers 请求头
|
|
297
|
+
* @param {Number} type 传参方式 json, form, text
|
|
298
|
+
* @param {Object} cookie 服务缓存
|
|
299
|
+
* @return {Object} 响应的结果
|
|
371
300
|
*/
|
|
372
|
-
Http.prototype.
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
return await this.run_fast(op, body);
|
|
301
|
+
Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
302
|
+
return await this.req({
|
|
303
|
+
method: "POST",
|
|
304
|
+
url,
|
|
305
|
+
body,
|
|
306
|
+
headers,
|
|
307
|
+
cookie
|
|
308
|
+
});
|
|
381
309
|
};
|
|
382
310
|
|
|
383
311
|
/**
|
|
@@ -404,8 +332,9 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
|
404
332
|
}
|
|
405
333
|
} else {
|
|
406
334
|
var content_type = res.headers["content-type"];
|
|
407
|
-
var type = content_type.replace("image/", "").replace("application/x-", "").replace("text/", "")
|
|
408
|
-
|
|
335
|
+
var type = content_type.replace("image/", "").replace("application/x-", "").replace("text/", "")
|
|
336
|
+
.replace("audio/",
|
|
337
|
+
"").replace("audio/", "").replace("jpeg", "jpg");
|
|
409
338
|
file = filename + "." + type;
|
|
410
339
|
}
|
|
411
340
|
} else {
|
|
@@ -427,8 +356,7 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
|
427
356
|
* 清除缓存
|
|
428
357
|
*/
|
|
429
358
|
Http.prototype.clear = function() {
|
|
430
|
-
this.
|
|
431
|
-
this.headers = [];
|
|
359
|
+
this.cookies = {};
|
|
432
360
|
};
|
|
433
361
|
|
|
434
362
|
if (global.$) {
|
package/package.json
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mm_https",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "node test.js"
|
|
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
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "mm_https",
|
|
3
|
+
"version": "1.4.7",
|
|
4
|
+
"description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"dev": "nodemon test.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/qiuwenwu/mm_https.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"myhttp",
|
|
16
|
+
"async",
|
|
17
|
+
"await",
|
|
18
|
+
"promise",
|
|
19
|
+
"add",
|
|
20
|
+
"del",
|
|
21
|
+
"set",
|
|
22
|
+
"get",
|
|
23
|
+
"query",
|
|
24
|
+
"run",
|
|
25
|
+
"exec"
|
|
26
|
+
],
|
|
27
|
+
"author": "邱文武",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/qiuwenwu/mm_https/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/qiuwenwu/mm_https#readme",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"https-proxy-agent": "^5.0.1",
|
|
35
|
+
"iconv-lite": "^0.5.0",
|
|
36
|
+
"mm_expand": "^1.4.7"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/test.js
CHANGED
|
@@ -20,10 +20,11 @@ var fs = require("fs");
|
|
|
20
20
|
// query: token
|
|
21
21
|
// };
|
|
22
22
|
// var res = await hp.post('https://openai.weixin.qq.com/openapi/message/' + TOKEN, pm);
|
|
23
|
-
// console.log(res.body)
|
|
23
|
+
// console.log(res.message, res.body)
|
|
24
24
|
// }
|
|
25
25
|
// test3();
|
|
26
26
|
|
|
27
|
+
|
|
27
28
|
// async function test2(content, from_user) {.
|
|
28
29
|
// 图灵机器人
|
|
29
30
|
// const apikey = "xxx";
|
|
@@ -53,25 +54,45 @@ var fs = require("fs");
|
|
|
53
54
|
// }
|
|
54
55
|
// test2("真的", "we21lkjq3l");
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
|
|
58
|
+
// // 测试
|
|
57
59
|
// async function test() {
|
|
58
|
-
// var hp = new Http(
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
// res = await hp.post("
|
|
60
|
+
// var hp = new Http({
|
|
61
|
+
// proxy: "http://127.0.0.1:10809"
|
|
62
|
+
// });
|
|
63
|
+
// res = await hp.post("https://accounts.binance.com/bapi/accounts/v2/public/authcenter/login", {
|
|
62
64
|
// 'phone': "15817188815",
|
|
63
65
|
// 'password': "asd123"
|
|
64
66
|
// }, null, 'form');
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
// console.log('结果:'
|
|
67
|
+
// var token = hp.cookies['x-auth-token'];
|
|
68
|
+
// // res = await hp.get("http://api.bitcentre.com.cn/paper/id?id=1", {
|
|
69
|
+
// // "x-auth-token": token
|
|
70
|
+
// // });
|
|
71
|
+
|
|
72
|
+
// // res = await hp.get("https://www.google.com");
|
|
73
|
+
// console.log('结果:', res);
|
|
74
|
+
// console.log('结果:', hp.cookies);
|
|
73
75
|
// }
|
|
74
76
|
|
|
77
|
+
// 测试
|
|
78
|
+
async function test() {
|
|
79
|
+
var hp = new Http();
|
|
80
|
+
// var res = await hp.get("http://www.baidu.com");
|
|
81
|
+
// console.log('百度:' + JSON.stringify(res));
|
|
82
|
+
res = await hp.post("http://127.0.0.1:8000/login", {
|
|
83
|
+
'phone': "15817188815",
|
|
84
|
+
'password': "asd123"
|
|
85
|
+
}, null, 'text');
|
|
86
|
+
console.log('登录:' + JSON.stringify(res));
|
|
87
|
+
var token = hp.cookies['x-auth-token'];
|
|
88
|
+
console.log('访问牌:' + token);
|
|
89
|
+
res = await hp.get("http://127.0.0.1:8000/paper/id?id=1", {
|
|
90
|
+
"x-auth-token": token || ""
|
|
91
|
+
});
|
|
92
|
+
console.log('结果:' + JSON.stringify(res));
|
|
93
|
+
console.log('结果:', hp.cookies);
|
|
94
|
+
}
|
|
95
|
+
|
|
75
96
|
// async function test () {
|
|
76
97
|
// var hp = new Http();
|
|
77
98
|
// var res = await hp.get_fast('http://www.hao123.com');
|
|
@@ -101,12 +122,69 @@ var fs = require("fs");
|
|
|
101
122
|
// test();
|
|
102
123
|
|
|
103
124
|
|
|
104
|
-
async function download(){
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
125
|
+
// async function download(){
|
|
126
|
+
// var hp = new Http();
|
|
127
|
+
// // var url = "http://www.haoduoma.com/d/file/p/2021-06-18/76df67e451ca9dec9991588ad7fda6e6.jpg";
|
|
128
|
+
// var url = "https://www.copipi.com/p/224/224/becce1fc21d723614b598f5377d5d676.jpg";
|
|
129
|
+
// "./二维码/中文".addDir();
|
|
130
|
+
// var filename = "./二维码/你好.jpg".fullname();
|
|
131
|
+
// var file = await hp.download(url, filename);
|
|
132
|
+
// console.log(file);
|
|
133
|
+
// }
|
|
134
|
+
// download();
|
|
135
|
+
|
|
136
|
+
// async function test() {
|
|
137
|
+
// var http = new Http();
|
|
138
|
+
// console.log("开始请求");
|
|
139
|
+
// var options = http.new_options({
|
|
140
|
+
// url: "http://www.hao123.com:8000/test?dd=123"
|
|
141
|
+
// });
|
|
142
|
+
// console.log(1, options);
|
|
143
|
+
|
|
144
|
+
// options = http.new_options({
|
|
145
|
+
// url: "www.hao123.com:8000/test?dd=123"
|
|
146
|
+
// });
|
|
147
|
+
// console.log(2, options);
|
|
148
|
+
|
|
149
|
+
// options = http.new_options({
|
|
150
|
+
// url: "www.hao123.com/test?dd=123"
|
|
151
|
+
// });
|
|
152
|
+
// console.log(3, options);
|
|
153
|
+
|
|
154
|
+
// var options = http.new_options({
|
|
155
|
+
// url: "http://www.hao123.com/test?dd=123"
|
|
156
|
+
// });
|
|
157
|
+
// console.log(4, options);
|
|
158
|
+
|
|
159
|
+
// var options = http.new_options({
|
|
160
|
+
// url: "http://www.hao123.com"
|
|
161
|
+
// });
|
|
162
|
+
// console.log(5, options);
|
|
163
|
+
|
|
164
|
+
// options = http.new_options({
|
|
165
|
+
// url: "https://www.hao123.com:8000"
|
|
166
|
+
// });
|
|
167
|
+
// console.log(6, options);
|
|
168
|
+
|
|
169
|
+
// options = http.new_options({
|
|
170
|
+
// url: "https://www.hao123.com?dd=123"
|
|
171
|
+
// });
|
|
172
|
+
// console.log(6, options);
|
|
173
|
+
// }
|
|
174
|
+
|
|
175
|
+
// async function test() {
|
|
176
|
+
// var http = new Http();
|
|
177
|
+
// var res = await http.get("https://www.hao123.com");
|
|
178
|
+
// console.log("结果", res.body);
|
|
179
|
+
// var res = await http.download('http://www.51sjk.com/Upload/Articles/1/0/8/8799_20200415005654483_0.jpg', "./demo.jpg");
|
|
180
|
+
// // console.log(res);
|
|
181
|
+
|
|
182
|
+
// // console.log("代理请求");
|
|
183
|
+
// // var http = new Http({
|
|
184
|
+
// // proxy: "http://127.0.0.1:10809"
|
|
185
|
+
// // });
|
|
186
|
+
// // var res = await http.get("https://www.binance.com/bapi/nft/v1/friendly/nft/nft-asset/asset-detail?nftInfoId=31849573");
|
|
187
|
+
// // console.log("结果", res.body);
|
|
188
|
+
// }
|
|
111
189
|
|
|
112
|
-
|
|
190
|
+
test();
|