mm_https 1.4.8 → 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 +117 -55
- package/package.json +1 -1
- package/test.js +14 -11
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');
|
|
@@ -15,9 +16,6 @@ const iconv = require('iconv-lite');
|
|
|
15
16
|
const {
|
|
16
17
|
hostname
|
|
17
18
|
} = require('os');
|
|
18
|
-
const {
|
|
19
|
-
header
|
|
20
|
-
} = require('request/lib/hawk');
|
|
21
19
|
const {
|
|
22
20
|
HttpsProxyAgent
|
|
23
21
|
} = require('https-proxy-agent');
|
|
@@ -26,21 +24,22 @@ class Http {
|
|
|
26
24
|
constructor(config) {
|
|
27
25
|
this.config = Object.assign({
|
|
28
26
|
headers: {
|
|
29
|
-
'
|
|
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',
|
|
30
28
|
// 'accept-encoding': 'gzip, deflate, br',
|
|
31
|
-
'
|
|
32
|
-
'
|
|
33
|
-
'
|
|
34
|
-
'
|
|
35
|
-
'
|
|
36
|
-
'sec-ch-ua
|
|
37
|
-
'sec-ch-ua-
|
|
38
|
-
'sec-
|
|
39
|
-
'sec-fetch-
|
|
40
|
-
'sec-fetch-
|
|
41
|
-
'sec-fetch-
|
|
42
|
-
'
|
|
43
|
-
'
|
|
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'
|
|
44
43
|
},
|
|
45
44
|
rejectUnauthorized: false,
|
|
46
45
|
cookie: "",
|
|
@@ -61,26 +60,28 @@ Http.prototype.set_cookie = function(cookieStr) {
|
|
|
61
60
|
var arr = cookieStr.split('; ');
|
|
62
61
|
var name;
|
|
63
62
|
if (arr.length > 1) {
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
+
}
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
Http.prototype.get_cookie = function() {
|
|
72
73
|
var dist = this.cookies;
|
|
73
74
|
var str = "";
|
|
74
|
-
for(var k in dist)
|
|
75
|
-
{
|
|
75
|
+
for (var k in dist) {
|
|
76
76
|
str += "; " + k + "=" + dist[k];
|
|
77
77
|
}
|
|
78
|
-
|
|
78
|
+
var cookie = str.replace("; ", "");
|
|
79
|
+
return cookie
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
Http.prototype.options = function(options) {
|
|
82
83
|
return {
|
|
83
|
-
|
|
84
|
+
href: "",
|
|
84
85
|
method: "GET",
|
|
85
86
|
body: {},
|
|
86
87
|
proxy: "http://127.0.0.1:10809",
|
|
@@ -91,41 +92,67 @@ Http.prototype.options = function(options) {
|
|
|
91
92
|
Http.prototype.http_options = function(options) {
|
|
92
93
|
var cg = this.config;
|
|
93
94
|
var {
|
|
94
|
-
|
|
95
|
+
href,
|
|
95
96
|
body,
|
|
96
97
|
headers,
|
|
97
98
|
method,
|
|
98
99
|
cookie,
|
|
99
100
|
type
|
|
100
101
|
} = options;
|
|
101
|
-
var
|
|
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){
|
|
102
|
+
var u = Url.parse(href, true);
|
|
103
|
+
if (cookie) {
|
|
112
104
|
this.set_cookie(cookie);
|
|
113
105
|
}
|
|
114
|
-
return {
|
|
115
|
-
url,
|
|
116
|
-
protocol,
|
|
117
|
-
hostname,
|
|
106
|
+
return Object.assign({}, {
|
|
118
107
|
body,
|
|
119
108
|
type,
|
|
120
|
-
port: Number(port),
|
|
121
|
-
path: path.indexOf("/") === 0 ? path : "/" + path,
|
|
122
109
|
method: method || "GET",
|
|
123
110
|
headers: headers ? Object.assign({}, cg.headers, headers) : Object.assign({}, cg.headers),
|
|
124
111
|
agent: cg.proxy ? new HttpProxyAgent(cg.proxy) : null,
|
|
125
112
|
cookie: this.get_cookie()
|
|
126
|
-
}
|
|
113
|
+
}, u);
|
|
127
114
|
}
|
|
128
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
|
+
|
|
129
156
|
Http.prototype.post_option = function(op) {
|
|
130
157
|
var param = op.body;
|
|
131
158
|
var body = "";
|
|
@@ -188,9 +215,9 @@ Http.prototype.req = async function(options) {
|
|
|
188
215
|
for (var i = 0; i < len; i++) {
|
|
189
216
|
_this.set_cookie(lt[i]);
|
|
190
217
|
}
|
|
191
|
-
options.
|
|
218
|
+
options.href = url;
|
|
192
219
|
op = this.http_options(options);
|
|
193
|
-
op.referer = options.
|
|
220
|
+
op.referer = options.href;
|
|
194
221
|
res = await this.request(op);
|
|
195
222
|
}
|
|
196
223
|
} else {
|
|
@@ -198,6 +225,38 @@ Http.prototype.req = async function(options) {
|
|
|
198
225
|
}
|
|
199
226
|
}
|
|
200
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
|
+
|
|
201
260
|
/**
|
|
202
261
|
* 请求
|
|
203
262
|
* @param {Object} options 配置
|
|
@@ -210,30 +269,33 @@ Http.prototype.request = function(options) {
|
|
|
210
269
|
var body = '';
|
|
211
270
|
var req = hp.request(options, (res) => {
|
|
212
271
|
if (res.statusCode == 200) {
|
|
213
|
-
var {
|
|
214
|
-
|
|
272
|
+
var {
|
|
273
|
+
headers
|
|
274
|
+
} = res;
|
|
275
|
+
var chunks = [];
|
|
215
276
|
var encoding = headers['content-encoding'];
|
|
216
277
|
if (encoding) {
|
|
217
|
-
res.setEncoding('utf-8');
|
|
278
|
+
// res.setEncoding('utf-8');
|
|
218
279
|
} else {
|
|
219
280
|
res.setEncoding("binary");
|
|
220
281
|
}
|
|
221
282
|
res.on('data', function(d) {
|
|
222
|
-
|
|
283
|
+
chunks.push(d);
|
|
223
284
|
}).on('end', function(d) {
|
|
285
|
+
var buffer = Buffer.concat(chunks);
|
|
224
286
|
switch (encoding) {
|
|
225
287
|
case 'br':
|
|
226
288
|
case 'gzip':
|
|
227
289
|
case 'deflate':
|
|
228
|
-
body = unzipSync(buffer);
|
|
290
|
+
body = unzipSync(buffer).toString();
|
|
229
291
|
break;
|
|
230
292
|
default:
|
|
231
|
-
body = buffer;
|
|
293
|
+
body = buffer.toString();
|
|
232
294
|
break;
|
|
233
295
|
}
|
|
234
296
|
var mh = body.match(/charset=(gb2312|GBK)"/i);
|
|
235
297
|
if (mh) {
|
|
236
|
-
body = iconv.decode(
|
|
298
|
+
body = iconv.decode(body, 'GBK');
|
|
237
299
|
}
|
|
238
300
|
var lt = headers['set-cookie'];
|
|
239
301
|
if (lt) {
|
|
@@ -283,7 +345,7 @@ Http.prototype.request = function(options) {
|
|
|
283
345
|
Http.prototype.get = async function(url, headers, cookie) {
|
|
284
346
|
return await this.req({
|
|
285
347
|
method: "GET",
|
|
286
|
-
url,
|
|
348
|
+
href: url,
|
|
287
349
|
headers,
|
|
288
350
|
cookie
|
|
289
351
|
});
|
|
@@ -302,7 +364,7 @@ Http.prototype.get = async function(url, headers, cookie) {
|
|
|
302
364
|
Http.prototype.post = async function(url, body, headers, type, cookie) {
|
|
303
365
|
return await this.req({
|
|
304
366
|
method: "POST",
|
|
305
|
-
url,
|
|
367
|
+
href: url,
|
|
306
368
|
body,
|
|
307
369
|
type,
|
|
308
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,11 +94,13 @@ var fs = require("fs");
|
|
|
93
94
|
// // console.log('结果:', hp.cookies);
|
|
94
95
|
// }
|
|
95
96
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
97
|
+
async function test() {
|
|
98
|
+
var hp = new Http();
|
|
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);
|
|
103
|
+
}
|
|
101
104
|
|
|
102
105
|
// async function test() {
|
|
103
106
|
// var hp = new Http();
|
|
@@ -155,17 +158,17 @@ var fs = require("fs");
|
|
|
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 @@ var fs = require("fs");
|
|
|
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"
|
|
@@ -187,4 +190,4 @@ var fs = require("fs");
|
|
|
187
190
|
// // console.log("结果", res.body);
|
|
188
191
|
// }
|
|
189
192
|
|
|
190
|
-
|
|
193
|
+
test();
|