mm_https 1.4.5 → 1.4.8
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 +245 -321
- package/package.json +5 -4
- package/test.js +92 -21
package/index.js
CHANGED
|
@@ -3,329 +3,134 @@
|
|
|
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
|
-
* @constructor
|
|
51
|
-
* @param {Object} config 配置参数
|
|
52
|
-
*/
|
|
53
26
|
constructor(config) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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);
|
|
62
50
|
|
|
63
51
|
/**
|
|
64
|
-
*
|
|
52
|
+
* cookie缓存
|
|
65
53
|
*/
|
|
66
|
-
this.
|
|
54
|
+
this.cookies = {
|
|
67
55
|
|
|
68
|
-
|
|
69
|
-
headers: {
|
|
70
|
-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
|
|
71
|
-
'Accept-Encoding': 'gzip, deflate',
|
|
72
|
-
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
|
73
|
-
'Cache-Control': 'max-age=0',
|
|
74
|
-
'Connection': 'keep-alive',
|
|
75
|
-
'Upgrade-Insecure-Requests': '1',
|
|
76
|
-
'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',
|
|
77
|
-
'X-Requested-With': 'XMLHttpRequest'
|
|
78
|
-
},
|
|
79
|
-
rejectUnauthorized: false
|
|
80
|
-
}, config)
|
|
56
|
+
}
|
|
81
57
|
}
|
|
82
58
|
}
|
|
83
59
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
this.list.del({
|
|
93
|
-
name: name,
|
|
94
|
-
path: path,
|
|
95
|
-
domain: domain
|
|
96
|
-
});
|
|
97
|
-
} else if (path) {
|
|
98
|
-
this.list.del({
|
|
99
|
-
name: name,
|
|
100
|
-
path: path
|
|
101
|
-
});
|
|
102
|
-
} else {
|
|
103
|
-
this.list.del({
|
|
104
|
-
name: name
|
|
105
|
-
});
|
|
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]);
|
|
106
68
|
}
|
|
107
|
-
}
|
|
69
|
+
}
|
|
108
70
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
*/
|
|
116
|
-
Http.prototype.option = function(method, url, cookie) {
|
|
117
|
-
var {
|
|
118
|
-
host,
|
|
119
|
-
port,
|
|
120
|
-
path
|
|
121
|
-
} = getInfo(url);
|
|
122
|
-
var op = Object.assign({}, this.config, {
|
|
123
|
-
url: url,
|
|
124
|
-
hostname: host,
|
|
125
|
-
path: path,
|
|
126
|
-
port: port,
|
|
127
|
-
method: method
|
|
128
|
-
});
|
|
129
|
-
if (cookie) {
|
|
130
|
-
op.cookie = cookie;
|
|
131
|
-
} else if (this.cookie) {
|
|
132
|
-
op.cookie = this.cookie.str();
|
|
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];
|
|
133
77
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
return op;
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* http请求
|
|
142
|
-
* @param {Object} options 请求配置
|
|
143
|
-
* @param {Object} param 请求参数
|
|
144
|
-
* @return {Promise|Object} 请求的结果
|
|
145
|
-
*/
|
|
146
|
-
Http.prototype.req = function(options, param) {
|
|
147
|
-
return new Promise((resolve, reject) => {
|
|
148
|
-
var hp;
|
|
149
|
-
var url = options.url;
|
|
150
|
-
if (url && url.indexOf('https') !== -1) {
|
|
151
|
-
hp = https;
|
|
152
|
-
} else {
|
|
153
|
-
hp = http;
|
|
154
|
-
}
|
|
155
|
-
var req = hp.request(options, function(res) {
|
|
156
|
-
var code = res.statusCode;
|
|
157
|
-
if (code === 200) {
|
|
158
|
-
var chunks = [];
|
|
159
|
-
var encoding = res.headers['content-encoding'];
|
|
160
|
-
// 非gzip/deflate要转成utf-8格式
|
|
161
|
-
if (encoding === 'undefined') {
|
|
162
|
-
res.setEncoding(this.encoding);
|
|
163
|
-
}
|
|
164
|
-
res.on('data', function(chunk) {
|
|
165
|
-
chunks.push(chunk);
|
|
166
|
-
});
|
|
167
|
-
res.on('end', function() {
|
|
168
|
-
var buffer = Buffer.concat(chunks);
|
|
169
|
-
if (encoding === 'gzip') {
|
|
170
|
-
gunzip(buffer, function(err, decoded) {
|
|
171
|
-
if (err) {
|
|
172
|
-
reject(err);
|
|
173
|
-
} else {
|
|
174
|
-
var body = decoded.toString();
|
|
175
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
176
|
-
if (mh) {
|
|
177
|
-
body = iconv.decode(decoded, 'GBK');
|
|
178
|
-
}
|
|
179
|
-
resolve({
|
|
180
|
-
headers: res.headers,
|
|
181
|
-
binary: buffer,
|
|
182
|
-
body: body
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
} else if (encoding === 'deflate') {
|
|
187
|
-
inflate(buffer, function(err, decoded) {
|
|
188
|
-
if (err) {
|
|
189
|
-
reject(err);
|
|
190
|
-
} else {
|
|
191
|
-
var body = decoded.toString();
|
|
192
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
193
|
-
if (mh) {
|
|
194
|
-
body = iconv.decode(decoded, 'GBK');
|
|
195
|
-
}
|
|
196
|
-
resolve({
|
|
197
|
-
headers: res.headers,
|
|
198
|
-
binary: buffer,
|
|
199
|
-
body: body
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
} else {
|
|
204
|
-
var body = buffer.toString();
|
|
205
|
-
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
206
|
-
if (mh) {
|
|
207
|
-
body = iconv.decode(buffer, 'GBK');
|
|
208
|
-
}
|
|
209
|
-
resolve({
|
|
210
|
-
headers: res.headers,
|
|
211
|
-
binary: buffer,
|
|
212
|
-
body: body
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
} else {
|
|
217
|
-
resolve({
|
|
218
|
-
status: code,
|
|
219
|
-
message: res.statusMessage,
|
|
220
|
-
headers: res.headers
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
if (param) {
|
|
225
|
-
req.write(param);
|
|
226
|
-
}
|
|
227
|
-
req.on("error", function(err) {
|
|
228
|
-
resolve({
|
|
229
|
-
status: 0,
|
|
230
|
-
message: err.message
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
req.end();
|
|
234
|
-
});
|
|
235
|
-
};
|
|
78
|
+
return str;
|
|
79
|
+
}
|
|
236
80
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
var res = await this.req(options, param);
|
|
245
|
-
if (res) {
|
|
246
|
-
if (res.status === 302) {
|
|
247
|
-
var url = res.headers.location;
|
|
248
|
-
var op = this.option(options.method, url);
|
|
249
|
-
res = await this.req(op, param);
|
|
250
|
-
if (res.status === 302) {
|
|
251
|
-
var url = res.headers.location;
|
|
252
|
-
op = this.option(options.method, url);
|
|
253
|
-
res = await this.req(op, param);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
var hd = res.headers;
|
|
257
|
-
if (hd) {
|
|
258
|
-
var lt = hd['set-cookie'];
|
|
259
|
-
if (lt) {
|
|
260
|
-
var len = lt.length;
|
|
261
|
-
for (var i = 0; i < len; i++) {
|
|
262
|
-
this.cookie.str(lt[i]);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
this.referer = options.url;
|
|
266
|
-
}
|
|
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)
|
|
267
88
|
}
|
|
268
|
-
|
|
269
|
-
};
|
|
89
|
+
}
|
|
270
90
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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
|
+
type
|
|
100
|
+
} = options;
|
|
101
|
+
var protocol = url.left(":/") || 'http';
|
|
102
|
+
// 移除协议头 取端口号
|
|
103
|
+
var u = url.replace(protocol + "://", "");
|
|
104
|
+
var port = u.between(":", "/") || u.right(":") || (protocol === 'http' ? 80 : 443);
|
|
105
|
+
// 移除端口号,取主机地址
|
|
106
|
+
u = u.replace(":" + port, "");
|
|
107
|
+
var hostname = u.left("/", true).left("?", true);
|
|
108
|
+
// 移除主机地址,取路由路径
|
|
109
|
+
u = u.replace(hostname, "");
|
|
110
|
+
var path = u;
|
|
111
|
+
if(cookie){
|
|
112
|
+
this.set_cookie(cookie);
|
|
290
113
|
}
|
|
291
|
-
return
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* @description GET请求 —— 快速
|
|
309
|
-
* @param {String} url 请求地址
|
|
310
|
-
* @return {Object} 请求的结果
|
|
311
|
-
*/
|
|
312
|
-
Http.prototype.get_fast = async function(url) {
|
|
313
|
-
var op = this.option('GET', url, {});
|
|
314
|
-
return await this.run_fast(op);
|
|
315
|
-
};
|
|
114
|
+
return {
|
|
115
|
+
url,
|
|
116
|
+
protocol,
|
|
117
|
+
hostname,
|
|
118
|
+
body,
|
|
119
|
+
type,
|
|
120
|
+
port: Number(port),
|
|
121
|
+
path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
122
|
+
method: method || "GET",
|
|
123
|
+
headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
|
|
124
|
+
agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
|
|
125
|
+
cookie: this.get_cookie()
|
|
126
|
+
}
|
|
127
|
+
}
|
|
316
128
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
* @param {String} url 请求地址
|
|
320
|
-
* @param {Object} param 正文参数
|
|
321
|
-
* @param {Object} headers 请求头
|
|
322
|
-
* @param {String} type 请求类型
|
|
323
|
-
* @param {String} cookie Cookie缓存
|
|
324
|
-
* @return {Object} 请求的结果
|
|
325
|
-
*/
|
|
326
|
-
Http.prototype.post = async function(url, param, headers, type, cookie) {
|
|
129
|
+
Http.prototype.post_option = function(op) {
|
|
130
|
+
var param = op.body;
|
|
327
131
|
var body = "";
|
|
328
132
|
var tp = typeof(param);
|
|
133
|
+
var type = op.type;
|
|
329
134
|
if (tp === "object") {
|
|
330
135
|
if (!type) {
|
|
331
136
|
type = 'application/json; charset=UTF-8';
|
|
@@ -359,30 +164,150 @@ Http.prototype.post = async function(url, param, headers, type, cookie) {
|
|
|
359
164
|
}
|
|
360
165
|
}
|
|
361
166
|
}
|
|
362
|
-
var op = this.option('POST', url, cookie);
|
|
363
167
|
op.body = body;
|
|
364
168
|
op.headers['Content-Type'] = type;
|
|
365
169
|
op.headers['Content-Length'] = Buffer.byteLength(body);
|
|
366
|
-
|
|
367
|
-
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* http请求
|
|
174
|
+
* @param {Object} options 配置
|
|
175
|
+
* @return {Object} 返回执行结果
|
|
176
|
+
*/
|
|
177
|
+
Http.prototype.req = async function(options) {
|
|
178
|
+
var op = this.http_options(options);
|
|
179
|
+
if (op.method.toLocaleUpperCase() === "POST") {
|
|
180
|
+
this.post_option(op);
|
|
181
|
+
}
|
|
182
|
+
var res = await this.request(op);
|
|
183
|
+
if (res.statusCode == 302) {
|
|
184
|
+
var url = headers.location;
|
|
185
|
+
var lt = headers['set-cookie'];
|
|
186
|
+
if (lt) {
|
|
187
|
+
var len = lt.length;
|
|
188
|
+
for (var i = 0; i < len; i++) {
|
|
189
|
+
_this.set_cookie(lt[i]);
|
|
190
|
+
}
|
|
191
|
+
options.url = url;
|
|
192
|
+
op = this.http_options(options);
|
|
193
|
+
op.referer = options.url;
|
|
194
|
+
res = await this.request(op);
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
return res;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 请求
|
|
203
|
+
* @param {Object} options 配置
|
|
204
|
+
*/
|
|
205
|
+
Http.prototype.request = function(options) {
|
|
206
|
+
var hp = options.protocol === "https" ? https : http;
|
|
207
|
+
delete options.protocol;
|
|
208
|
+
var _this = this;
|
|
209
|
+
return new Promise(function(resolve, reject) {
|
|
210
|
+
var body = '';
|
|
211
|
+
var req = hp.request(options, (res) => {
|
|
212
|
+
if (res.statusCode == 200) {
|
|
213
|
+
var { headers } = res;
|
|
214
|
+
var buffer = '';
|
|
215
|
+
var encoding = headers['content-encoding'];
|
|
216
|
+
if (encoding) {
|
|
217
|
+
res.setEncoding('utf-8');
|
|
218
|
+
} else {
|
|
219
|
+
res.setEncoding("binary");
|
|
220
|
+
}
|
|
221
|
+
res.on('data', function(d) {
|
|
222
|
+
buffer += d;
|
|
223
|
+
}).on('end', function(d) {
|
|
224
|
+
switch (encoding) {
|
|
225
|
+
case 'br':
|
|
226
|
+
case 'gzip':
|
|
227
|
+
case 'deflate':
|
|
228
|
+
body = unzipSync(buffer);
|
|
229
|
+
break;
|
|
230
|
+
default:
|
|
231
|
+
body = buffer;
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
235
|
+
if (mh) {
|
|
236
|
+
body = iconv.decode(buffer, 'GBK');
|
|
237
|
+
}
|
|
238
|
+
var lt = headers['set-cookie'];
|
|
239
|
+
if (lt) {
|
|
240
|
+
var len = lt.length;
|
|
241
|
+
for (var i = 0; i < len; i++) {
|
|
242
|
+
_this.set_cookie(lt[i]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
resolve({
|
|
246
|
+
status: 200,
|
|
247
|
+
headers,
|
|
248
|
+
body: body
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
} else {
|
|
252
|
+
resolve({
|
|
253
|
+
status: {
|
|
254
|
+
code: res.statusCode,
|
|
255
|
+
message: res.statusMessage
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
reject(res);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
req.on("error", function(err) {
|
|
263
|
+
resolve({
|
|
264
|
+
status: 0,
|
|
265
|
+
message: err.message
|
|
266
|
+
});
|
|
267
|
+
reject(err);
|
|
268
|
+
});
|
|
269
|
+
if (options.method.toLocaleUpperCase() == "POST") {
|
|
270
|
+
req.write(options.body);
|
|
271
|
+
}
|
|
272
|
+
req.end();
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @description GET请求
|
|
278
|
+
* @param {String} url 请求地址
|
|
279
|
+
* @param {Object} headers 请求头
|
|
280
|
+
* @param {Object} cookie 服务缓存
|
|
281
|
+
* @return {Object} 响应的结果
|
|
282
|
+
*/
|
|
283
|
+
Http.prototype.get = async function(url, headers, cookie) {
|
|
284
|
+
return await this.req({
|
|
285
|
+
method: "GET",
|
|
286
|
+
url,
|
|
287
|
+
headers,
|
|
288
|
+
cookie
|
|
289
|
+
});
|
|
368
290
|
};
|
|
369
291
|
|
|
292
|
+
|
|
370
293
|
/**
|
|
371
|
-
* @description POST
|
|
294
|
+
* @description POST提交
|
|
372
295
|
* @param {String} url 请求地址
|
|
373
|
-
* @param {
|
|
374
|
-
* @param {
|
|
375
|
-
* @
|
|
296
|
+
* @param {Object} body 提交参
|
|
297
|
+
* @param {Object} headers 请求头
|
|
298
|
+
* @param {Number} type 传参方式 json, form, text
|
|
299
|
+
* @param {Object} cookie 服务缓存
|
|
300
|
+
* @return {Object} 响应的结果
|
|
376
301
|
*/
|
|
377
|
-
Http.prototype.
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
302
|
+
Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
303
|
+
return await this.req({
|
|
304
|
+
method: "POST",
|
|
305
|
+
url,
|
|
306
|
+
body,
|
|
307
|
+
type,
|
|
308
|
+
headers,
|
|
309
|
+
cookie
|
|
310
|
+
});
|
|
386
311
|
};
|
|
387
312
|
|
|
388
313
|
/**
|
|
@@ -395,10 +320,9 @@ Http.prototype.post_fast = async function(url, body, type) {
|
|
|
395
320
|
* @return {String} 成功返回保存路径,失败返回null
|
|
396
321
|
*/
|
|
397
322
|
Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
398
|
-
this.encoding = "binary";
|
|
399
323
|
var res = await this.get(url, headers, cookie);
|
|
400
324
|
var file = null;
|
|
401
|
-
if (res.
|
|
325
|
+
if (res.body) {
|
|
402
326
|
if (auto) {
|
|
403
327
|
var arr = url.split('/');
|
|
404
328
|
var fullname = arr[arr.length - 1];
|
|
@@ -409,15 +333,16 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
|
409
333
|
}
|
|
410
334
|
} else {
|
|
411
335
|
var content_type = res.headers["content-type"];
|
|
412
|
-
var type = content_type.replace("image/", "").replace("application/x-", "").replace("text/", "")
|
|
413
|
-
|
|
336
|
+
var type = content_type.replace("image/", "").replace("application/x-", "").replace("text/", "")
|
|
337
|
+
.replace("audio/",
|
|
338
|
+
"").replace("audio/", "").replace("jpeg", "jpg");
|
|
414
339
|
file = filename + "." + type;
|
|
415
340
|
}
|
|
416
341
|
} else {
|
|
417
342
|
file = filename;
|
|
418
343
|
}
|
|
419
344
|
if (file) {
|
|
420
|
-
var length = await fs.writeFileSync(file, res.
|
|
345
|
+
var length = await fs.writeFileSync(file, res.body, "binary");
|
|
421
346
|
if (length == 0) {
|
|
422
347
|
file = null;
|
|
423
348
|
}
|
|
@@ -432,8 +357,7 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
|
|
|
432
357
|
* 清除缓存
|
|
433
358
|
*/
|
|
434
359
|
Http.prototype.clear = function() {
|
|
435
|
-
this.
|
|
436
|
-
this.headers = [];
|
|
360
|
+
this.cookies = {};
|
|
437
361
|
};
|
|
438
362
|
|
|
439
363
|
if (global.$) {
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_https",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.8",
|
|
4
4
|
"description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test.js"
|
|
7
|
+
"test": "node test.js",
|
|
8
|
+
"dev": "nodemon test.js"
|
|
8
9
|
},
|
|
9
10
|
"repository": {
|
|
10
11
|
"type": "git",
|
|
@@ -30,8 +31,8 @@
|
|
|
30
31
|
},
|
|
31
32
|
"homepage": "https://github.com/qiuwenwu/mm_https#readme",
|
|
32
33
|
"dependencies": {
|
|
34
|
+
"https-proxy-agent": "^5.0.1",
|
|
33
35
|
"iconv-lite": "^0.5.0",
|
|
34
36
|
"mm_expand": "^1.4.7"
|
|
35
|
-
}
|
|
36
|
-
"devDependencies": {}
|
|
37
|
+
}
|
|
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,23 +54,43 @@ var fs = require("fs");
|
|
|
53
54
|
// }
|
|
54
55
|
// test2("真的", "we21lkjq3l");
|
|
55
56
|
|
|
57
|
+
|
|
58
|
+
// // 测试
|
|
59
|
+
// async function test() {
|
|
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", {
|
|
64
|
+
// 'phone': "15817188815",
|
|
65
|
+
// 'password': "asd123"
|
|
66
|
+
// }, null, 'form');
|
|
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);
|
|
75
|
+
// }
|
|
76
|
+
|
|
56
77
|
// 测试
|
|
57
78
|
// async function test() {
|
|
58
79
|
// var hp = new Http();
|
|
59
80
|
// // var res = await hp.get("http://www.baidu.com");
|
|
60
81
|
// // console.log('百度:' + JSON.stringify(res));
|
|
61
|
-
// res = await hp.post("http://
|
|
82
|
+
// res = await hp.post("http://127.0.0.1:8000/login", {
|
|
62
83
|
// 'phone': "15817188815",
|
|
63
84
|
// 'password': "asd123"
|
|
64
|
-
// }, null, '
|
|
85
|
+
// }, null, 'json');
|
|
65
86
|
// console.log('登录:' + JSON.stringify(res));
|
|
66
|
-
//
|
|
67
|
-
// var token = hp.cookie.get('x-auth-token').value;
|
|
87
|
+
// var token = hp.cookies['x-auth-token'];
|
|
68
88
|
// console.log('访问牌:' + token);
|
|
69
|
-
// res = await hp.get("http://
|
|
70
|
-
//
|
|
71
|
-
// });
|
|
72
|
-
// console.log('结果:' + JSON.stringify(res));
|
|
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);
|
|
73
94
|
// }
|
|
74
95
|
|
|
75
96
|
// async function test () {
|
|
@@ -80,9 +101,9 @@ var fs = require("fs");
|
|
|
80
101
|
|
|
81
102
|
// async function test() {
|
|
82
103
|
// var hp = new Http();
|
|
83
|
-
// hp.encoding = "binary";
|
|
84
104
|
// var res = await hp.get('http://localhost:8080/dev/img/logo_gray.png');
|
|
85
|
-
//
|
|
105
|
+
// console.log(res);
|
|
106
|
+
// fs.writeFile("./logonew.png", res.body, "binary", function(err) {
|
|
86
107
|
// if (err) {
|
|
87
108
|
// console.log("下载失败");
|
|
88
109
|
// }
|
|
@@ -103,17 +124,67 @@ var fs = require("fs");
|
|
|
103
124
|
|
|
104
125
|
// async function download(){
|
|
105
126
|
// var hp = new Http();
|
|
106
|
-
// var url = "
|
|
107
|
-
// "
|
|
108
|
-
//
|
|
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);
|
|
109
132
|
// console.log(file);
|
|
110
133
|
// }
|
|
134
|
+
// download();
|
|
111
135
|
|
|
112
|
-
async function test(){
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
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
|
+
// }
|
|
118
189
|
|
|
119
|
-
test();
|
|
190
|
+
// test();
|