node-mitmproxy-pro 1.0.0 → 1.1.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.
@@ -0,0 +1,262 @@
1
+ 'use strict';
2
+
3
+ var forge = require('node-forge');
4
+ var fs = require('fs');
5
+ var path = require('path');
6
+ var config = require('../common/config');
7
+ var _ = require('lodash');
8
+ var mkdirp = require('mkdirp');
9
+ var colors = require('colors');
10
+
11
+ var utils = exports;
12
+ var pki = forge.pki;
13
+
14
+ utils.createCA = function (CN) {
15
+
16
+ var keys = pki.rsa.generateKeyPair(2046);
17
+ var cert = pki.createCertificate();
18
+ cert.publicKey = keys.publicKey;
19
+ cert.serialNumber = new Date().getTime() + '';
20
+ cert.validity.notBefore = new Date();
21
+ cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 5);
22
+ cert.validity.notAfter = new Date();
23
+ cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 20);
24
+ var attrs = [{
25
+ name: 'commonName',
26
+ value: CN
27
+ }, {
28
+ name: 'countryName',
29
+ value: 'CN'
30
+ }, {
31
+ shortName: 'ST',
32
+ value: 'GuangDong'
33
+ }, {
34
+ name: 'localityName',
35
+ value: 'ShenZhen'
36
+ }, {
37
+ name: 'organizationName',
38
+ value: 'node-mitmproxy'
39
+ }, {
40
+ shortName: 'OU',
41
+ value: 'https://github.com/wuchangming/node-mitmproxy'
42
+ }];
43
+ cert.setSubject(attrs);
44
+ cert.setIssuer(attrs);
45
+ cert.setExtensions([{
46
+ name: 'basicConstraints',
47
+ critical: true,
48
+ cA: true
49
+ }, {
50
+ name: 'keyUsage',
51
+ critical: true,
52
+ keyCertSign: true
53
+ }, {
54
+ name: 'subjectKeyIdentifier'
55
+ }]);
56
+
57
+ // self-sign certificate
58
+ cert.sign(keys.privateKey, forge.md.sha256.create());
59
+
60
+ return {
61
+ key: keys.privateKey,
62
+ cert: cert
63
+ };
64
+ };
65
+
66
+ utils.covertNodeCertToForgeCert = function (originCertificate) {
67
+ var obj = forge.asn1.fromDer(originCertificate.raw.toString('binary'));
68
+ return forge.pki.certificateFromAsn1(obj);
69
+ };
70
+
71
+ utils.createFakeCertificateByDomain = function (caKey, caCert, domain) {
72
+ var keys = pki.rsa.generateKeyPair(2046);
73
+ var cert = pki.createCertificate();
74
+ cert.publicKey = keys.publicKey;
75
+
76
+ cert.serialNumber = new Date().getTime() + '';
77
+ cert.validity.notBefore = new Date();
78
+ cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 1);
79
+ cert.validity.notAfter = new Date();
80
+ cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 1);
81
+ var attrs = [{
82
+ name: 'commonName',
83
+ value: domain
84
+ }, {
85
+ name: 'countryName',
86
+ value: 'CN'
87
+ }, {
88
+ shortName: 'ST',
89
+ value: 'GuangDong'
90
+ }, {
91
+ name: 'localityName',
92
+ value: 'ShengZhen'
93
+ }, {
94
+ name: 'organizationName',
95
+ value: 'node-mitmproxy'
96
+ }, {
97
+ shortName: 'OU',
98
+ value: 'https://github.com/wuchangming/node-mitmproxy'
99
+ }];
100
+
101
+ cert.setIssuer(caCert.subject.attributes);
102
+ cert.setSubject(attrs);
103
+
104
+ cert.setExtensions([{
105
+ name: 'basicConstraints',
106
+ critical: true,
107
+ cA: false
108
+ }, {
109
+ name: 'keyUsage',
110
+ critical: true,
111
+ digitalSignature: true,
112
+ contentCommitment: true,
113
+ keyEncipherment: true,
114
+ dataEncipherment: true,
115
+ keyAgreement: true,
116
+ keyCertSign: true,
117
+ cRLSign: true,
118
+ encipherOnly: true,
119
+ decipherOnly: true
120
+ }, {
121
+ name: 'subjectAltName',
122
+ altNames: [{
123
+ type: 2,
124
+ value: domain
125
+ }]
126
+ }, {
127
+ name: 'subjectKeyIdentifier'
128
+ }, {
129
+ name: 'extKeyUsage',
130
+ serverAuth: true,
131
+ clientAuth: true,
132
+ codeSigning: true,
133
+ emailProtection: true,
134
+ timeStamping: true
135
+ }, {
136
+ name: 'authorityKeyIdentifier'
137
+ }]);
138
+ cert.sign(caKey, forge.md.sha256.create());
139
+
140
+ return {
141
+ key: keys.privateKey,
142
+ cert: cert
143
+ };
144
+ };
145
+
146
+ utils.createFakeCertificateByCA = function (caKey, caCert, originCertificate) {
147
+ var certificate = utils.covertNodeCertToForgeCert(originCertificate);
148
+
149
+ var keys = pki.rsa.generateKeyPair(2046);
150
+ var cert = pki.createCertificate();
151
+ cert.publicKey = keys.publicKey;
152
+
153
+ cert.serialNumber = certificate.serialNumber;
154
+ cert.validity.notBefore = new Date();
155
+ cert.validity.notBefore.setFullYear(cert.validity.notBefore.getFullYear() - 1);
156
+ cert.validity.notAfter = new Date();
157
+ cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 1);
158
+
159
+ cert.setSubject(certificate.subject.attributes);
160
+ cert.setIssuer(caCert.subject.attributes);
161
+
162
+ certificate.subjectaltname && (cert.subjectaltname = certificate.subjectaltname);
163
+
164
+ var subjectAltName = _.find(certificate.extensions, { name: 'subjectAltName' });
165
+ cert.setExtensions([{
166
+ name: 'basicConstraints',
167
+ critical: true,
168
+ cA: false
169
+ }, {
170
+ name: 'keyUsage',
171
+ critical: true,
172
+ digitalSignature: true,
173
+ contentCommitment: true,
174
+ keyEncipherment: true,
175
+ dataEncipherment: true,
176
+ keyAgreement: true,
177
+ keyCertSign: true,
178
+ cRLSign: true,
179
+ encipherOnly: true,
180
+ decipherOnly: true
181
+ }, {
182
+ name: 'subjectAltName',
183
+ altNames: subjectAltName.altNames
184
+ }, {
185
+ name: 'subjectKeyIdentifier'
186
+ }, {
187
+ name: 'extKeyUsage',
188
+ serverAuth: true,
189
+ clientAuth: true,
190
+ codeSigning: true,
191
+ emailProtection: true,
192
+ timeStamping: true
193
+ }, {
194
+ name: 'authorityKeyIdentifier'
195
+ }]);
196
+ cert.sign(caKey, forge.md.sha256.create());
197
+
198
+ return {
199
+ key: keys.privateKey,
200
+ cert: cert
201
+ };
202
+ };
203
+
204
+ utils.isBrowserRequest = function () {
205
+ return (/Mozilla/i.test(userAgent)
206
+ );
207
+ };
208
+ //
209
+ // /^[^.]+\.a\.com$/.test('c.a.com')
210
+ //
211
+ utils.isMappingHostName = function (DNSName, hostname) {
212
+ var reg = DNSName.replace(/\./g, '\\.').replace(/\*/g, '[^.]+');
213
+ reg = '^' + reg + '$';
214
+ return new RegExp(reg).test(hostname);
215
+ };
216
+
217
+ utils.getMappingHostNamesFormCert = function (cert) {
218
+ var mappingHostNames = [];
219
+ mappingHostNames.push(cert.subject.getField('CN') ? cert.subject.getField('CN').value : '');
220
+ var altNames = cert.getExtension('subjectAltName') ? cert.getExtension('subjectAltName').altNames : [];
221
+ mappingHostNames = mappingHostNames.concat(_.map(altNames, 'value'));
222
+ return mappingHostNames;
223
+ };
224
+
225
+ // sync
226
+ utils.initCA = function () {
227
+ var basePath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : config.getDefaultCABasePath();
228
+
229
+
230
+ var caCertPath = path.resolve(basePath, config.caCertFileName);
231
+ var caKeyPath = path.resolve(basePath, config.caKeyFileName);
232
+
233
+ try {
234
+ fs.accessSync(caCertPath, fs.F_OK);
235
+ fs.accessSync(caKeyPath, fs.F_OK);
236
+
237
+ // has exist
238
+ return {
239
+ caCertPath: caCertPath,
240
+ caKeyPath: caKeyPath,
241
+ create: false
242
+ };
243
+ } catch (e) {
244
+
245
+ var caObj = utils.createCA(config.caName);
246
+
247
+ var caCert = caObj.cert;
248
+ var cakey = caObj.key;
249
+
250
+ var certPem = pki.certificateToPem(caCert);
251
+ var keyPem = pki.privateKeyToPem(cakey);
252
+
253
+ mkdirp.sync(path.dirname(caCertPath));
254
+ fs.writeFileSync(caCertPath, certPem);
255
+ fs.writeFileSync(caKeyPath, keyPem);
256
+ }
257
+ return {
258
+ caCertPath: caCertPath,
259
+ caKeyPath: caKeyPath,
260
+ create: true
261
+ };
262
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-mitmproxy-pro",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Node.js MITM Proxy Pro",
5
5
  "main": "lib",
6
6
  "bin": "lib/bin/index.js",
@@ -43,7 +43,8 @@
43
43
  "mkdirp": "^0.5.1",
44
44
  "node-forge": "^0.6.39",
45
45
  "through2": "^2.0.1",
46
- "tunnel-agent": "^0.4.3"
46
+ "tunnel-agent": "^0.4.3",
47
+ "uuid": "^13.0.0"
47
48
  },
48
49
  "files": [
49
50
  "lib"