mm_https 1.4.9 → 1.5.1

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 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
- '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',
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
- 'accept-encoding': 'deflate',
29
- 'accept-language': 'zh-CN,zh;q=0.9',
30
- 'cache-control': 'no-cache',
31
- 'pragma': 'no-cache',
32
- 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
33
- 'sec-ch-ua-mobile': '?0',
34
- 'sec-ch-ua-platform': '"Windows"',
35
- 'sec-fetch-dest': 'document',
36
- 'sec-fetch-mode': 'navigate',
37
- 'sec-fetch-site': 'none',
38
- 'sec-fetch-user': '?1',
39
- 'upgrade-insecure-requests': '1',
40
- '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'
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 kv = arr[0];
62
- var arr_kv = kv.split("=");
63
- name = arr_kv[0];
64
- this.cookies[name] = encodeURIComponent(arr_kv[1]);
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
- return str;
78
+ var cookie = str.replace("; ", "");
79
+ return cookie
75
80
  }
76
81
 
77
82
  Http.prototype.options = function(options) {
78
83
  return {
79
- url: "",
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
- url,
95
+ href,
91
96
  body,
92
97
  headers,
93
98
  method,
94
99
  cookie,
95
100
  type
96
101
  } = options;
97
- var protocol = url.left(":/") || 'http';
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.url = url;
218
+ options.href = url;
188
219
  op = this.http_options(options);
189
- op.referer = options.url;
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,25 @@ Http.prototype.request = function(options) {
209
272
  var {
210
273
  headers
211
274
  } = res;
212
- var buffer = '';
275
+ var chunks = [];
213
276
  var encoding = headers['content-encoding'];
214
- if (encoding) {
215
- res.setEncoding('utf-8');
216
- } else {
217
- res.setEncoding("binary");
218
- }
219
277
  res.on('data', function(d) {
220
- buffer += d;
278
+ chunks.push(d);
221
279
  }).on('end', function(d) {
280
+ var buffer = Buffer.concat(chunks);
222
281
  switch (encoding) {
223
282
  case 'br':
224
283
  case 'gzip':
225
284
  case 'deflate':
226
- body = unzipSync(buffer);
285
+ body = unzipSync(buffer).toString();
227
286
  break;
228
287
  default:
229
- body = buffer;
288
+ body = buffer.toString();
230
289
  break;
231
290
  }
232
291
  var mh = body.match(/charset=(gb2312|GBK)"/i);
233
292
  if (mh) {
234
- body = iconv.decode(buffer, 'GBK');
293
+ body = iconv.decode(body, 'GBK');
235
294
  }
236
295
  var lt = headers['set-cookie'];
237
296
  if (lt) {
@@ -243,7 +302,8 @@ Http.prototype.request = function(options) {
243
302
  resolve({
244
303
  status: 200,
245
304
  headers,
246
- body: body
305
+ buffer,
306
+ body
247
307
  });
248
308
  });
249
309
  } else {
@@ -281,7 +341,7 @@ Http.prototype.request = function(options) {
281
341
  Http.prototype.get = async function(url, headers, cookie) {
282
342
  return await this.req({
283
343
  method: "GET",
284
- url,
344
+ href: url,
285
345
  headers,
286
346
  cookie
287
347
  });
@@ -300,7 +360,7 @@ Http.prototype.get = async function(url, headers, cookie) {
300
360
  Http.prototype.post = async function(url, body, headers, type, cookie) {
301
361
  return await this.req({
302
362
  method: "POST",
303
- url,
363
+ href: url,
304
364
  body,
305
365
  type,
306
366
  headers,
@@ -340,7 +400,7 @@ Http.prototype.download = async function(url, filename, auto, headers, cookie) {
340
400
  file = filename;
341
401
  }
342
402
  if (file) {
343
- var length = await fs.writeFileSync(file, res.body, "binary");
403
+ var length = await fs.writeFileSync(file, res.buffer, "binary");
344
404
  if (length == 0) {
345
405
  file = null;
346
406
  }
package/logonew.png ADDED
Binary file
package/nodemon.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "restartable": "rs",
3
+ "ignore": [
4
+ ".git",
5
+ ".svn",
6
+ "node_modules/**/node_modules",
7
+ "static"
8
+ ],
9
+ "verbose": true,
10
+ "execMap": {
11
+ "": "node",
12
+ "js": "node --harmony"
13
+ },
14
+ "env": {
15
+ "NODE_ENV": "development"
16
+ },
17
+ "ext": "js ts json",
18
+ "legacy-watch": false
19
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_https",
3
- "version": "1.4.9",
3
+ "version": "1.5.1",
4
4
  "description": "这是超级美眉http请求帮助类模块,可自动补充cookie,实现模拟登录,通讯",
5
5
  "main": "index.js",
6
6
  "scripts": {
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,17 +94,30 @@ var fs = require("fs");
93
94
  // // console.log('结果:', hp.cookies);
94
95
  // }
95
96
 
96
- async function test () {
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
+ // console.log(res);
104
+ // }
105
+
106
+
107
+ async function test() {
97
108
  var hp = new Http();
98
- var res = await hp.get('http://www.hao123.com');
99
- console.log(res.body);
109
+ hp.config.headers.Accept = "application/javascript; charset=UTF-8";
110
+ var url ="http://localhost:8001/api/wallet/count";
111
+ var res = await hp.get(url);
112
+ console.log(res);
100
113
  }
101
114
 
102
115
  // async function test() {
116
+ // console.log("测试")
103
117
  // var hp = new Http();
104
- // var res = await hp.get('http://localhost:8080/dev/img/logo_gray.png');
118
+ // var res = await hp.get('https://www.baidu.com/img/flexible/logo/pc/result.png');
105
119
  // console.log(res);
106
- // fs.writeFile("./logonew.png", res.body, "binary", function(err) {
120
+ // fs.writeFile("./logonew.png", res.buffer, "binary", function(err) {
107
121
  // if (err) {
108
122
  // console.log("下载失败");
109
123
  // }
@@ -155,17 +169,17 @@ async function test () {
155
169
  // url: "http://www.hao123.com/test?dd=123"
156
170
  // });
157
171
  // console.log(4, options);
158
-
172
+
159
173
  // var options = http.new_options({
160
174
  // url: "http://www.hao123.com"
161
175
  // });
162
176
  // console.log(5, options);
163
-
177
+
164
178
  // options = http.new_options({
165
179
  // url: "https://www.hao123.com:8000"
166
180
  // });
167
181
  // console.log(6, options);
168
-
182
+
169
183
  // options = http.new_options({
170
184
  // url: "https://www.hao123.com?dd=123"
171
185
  // });
@@ -178,7 +192,7 @@ async function test () {
178
192
  // console.log("结果", res.body);
179
193
  // var res = await http.download('http://www.51sjk.com/Upload/Articles/1/0/8/8799_20200415005654483_0.jpg', "./demo.jpg");
180
194
  // // console.log(res);
181
-
195
+
182
196
  // // console.log("代理请求");
183
197
  // // var http = new Http({
184
198
  // // proxy: "http://127.0.0.1:10809"