mm_https 1.4.9 → 1.5.0
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 +112 -48
- package/package.json +1 -1
- package/test.js +11 -8
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
const http = require('http');
|
|
7
7
|
const https = require('https');
|
|
8
|
+
const Url = require('url');
|
|
8
9
|
const HttpProxyAgent = require('https-proxy-agent');
|
|
9
10
|
const fs = require("fs");
|
|
10
11
|
const Cookie = require('./cookie');
|
|
@@ -23,21 +24,22 @@ class Http {
|
|
|
23
24
|
constructor(config) {
|
|
24
25
|
this.config = Object.assign({
|
|
25
26
|
headers: {
|
|
26
|
-
'
|
|
27
|
+
'Accept': 'text/html,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',
|
|
27
28
|
// 'accept-encoding': 'gzip, deflate, br',
|
|
28
|
-
'
|
|
29
|
-
'
|
|
30
|
-
'
|
|
31
|
-
'
|
|
32
|
-
'
|
|
33
|
-
'sec-ch-ua
|
|
34
|
-
'sec-ch-ua-
|
|
35
|
-
'sec-
|
|
36
|
-
'sec-fetch-
|
|
37
|
-
'sec-fetch-
|
|
38
|
-
'sec-fetch-
|
|
39
|
-
'
|
|
40
|
-
'
|
|
29
|
+
'Accept-Encoding': 'gzip, deflate',
|
|
30
|
+
'Accept-Language': 'zh-CN,zh;q=0.9',
|
|
31
|
+
'Cache-Control': 'no-cache',
|
|
32
|
+
'Connection': 'keep-alive',
|
|
33
|
+
'Pragma': 'no-cache',
|
|
34
|
+
// 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
|
|
35
|
+
// 'sec-ch-ua-mobile': '?0',
|
|
36
|
+
// 'sec-ch-ua-platform': '"Windows"',
|
|
37
|
+
// 'sec-fetch-dest': 'document',
|
|
38
|
+
// 'sec-fetch-mode': 'navigate',
|
|
39
|
+
// 'sec-fetch-site': 'none',
|
|
40
|
+
// 'sec-fetch-user': '?1',
|
|
41
|
+
'Upgrade-Insecure-Requests': '1',
|
|
42
|
+
'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'
|
|
41
43
|
},
|
|
42
44
|
rejectUnauthorized: false,
|
|
43
45
|
cookie: "",
|
|
@@ -58,10 +60,12 @@ Http.prototype.set_cookie = function(cookieStr) {
|
|
|
58
60
|
var arr = cookieStr.split('; ');
|
|
59
61
|
var name;
|
|
60
62
|
if (arr.length > 1) {
|
|
61
|
-
var
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
for (var i = 0; i < arr.length; i++) {
|
|
64
|
+
var kv = arr[i];
|
|
65
|
+
var arr_kv = kv.split("=");
|
|
66
|
+
name = arr_kv[0];
|
|
67
|
+
this.cookies[name] = encodeURIComponent(arr_kv[1]);
|
|
68
|
+
}
|
|
65
69
|
}
|
|
66
70
|
}
|
|
67
71
|
|
|
@@ -71,12 +75,13 @@ Http.prototype.get_cookie = function() {
|
|
|
71
75
|
for (var k in dist) {
|
|
72
76
|
str += "; " + k + "=" + dist[k];
|
|
73
77
|
}
|
|
74
|
-
|
|
78
|
+
var cookie = str.replace("; ", "");
|
|
79
|
+
return cookie
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
Http.prototype.options = function(options) {
|
|
78
83
|
return {
|
|
79
|
-
|
|
84
|
+
href: "",
|
|
80
85
|
method: "GET",
|
|
81
86
|
body: {},
|
|
82
87
|
proxy: "http://127.0.0.1:10809",
|
|
@@ -87,41 +92,67 @@ Http.prototype.options = function(options) {
|
|
|
87
92
|
Http.prototype.http_options = function(options) {
|
|
88
93
|
var cg = this.config;
|
|
89
94
|
var {
|
|
90
|
-
|
|
95
|
+
href,
|
|
91
96
|
body,
|
|
92
97
|
headers,
|
|
93
98
|
method,
|
|
94
99
|
cookie,
|
|
95
100
|
type
|
|
96
101
|
} = options;
|
|
97
|
-
var
|
|
98
|
-
// 移除协议头 取端口号
|
|
99
|
-
var u = url.replace(protocol + "://", "");
|
|
100
|
-
var port = u.between(":", "/") || u.right(":") || (protocol === 'http' ? 80 : 443);
|
|
101
|
-
// 移除端口号,取主机地址
|
|
102
|
-
u = u.replace(":" + port, "");
|
|
103
|
-
var hostname = u.left("/", true).left("?", true);
|
|
104
|
-
// 移除主机地址,取路由路径
|
|
105
|
-
u = u.replace(hostname, "");
|
|
106
|
-
var path = u;
|
|
102
|
+
var u = Url.parse(href, true);
|
|
107
103
|
if (cookie) {
|
|
108
104
|
this.set_cookie(cookie);
|
|
109
105
|
}
|
|
110
|
-
return {
|
|
111
|
-
url,
|
|
112
|
-
protocol,
|
|
113
|
-
hostname,
|
|
106
|
+
return Object.assign({}, {
|
|
114
107
|
body,
|
|
115
108
|
type,
|
|
116
|
-
port: Number(port),
|
|
117
|
-
path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
118
109
|
method: method || "GET",
|
|
119
110
|
headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
|
|
120
111
|
agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
|
|
121
112
|
cookie: this.get_cookie()
|
|
122
|
-
}
|
|
113
|
+
}, u);
|
|
123
114
|
}
|
|
124
115
|
|
|
116
|
+
// Http.prototype.http_options = function(options) {
|
|
117
|
+
// var cg = this.config;
|
|
118
|
+
// var {
|
|
119
|
+
// href,
|
|
120
|
+
// body,
|
|
121
|
+
// headers,
|
|
122
|
+
// method,
|
|
123
|
+
// cookie,
|
|
124
|
+
// type
|
|
125
|
+
// } = options;
|
|
126
|
+
// var protocol = href.left(":/") || 'http';
|
|
127
|
+
// // 移除协议头 取端口号
|
|
128
|
+
// var u = href.replace(protocol + "://", "");
|
|
129
|
+
// var port = u.between(":", "/") || u.left("/", true).right(":") || (protocol === 'http' ? 80 : 443);
|
|
130
|
+
|
|
131
|
+
// // 移除端口号,取主机地址
|
|
132
|
+
// u = u.replace(":" + port, "");
|
|
133
|
+
// var hostname = u.left("/", true).left("?", true);
|
|
134
|
+
// // 移除主机地址,取路由路径
|
|
135
|
+
// u = u.replace(hostname, "");
|
|
136
|
+
// var path = u.left("?", true);
|
|
137
|
+
// if (cookie) {
|
|
138
|
+
// this.set_cookie(cookie);
|
|
139
|
+
// }
|
|
140
|
+
// return {
|
|
141
|
+
// href,
|
|
142
|
+
// protocol,
|
|
143
|
+
// hostname,
|
|
144
|
+
// body,
|
|
145
|
+
// type,
|
|
146
|
+
// port: Number(port),
|
|
147
|
+
// search: "?" + u.right("?"),
|
|
148
|
+
// path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
149
|
+
// method: method || "GET",
|
|
150
|
+
// headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
|
|
151
|
+
// agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
|
|
152
|
+
// cookie: this.get_cookie()
|
|
153
|
+
// }
|
|
154
|
+
// }
|
|
155
|
+
|
|
125
156
|
Http.prototype.post_option = function(op) {
|
|
126
157
|
var param = op.body;
|
|
127
158
|
var body = "";
|
|
@@ -184,9 +215,9 @@ Http.prototype.req = async function(options) {
|
|
|
184
215
|
for (var i = 0; i < len; i++) {
|
|
185
216
|
_this.set_cookie(lt[i]);
|
|
186
217
|
}
|
|
187
|
-
options.
|
|
218
|
+
options.href = url;
|
|
188
219
|
op = this.http_options(options);
|
|
189
|
-
op.referer = options.
|
|
220
|
+
op.referer = options.href;
|
|
190
221
|
res = await this.request(op);
|
|
191
222
|
}
|
|
192
223
|
} else {
|
|
@@ -194,6 +225,38 @@ Http.prototype.req = async function(options) {
|
|
|
194
225
|
}
|
|
195
226
|
}
|
|
196
227
|
|
|
228
|
+
Http.prototype.undata = function(encoding) {
|
|
229
|
+
var bufarr = [];
|
|
230
|
+
var errored = false;
|
|
231
|
+
var zpipe;
|
|
232
|
+
if (encoding) {
|
|
233
|
+
zpipe = zlib.createGunzip();
|
|
234
|
+
} else {
|
|
235
|
+
zpipe = zlib.createInflate();
|
|
236
|
+
}
|
|
237
|
+
zpipe.on('data', function(d) {
|
|
238
|
+
bufarr.push(d);
|
|
239
|
+
})
|
|
240
|
+
.on('end', function() {
|
|
241
|
+
if (errored) return;
|
|
242
|
+
errored = true;
|
|
243
|
+
cb(null, response, enc ? Buffer.concat(bufarr).toString(
|
|
244
|
+
enc) : Buffer.concat(bufarr));
|
|
245
|
+
})
|
|
246
|
+
.on('error', function(err) {
|
|
247
|
+
if (errored) return;
|
|
248
|
+
errored = true;
|
|
249
|
+
cb(err, response, null);
|
|
250
|
+
});
|
|
251
|
+
response.pipe(zpipe);
|
|
252
|
+
response
|
|
253
|
+
.on('error', function(err) {
|
|
254
|
+
if (errored) return;
|
|
255
|
+
errored = true;
|
|
256
|
+
cb(err, response, null);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
197
260
|
/**
|
|
198
261
|
* 请求
|
|
199
262
|
* @param {Object} options 配置
|
|
@@ -209,29 +272,30 @@ Http.prototype.request = function(options) {
|
|
|
209
272
|
var {
|
|
210
273
|
headers
|
|
211
274
|
} = res;
|
|
212
|
-
var
|
|
275
|
+
var chunks = [];
|
|
213
276
|
var encoding = headers['content-encoding'];
|
|
214
277
|
if (encoding) {
|
|
215
|
-
res.setEncoding('utf-8');
|
|
278
|
+
// res.setEncoding('utf-8');
|
|
216
279
|
} else {
|
|
217
280
|
res.setEncoding("binary");
|
|
218
281
|
}
|
|
219
282
|
res.on('data', function(d) {
|
|
220
|
-
|
|
283
|
+
chunks.push(d);
|
|
221
284
|
}).on('end', function(d) {
|
|
285
|
+
var buffer = Buffer.concat(chunks);
|
|
222
286
|
switch (encoding) {
|
|
223
287
|
case 'br':
|
|
224
288
|
case 'gzip':
|
|
225
289
|
case 'deflate':
|
|
226
|
-
body = unzipSync(buffer);
|
|
290
|
+
body = unzipSync(buffer).toString();
|
|
227
291
|
break;
|
|
228
292
|
default:
|
|
229
|
-
body = buffer;
|
|
293
|
+
body = buffer.toString();
|
|
230
294
|
break;
|
|
231
295
|
}
|
|
232
296
|
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
233
297
|
if (mh) {
|
|
234
|
-
body = iconv.decode(
|
|
298
|
+
body = iconv.decode(body, 'GBK');
|
|
235
299
|
}
|
|
236
300
|
var lt = headers['set-cookie'];
|
|
237
301
|
if (lt) {
|
|
@@ -281,7 +345,7 @@ Http.prototype.request = function(options) {
|
|
|
281
345
|
Http.prototype.get = async function(url, headers, cookie) {
|
|
282
346
|
return await this.req({
|
|
283
347
|
method: "GET",
|
|
284
|
-
url,
|
|
348
|
+
href: url,
|
|
285
349
|
headers,
|
|
286
350
|
cookie
|
|
287
351
|
});
|
|
@@ -300,7 +364,7 @@ Http.prototype.get = async function(url, headers, cookie) {
|
|
|
300
364
|
Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
301
365
|
return await this.req({
|
|
302
366
|
method: "POST",
|
|
303
|
-
url,
|
|
367
|
+
href: url,
|
|
304
368
|
body,
|
|
305
369
|
type,
|
|
306
370
|
headers,
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// const nJwt = require('njwt');
|
|
2
|
+
var http = require('http');
|
|
2
3
|
var Http = require('./index.js');
|
|
3
4
|
var fs = require("fs");
|
|
4
5
|
|
|
@@ -68,7 +69,7 @@ var fs = require("fs");
|
|
|
68
69
|
// // res = await hp.get("http://api.bitcentre.com.cn/paper/id?id=1", {
|
|
69
70
|
// // "x-auth-token": token
|
|
70
71
|
// // });
|
|
71
|
-
|
|
72
|
+
|
|
72
73
|
// // res = await hp.get("https://www.google.com");
|
|
73
74
|
// console.log('结果:', res);
|
|
74
75
|
// console.log('结果:', hp.cookies);
|
|
@@ -93,10 +94,12 @@ var fs = require("fs");
|
|
|
93
94
|
// // console.log('结果:', hp.cookies);
|
|
94
95
|
// }
|
|
95
96
|
|
|
96
|
-
async function test
|
|
97
|
+
async function test() {
|
|
97
98
|
var hp = new Http();
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
hp.config.headers.Accept = "application/javascript; charset=UTF-8";
|
|
100
|
+
var url =
|
|
101
|
+
"http://69.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124007023275733383882_1586573202818&pn=1&pz=100000&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152";
|
|
102
|
+
var res = await hp.get(url);
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
// async function test() {
|
|
@@ -155,17 +158,17 @@ async function test () {
|
|
|
155
158
|
// url: "http://www.hao123.com/test?dd=123"
|
|
156
159
|
// });
|
|
157
160
|
// console.log(4, options);
|
|
158
|
-
|
|
161
|
+
|
|
159
162
|
// var options = http.new_options({
|
|
160
163
|
// url: "http://www.hao123.com"
|
|
161
164
|
// });
|
|
162
165
|
// console.log(5, options);
|
|
163
|
-
|
|
166
|
+
|
|
164
167
|
// options = http.new_options({
|
|
165
168
|
// url: "https://www.hao123.com:8000"
|
|
166
169
|
// });
|
|
167
170
|
// console.log(6, options);
|
|
168
|
-
|
|
171
|
+
|
|
169
172
|
// options = http.new_options({
|
|
170
173
|
// url: "https://www.hao123.com?dd=123"
|
|
171
174
|
// });
|
|
@@ -178,7 +181,7 @@ async function test () {
|
|
|
178
181
|
// console.log("结果", res.body);
|
|
179
182
|
// var res = await http.download('http://www.51sjk.com/Upload/Articles/1/0/8/8799_20200415005654483_0.jpg', "./demo.jpg");
|
|
180
183
|
// // console.log(res);
|
|
181
|
-
|
|
184
|
+
|
|
182
185
|
// // console.log("代理请求");
|
|
183
186
|
// // var http = new Http({
|
|
184
187
|
// // proxy: "http://127.0.0.1:10809"
|