-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
655 lines (547 loc) · 23.9 KB
/
index.js
File metadata and controls
655 lines (547 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
'use strict';
const request = require('request');
const co = require('co');
const typed = require('typedproxy');
const net = require('net');
const typesBrowserMobProxyClient = {
'ip4' : (value) => {
const typeOfValue = {}.toString.call(value).slice(8, -1);
if (typeOfValue !== 'String') {
throw new TypeError(`ip4 parameter must be String instance, not ${typeOfValue}`);
}
if (!net.isIPv4(value)) {
throw new TypeError(`ip4 parameter must be String instance, not ${typeOfValue}`);
}
},
'port' : (value) => {
const typeOfValue = {}.toString.call(value).slice(8, -1);
if (typeOfValue !== 'Number') {
throw new TypeError(`port parameter must be Number instance, not ${typeOfValue}`);
}
if (!Number.isSafeInteger(value)) {
throw new TypeError(`port parameter must be safe integer value`);
}
if (value < 0 || value > 65535 ) {
throw new TypeError(`port parameter must be more than 0, and less than 65535, not ${value}`);
}
}
};
const clients = Symbol();
const closeProxyMethod = Symbol();
const bmpRequest = Symbol();
/**
* Class which is used for service set of browserMob Proxy instances.
* @type {browserMobProxyClient}
*/
const browserMobProxyClient = class browserMobProxyClient {
/**
* Creates {@link browserMobProxyClient} new instance.
* @param {string} ip4Host - ip address of host, where BrowserMob Proxy has started.
* @param {number} portPort - tcp port, where BrowserMob Proxy has started.
*/
constructor(ip4Host, portPort){
this.host = ip4Host;
this.port = portPort;
this.url = `http://${ip4Host}:${portPort}`;
this[clients] = [];
};
/**
* Object that represent body of server answer.
* @typedef {object} promiseRequestBody
*/
/**
* Promise version of {@link https://github.com/request/request|request}. It's used only for internal purpose,
* access to it is restricted by using private Symbol() variable.
* @private
* @param {string} urlApi - url for REST API method.
* @param {object} [anyParam] - any parameters of request, such as method, json, etc.
* @returns {Promise<promiseRequestBody, Error>} JSON parsed object of response body if fulfill, Error otherwise
*/
static [bmpRequest](urlApi, anyParam) {
anyParam = anyParam || {method : 'GET'};
return new Promise((resolve, reject) => {
request(urlApi, anyParam, (error, response, body) => {
console.log(`request : ${urlApi}; method : ${anyParam.method}`);
if(error) {
console.log('Error (FAIL) : ', error);
return reject(error);
} else {
try {
console.log('parsed body : ', JSON.parse(body));
return resolve(JSON.parse(body));
}catch(bodyError){
try{
if(response.statusCode == 200 || response.statusCode == 204){
console.log('parsed status (OK) : ', response.statusCode);
if(body) {
return resolve(JSON.parse(body));
} else {
return resolve();
}
} else {
console.log('parsed status (FAIL) : ', response.statusCode);
return reject(response);
}
} catch(responseError) {
console.log('responseError (FAIL) : ', response.statusCode);
return reject(responseError);
}
}
};
});
});
};
/**
* Object that represent proxy info
* @typedef {object} proxyInfo
* @property port {number} tcp port, where proxy was started
*/
/**
* Receives list of all proxies, which were started.
* @returns {Promise<proxyInfo[]>}
*/
getProxiesList() {
const apiUrl = `${this.url}/proxy`;
return co(function* (){
let list = yield browserMobProxyClient[bmpRequest](apiUrl);
return list.proxyList;
});
};
/** Closes a proxy
* @private
* @param {number} portProxy - tcp port, where proxy was started
* @returns {Promise<promiseRequestBody, Error>}
*/
[closeProxyMethod](portProxy) {
const apiUrl = `${this.url}/proxy/${portProxy}`;
const options = {method : 'DELETE'};
return browserMobProxyClient[bmpRequest](apiUrl, options);
};
/**
* Creates new instance of {@link browserMobProxyClientApi}
* @returns {Promise<browserMobProxyClientApi, Error>}
*/
create() {
const self = this;
return co(function* (){
const client = new browserMobProxyClientApi(self.url);
self[clients].push(client);
return client;
});
};
/**
* Returns own proxy list. Returned proxies belong only to current instance of {@link browserMobProxyClient}
* @returns {Promise< proxyInfo[] | Error>}
*/
getOwnProxiesList(){
const self = this;
return co(function* (){
let proxyList = [];
for(let resolvedPromiseClient of self[clients]){
const client = yield resolvedPromiseClient;
proxyList.push(client);
};
return proxyList;
});
};
/**
* Closes all proxies belong to current instance of {@link browserMobProxyClient}
* @returns {Promise | Error}
*/
closeAllOwnProxies() {
const self = this;
return co(function* (){
const proxyList = yield self.getOwnProxiesList();
for(let proxy of proxyList){
yield self[closeProxyMethod](proxy.port);
self[clients].shift();
}
});
};
};
/** Class - client for interaction with BrowserMob Proxy REST API Server. */
class browserMobProxyClientApi {
/**
* Create {@link browserMobProxyClientApi} instance
* @param {string} urlServerAPI - url to BrowserMob Proxy was started before.
* @param {string} [clientHost] - host of upstream proxy server. If you want work trough upstream proxy server.
* @param {number} [clientPort] - tcp port of upstream proxy server.
* @param {boolean} [trustAllServers=true] - ignore or not certificate errors
* @returns {Promise< browserMobProxyClientApi | Error>}
*/
constructor(urlServerAPI, clientHost, clientPort, trustAllServers = true) {
this.serverUrl = urlServerAPI;
//api path and options
let apiUrl = `${this.serverUrl}/proxy?trustAllServers=${trustAllServers}`;
const options = { method : 'POST' };
//connect to external proxy, if needed
if(clientHost && clientPort){
apiUrl = `${this.serverUrl}/proxy?httpProxy=${clientHost}:${clientPort}&trustAllServers=${trustAllServers}`;
}
//create new proxy
const self = this;
return co(function* (){
const port = yield browserMobProxyClient[bmpRequest](apiUrl, options);
self.port = port.port;
self.apiUrl = `${self.serverUrl}/proxy/${self.port}`;
return self;
});
};
/**
* Object that represent {@link http://www.softwareishard.com/blog/har-12-spec|HAR}
* @typedef {object} promiseRequestBody
*/
/**
* Creates a new HAR attached to the proxy and returns the HAR content if there was a previous HAR
* @param {boolean} [boolCaptureHeaders=true] - capture headers or not
* @param {boolean} [boolCaptureBody=false] - capture content bodies or not
* @param {boolean} [boolCaptureAllContent=false] - capture binary content or not.
* @param {string} [pageRef='Page 1'] - the string name of the first page ref that should be used in the HAR
* @param {string} [pageTitle='Page 1'] - the title of first HAR page
* @returns {Promise< harObject | Error>}
*/
newHar(boolCaptureHeaders = true, boolCaptureBody = false, boolCaptureAllContent = false, pageRef, pageTitle) {
const form = {captureHeaders : boolCaptureHeaders, captureContent : boolCaptureBody, captureBinaryContent : boolCaptureAllContent};
if(pageRef) {
form.initialPageRef = pageRef;
}
if(pageTitle) {
form.initialPageTitle = pageTitle;
}
let apiUrl = `${this.apiUrl}/har`;
let options = { method : 'PUT', form : form};
return co(function* (){
let result = yield browserMobProxyClient[bmpRequest](apiUrl, options);
return result;
});
};
//new version
/**
* Starts a new page on the existing HAR
* @param {object} [newPageTitleObject] -
* @param {string} [pageRef='Page N'] - The string name of the first page ref that should be used in the HAR.
* @param {string} [pageTitle='Page N'] - The title of new HAR page
* @returns {Promise<undefined | Error>}
*/
startPage({pageRef, pageTitle} = {}) {
const form = {};
if(pageRef) {
form.pageRef = pageRef;
}
if(pageTitle) {
form.pageTitle = pageTitle;
}
let apiUrl = `${this.apiUrl}/har/pageRef`;
let options = { method : 'PUT', form : form};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl,options);
});
};
/**
* Shuts down the proxy and closes the port.
* @returns {Promise<undefined | Error>}
*/
close() {
const apiUrl = `${this.apiUrl}`;
const options = {method : 'DELETE'};
return co(function* (){
return browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Returns the JSON/HAR content representing all the HTTP traffic passed through the proxy
* (provided you have already created the HAR with this {@link browserMobProxyClientApi#newHar|method})
* @returns {Promise< harObject | Error>}
*/
getHar() {
const apiUrl = `${this.apiUrl}/har`;
let options = { method : 'GET' };
return co(function* (){
let result = yield browserMobProxyClient[bmpRequest](apiUrl, options);
return result;
});
};
/**
* Displays whitelisted items
* @returns {Promise<string[] | Error>} - Array of urls which have set before
*/
getWhiteList() {
const apiUrl = `${this.apiUrl}/whitelist`;
const options = { method : 'GET' };
return co(function* (){
let result = yield browserMobProxyClient[bmpRequest](apiUrl, options);
return result;
});
};
/**
* Sets a list of URL patterns to whitelist
* @param {string} httpCodeStatus - the HTTP status code to return for URLs that do not match the whitelist.
* @param {string} regexps - a comma separated list of regular expressions.
* @returns {Promise<undefined | Error>}
*/
setWhiteList(httpCodeStatus, regexps) {
const apiUrl = `${this.apiUrl}/whitelist`;
const options = { method : 'PUT', form : {regex : regexps.join(','), status : httpCodeStatus}};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Clears all URL patterns from the whitelist
* @returns {Promise<undefined | Error>}
*/
clearWhiteList() {
const apiUrl = `${this.apiUrl}/whitelist`;
let options = { method : 'DELETE'};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Displays blacklisted items
* @returns {Promise<BlackListedUrl[] | Error>}
*/
getBlackList() {
const apiUrl = `${this.apiUrl}/blacklist`;
const options = { method : 'GET' };
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
//new version
/**
* Object that represent black list item
* @typedef {object} BlackListedUrl
* @property urlPattern {string} incoming regexp for blocking
* @property statusCode {number} incoming http code is returned for blocked url
* @property httpMethodPattern {string} incoming regular expression for matching HTTP method (GET, POST, PUT, etc). If null processing all HTTP method.
* @property method {string} regular expression for matching HTTP method (GET, POST, PUT, etc). If null processing all HTTP method.
* @property responseCode {number} http code is returned for blocked url
* @property pattern {string} incoming regexp for blocking
*/
/**
* Setup url to black list
* @param {number} httpCodeStatus - The HTTP status code to return for URLs that are blacklisted
* @param {string} regexp - The blacklist regular expression
* @param {string} [methodsRegexp] - The regular expression for matching HTTP method (GET, POST, PUT, etc). Optional, by default processing all HTTP method
* @returns {Promise<BlackListedUrl[]>}
*/
setBlackList(httpCodeStatus, regexp, methodsRegexp) {
let form = {regex : regexp, status : httpCodeStatus};
if(methodsRegexp){
form.method = methodsRegexp;
}
const apiUrl = `${this.apiUrl}/blacklist`;
const options = { method : 'PUT', form : form};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
//new version
/**
* Clears all URL patterns from the blacklist
* @returns {Promise<undefined | Error>}
*/
clearBlackList() {
const apiUrl = `${this.apiUrl}/blacklist`;
let options = { method : 'DELETE'};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
//new version
/**
* Object for setting up limits of BrowserMob Proxy
* @typedef {object} LimitsSetterObject
* @property {number} [downstreamKbps] - Downstream bandwidth limit in kbps
* @property {number} [downstreamBps] - Downstream bandwidth limit in bit per second
* @property {number} [upstreamKbps] - Upstream bandwidth limit in kbps
* @property {number} [upstreamBps] - Upstream bandwidth limit in bit per second
* @property {number} [downstreamMaxKB] - Specifies how many kilobytes in total the client is allowed to download through the proxy
* @property {number} [upstreamMaxKB] - Specifies how many kilobytes in total the client is allowed to upload through the proxy
* @property {number} [latency=0] - Add the given latency to each HTTP request. By default all requests are invoked without latency
* @property {boolean} [enable=false] - A boolean that enable bandwidth limiter. Setting any of the properties above will implicitly enable throttling
* @property {number} [payloadPercentage] - Specifying what percentage of data sent is payload, e.g. use this to take into account overhead due to tcp/ip
* @property {number} [maxBitsPerSecond] - The max bits per seconds you want this instance of StreamManager to respect
*/
/**
* Sets the downstream bandwidth limit in kbps
* @param {LimitsSetterObject} [browserMobProxyLimitObject]
* @returns {Promise}
*/
setLimits(limitsSetterObject) {
const apiUrl = `${this.apiUrl}/limit`;
const options = { method : 'PUT', form : limitsSetterObject};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Object describes amount of data remaining to be uploaded/downloaded until the limit is reached
* @typedef {object} LimitsGetterObject
* @property {number} maxUpstreamKB - Show maxUpstreamKB set by {@link browserMobProxyClientApi#setLimits}
* @property {number} maxDownstreamKB - Show maxDownstreamKB set by {@link browserMobProxyClientApi#setLimits}
* @property {number} remainingUpstreamKB - Show how many kilobytes will be uploaded before the limit is reached
* @property {number} remainingDownstreamKB - Show how many kilobytes will be downloaded before the limit is reached
*/
/**
* Displays the amount of data remaining to be uploaded/downloaded until the limit is reached
* @returns {Promise<LimitsGetterObject>}
*/
getLimits() {
const apiUrl = `${this.apiUrl}/limit`;
const options = { method : 'GET' };
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Set and override HTTP Request headers
* @param {object} headers - Represents set of headers, where key is a header name and value is a value of HTTP header
* @returns {Promise}
*/
setHeaders(headers) {
const apiUrl = `${this.apiUrl}/headers`;
const options = { method : 'POST', json : true, body : headers};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Overrides normal DNS lookups and remaps the given hosts with the associated IP address
* @param {object} dns - Represents set of of hosts, where key is a host name and value is a IP address which associated with host name
* @returns {Promise}
*/
overrideDNS (dns) {
const apiUrl = `${this.apiUrl}/hosts`;
const options = { method : 'POST', json : true, body : dns};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Sets automatic basic authentication for the specified domain. This method supports only BASIC authentication.
* @param {object} auth - Object describes authentication data
* @param {string} auth.username - Login
* @param {string} auth.password - Password
* @param {string} domain - At the domain will be applying basic auth
* @returns {Promise}
*/
setAutoAuthentication (auth, domain) {
const apiUrl = `${this.apiUrl}/auth/basic/${domain}`;
const options = { method : 'POST', json : true, body : auth};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
}
/**
* Wait till all request are being made
* @param {object} waitObject - Object describes waits data
* @param {number} waitObject.quietPeriodInMs - amount of time after which network traffic will be considered "stopped"
* @param {number} waitObject.timeoutInMs - maximum amount of time to wait for network traffic to stop
* @returns {Promise}
*/
setWait(waitObject){
for(let waitProperty in waitObject) {
waitObject[waitProperty] = waitObject[waitProperty].toString();
}
const apiUrl = `${this.apiUrl}/wait`;
const options = { method : 'PUT', form : waitObject};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
}
/**
* Handles different proxy timeouts. The new LittleProxy implementation requires that all timeouts be set before start Proxy, because of it tests skipped.
* @param {object} timeoutObj - Describes timeout object
* @param {number} timeoutObj.requestTimeout - Request timeout in milliseconds. timeout value of -1 is interpreted as infinite timeout.
* @param {number} timeoutObj.readTimeout - Read timeout in milliseconds. Which is the timeout for waiting for data or, put differently, a maximum period inactivity between two consecutive data packets. A timeout value of zero is interpreted as an infinite timeout.
* @param {number} timeoutObj.connectionTimeout - Determines the timeout in milliseconds until a connection is established. A timeout value of zero is interpreted as an infinite timeout.
* @param {number} timeoutObj.dnsCacheTimeout - Sets the maximum length of time that records will be stored in this Cache. A nonpositive value disables this feature
* @returns {Promise}
*/
setTimeouts(timeoutObj){
for(let timeoutProperty in timeoutObj) {
timeoutObj[timeoutProperty] = timeoutObj[timeoutProperty].toString();
}
const apiUrl = `${this.apiUrl}/timeout`;
const options = { method : 'PUT', json : true, body : timeoutObj};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
}
/**
* Redirecting URL's
* @param redirectObj - Describes redirect object
* @param {string} redirectObj.matchRegex - a matching URL regular expression
* @param {string} redirectObj.replace - replacement URL
* @returns {Promise}
*/
setRedirectUrls(redirectObj){
const apiUrl = `${this.apiUrl}/rewrite`;
const options = { method : 'PUT', form : redirectObj};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Removes all URL redirection rules currently in effect
* @returns {Promise}
*/
removeRedirects(){
const apiUrl = `${this.apiUrl}/rewrite`;
const options = { method : 'DELETE'};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Setting the retry count
* @param {number} numberOfTries - The number of times a method will be retried
* @returns {Promise}
*/
setRetries(numberOfTries){
const apiUrl = `${this.apiUrl}/retry`;
const options = { method : 'PUT', form : {retrycount : numberOfTries}};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Empties the DNS cache
* @returns {Promise}
*/
clearDNSCache(){
const apiUrl = `${this.apiUrl}/dns/cache`;
const options = { method : 'DELETE'};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
};
/**
* Describe your own request interception
* @param {string} rule - a string which determines interceptor rules.
* @returns {Promise}
*/
setRequestInterception (rule) {
const apiUrl = `${this.apiUrl}/filter/request`;
const options = { method : 'POST', body : rule};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
}
/**
* Describe your own response interception
* @param {string} rule - a string which determines interceptor rules.
* @returns {Promise}
*/
setResponseInterception (rule) {
const apiUrl = `${this.apiUrl}/filter/response`;
const options = { method : 'POST', body : rule};
return co(function* (){
return yield browserMobProxyClient[bmpRequest](apiUrl, options);
});
}
};
const typedBrowserMobProxyClient = new typed(browserMobProxyClient, typesBrowserMobProxyClient);
module.exports = typedBrowserMobProxyClient;