forked from webmetrics/browsermob-proxy
-
Notifications
You must be signed in to change notification settings - Fork 669
/
Copy pathBrowserMobProxyServerLegacyAdapter.java
527 lines (463 loc) · 18.1 KB
/
BrowserMobProxyServerLegacyAdapter.java
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
package net.lightbody.bmp;
import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.exception.NameResolutionException;
import net.lightbody.bmp.proxy.BlacklistEntry;
import net.lightbody.bmp.proxy.CaptureType;
import net.lightbody.bmp.proxy.LegacyProxyServer;
import net.lightbody.bmp.proxy.auth.AuthType;
import net.lightbody.bmp.proxy.dns.AdvancedHostResolver;
import net.lightbody.bmp.proxy.http.RequestInterceptor;
import net.lightbody.bmp.proxy.http.ResponseInterceptor;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponseInterceptor;
import org.java_bandwidthlimiter.StreamManager;
import org.openqa.selenium.Proxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
/**
* Allows legacy code using the {@link LegacyProxyServer} interface to use the modern littleproxy-based implementation.
*
* @deprecated Use {@link BrowserMobProxyServer}. This adapter class will be removed in a future release.
*/
@Deprecated
public class BrowserMobProxyServerLegacyAdapter extends BrowserMobProxyServer implements LegacyProxyServer {
private static final Logger log = LoggerFactory.getLogger(BrowserMobProxyServerLegacyAdapter.class);
/**
* When true, throw an UnsupportedOperationException instead of logging a warning when an operation is not supported by the LittleProxy-based
* implementation of the BrowserMobProxy interface. Once all operations are implemented and the legacy interface is retired, this will be removed.
*/
private volatile boolean errorOnUnsupportedOperation = false;
/**
* When true will capture Javascript content
*/
private volatile Boolean captureJavascriptContent = true;
/**
* The port to start the proxy on, if set using {@link #setPort(int)}.
*/
private volatile int port;
/**
* The address to listen on, if set using {@link #setLocalHost(InetAddress)}.
*/
private volatile InetAddress clientBindAddress;
/**
* When true, this implementation of BrowserMobProxy will throw an UnsupportedOperationException when a method is not supported. This
* helps identify functionality that is not supported by the LittleProxy-based implementation. By default, this implementation will
* log a warning rather than throw an UnsupportedOperationException.
*
* @param errorOnUnsupportedOperation when true, throws an exception when an operation is not supported
*/
public void setErrorOnUnsupportedOperation(boolean errorOnUnsupportedOperation) {
this.errorOnUnsupportedOperation = errorOnUnsupportedOperation;
}
/**
* @deprecated use {@link net.lightbody.bmp.client.ClientUtil#createSeleniumProxy(BrowserMobProxy)}
*/
@Override
@Deprecated
public Proxy seleniumProxy() throws NameResolutionException {
return ClientUtil.createSeleniumProxy(this);
}
/**
* @deprecated specify the port using {@link #start(int)} or other start() methods with port parameters
*/
@Override
@Deprecated
public void setPort(int port) {
this.port = port;
}
/**
* @deprecated use {@link #getClientBindAddress()}
*/
@Override
@Deprecated
public InetAddress getLocalHost() {
return this.getClientBindAddress();
}
/**
* @deprecated use {@link net.lightbody.bmp.client.ClientUtil#getConnectableAddress()}
*/
@Override
@Deprecated
public InetAddress getConnectableLocalHost() throws UnknownHostException {
return ClientUtil.getConnectableAddress();
}
/**
* @deprecated use {@link #start(int, java.net.InetAddress)} or {@link #start(int, java.net.InetAddress, java.net.InetAddress)}
*/
@Override
@Deprecated
public void setLocalHost(InetAddress localHost) {
this.clientBindAddress = localHost;
}
@Override
public void start() {
super.start(port, clientBindAddress);
}
/**
* Adapter to enable clients to switch to a LittleProxy implementation of BrowserMobProxy but maintain compatibility with
* the 2.0.0 interface.
*/
@Deprecated
private class StreamManagerLegacyAdapter extends StreamManager {
private StreamManagerLegacyAdapter() {
super(0);
}
@Override
public void setDownstreamKbps(long downstreamKbps) {
BrowserMobProxyServerLegacyAdapter.this.setDownstreamKbps(downstreamKbps);
}
@Override
public void setUpstreamKbps(long upstreamKbps) {
BrowserMobProxyServerLegacyAdapter.this.setUpstreamKbps(upstreamKbps);
}
@Override
public void setLatency(long latency) {
BrowserMobProxyServerLegacyAdapter.this.setLatency(latency);
}
@Override
public void setDownstreamMaxKB(long downstreamMaxKB) {
BrowserMobProxyServerLegacyAdapter.this.setWriteLimitKbps(downstreamMaxKB);
}
@Override
public void setUpstreamMaxKB(long upstreamMaxKB) {
BrowserMobProxyServerLegacyAdapter.this.setReadLimitKbps(upstreamMaxKB);
}
@Override
public long getMaxUpstreamKB() {
return BrowserMobProxyServerLegacyAdapter.this.getReadBandwidthLimit() / 1024L;
}
/**
* @deprecated This method is no longer supported and does not return correct values.
*/
@Override
public long getRemainingUpstreamKB() {
return super.getRemainingUpstreamKB();
}
@Override
public long getMaxDownstreamKB() {
return BrowserMobProxyServerLegacyAdapter.this.getWriteBandwidthLimit() / 1024L;
}
/**
* @deprecated This method is no longer supported and does not return correct values.
*/
@Override
public long getRemainingDownstreamKB() {
return super.getRemainingDownstreamKB();
}
}
@Override
public void setRetryCount(int count) {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
} else {
log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
}
}
/**
* StreamManagerLegacyAdapter bound to this BrowserMob Proxy instance, to route the bandwidth control calls to the appropriate
* LittleProxy-compatible methods.
*/
private final StreamManagerLegacyAdapter streamManagerAdapter = new StreamManagerLegacyAdapter();
/**
* This method returns a "fake" StreamManager whose setter methods will wrap LittleProxy-compatible bandwidth control methods. No other methods
* in the returned StreamManager should be used; they will have no effect.
*
* @return fake StreamManager object that wraps LitteProxy-compatible bandwidth control methods
* @deprecated use bandwidth control methods from the {@link BrowserMobProxy}
*/
@Deprecated
public StreamManager getStreamManager() {
return streamManagerAdapter;
}
/**
* @deprecated use {@link #setWriteBandwidthLimit(long)}
*/
@Deprecated
public void setDownstreamKbps(long downstreamKbps) {
this.setWriteBandwidthLimit(downstreamKbps * 1024);
}
/**
* @deprecated use {@link #setReadBandwidthLimit(long)}
*/
@Deprecated
public void setUpstreamKbps(long upstreamKbps) {
this.setReadBandwidthLimit(upstreamKbps * 1024);
}
/**
* @deprecated use {@link #setLatency(long, java.util.concurrent.TimeUnit)}
*/
@Deprecated
public void setLatency(long latencyMs) {
setLatency(latencyMs, TimeUnit.MILLISECONDS);
}
/**
* @deprecated use {@link #setReadBandwidthLimit(long)}
*/
@Deprecated
public void setReadLimitKbps(long readLimitKbps) {
setReadBandwidthLimit(readLimitKbps * 1024);
}
/**
* @deprecated use {@link #setWriteBandwidthLimit(long)}
*/
@Deprecated
public void setWriteLimitKbps(long writeLimitKbps) {
setWriteBandwidthLimit(writeLimitKbps * 1024);
}
/**
* @deprecated
*/
@Override
@Deprecated
public void addRequestInterceptor(HttpRequestInterceptor i) {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
} else {
log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
}
}
/**
* @deprecated
*/
@Override
@Deprecated
public void addRequestInterceptor(RequestInterceptor interceptor) {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
} else {
log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
}
}
/**
* @deprecated
*/
@Override
@Deprecated
public void addResponseInterceptor(HttpResponseInterceptor i) {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
} else {
log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
}
}
/**
* @deprecated
*/
@Override
@Deprecated
public void addResponseInterceptor(ResponseInterceptor interceptor) {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
} else {
log.warn("No LittleProxy implementation for operation: " + new Throwable().getStackTrace()[0].getMethodName());
}
}
/**
* @deprecated use getBlacklist()
*/
@Deprecated
public List<BlacklistEntry> getBlacklistedRequests() {
return ImmutableList.copyOf(getBlacklist());
}
/**
* @deprecated use {@link #getBlacklist()}
*/
@Override
@Deprecated
public Collection<BlacklistEntry> getBlacklistedUrls() {
return getBlacklist();
}
/**
* @deprecated use {@link #getWhitelistStatusCode()}
*/
@Override
@Deprecated
public int getWhitelistResponseCode() {
return getWhitelistStatusCode();
}
/**
* @deprecated use {@link #disableWhitelist()}
*/
@Override
@Deprecated
public void clearWhitelist() {
this.disableWhitelist();
}
/**
* @deprecated use {@link #getWhitelistUrls()}
*/
@Deprecated
public List<Pattern> getWhitelistRequests() {
ImmutableList.Builder<Pattern> builder = ImmutableList.builder();
for (String patternString : getWhitelistUrls()) {
Pattern pattern = Pattern.compile(patternString);
builder.add(pattern);
}
return builder.build();
}
/**
* @deprecated use {@link #waitForQuiescence(long, long, java.util.concurrent.TimeUnit)}
*/
@Override
@Deprecated
public void waitForNetworkTrafficToStop(long quietPeriodInMs, long timeoutInMs) {
waitForQuiescence(quietPeriodInMs, timeoutInMs, TimeUnit.MILLISECONDS);
}
/**
* @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public boolean isCaptureHeaders() {
return getHarCaptureTypes().containsAll(CaptureType.getHeaderCaptureTypes());
}
/**
* @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public void setCaptureHeaders(boolean captureHeaders) {
if (captureHeaders) {
enableHarCaptureTypes(CaptureType.getHeaderCaptureTypes());
} else {
disableHarCaptureTypes(CaptureType.getHeaderCaptureTypes());
}
}
/**
* @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public boolean isCaptureContent() {
return getHarCaptureTypes().containsAll(CaptureType.getNonBinaryContentCaptureTypes());
}
/**
* @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public void setCaptureContent(boolean captureContent) {
if (captureContent) {
enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
} else {
disableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
}
}
@Deprecated
public void setCaptureJavascriptContent(Boolean captureJavascriptContent) {
this.captureJavascriptContent = captureJavascriptContent;
}
/**
* @deprecated use {@link #getHarCaptureTypes()} to check for relevant {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public boolean isCaptureBinaryContent() {
return getHarCaptureTypes().containsAll(CaptureType.getBinaryContentCaptureTypes());
}
/**
* @deprecated use {@link #setHarCaptureTypes(java.util.Set)} to set the appropriate {@link net.lightbody.bmp.proxy.CaptureType}
*/
@Deprecated
public void setCaptureBinaryContent(boolean captureBinaryContent) {
if (captureBinaryContent) {
enableHarCaptureTypes(CaptureType.getBinaryContentCaptureTypes());
} else {
disableHarCaptureTypes(CaptureType.getBinaryContentCaptureTypes());
}
}
/**
* @deprecated this method has no effect and will be removed from a future version
*/
@Deprecated
public void cleanup() {
}
/**
* @deprecated Remap hosts directly using {@link net.lightbody.bmp.proxy.dns.AdvancedHostResolver#remapHost(String, String)}.
* See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}.
*/
@Override
@Deprecated
public void remapHost(String source, String target) {
getHostNameResolver().remapHost(source, target);
}
/**
* @deprecated Manipulate the DNS cache directly using {@link net.lightbody.bmp.proxy.dns.AdvancedHostResolver#clearDNSCache()}.
* See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}.
*/
@Override
@Deprecated
public void clearDNSCache() {
getHostNameResolver().clearDNSCache();
}
/**
* @deprecated Manipulate the DNS cache directly using {@link net.lightbody.bmp.proxy.dns.AdvancedHostResolver#setNegativeDNSCacheTimeout(int, java.util.concurrent.TimeUnit)}
* and {@link net.lightbody.bmp.proxy.dns.AdvancedHostResolver#setPositiveDNSCacheTimeout(int, java.util.concurrent.TimeUnit)}.
* See {@link #getHostNameResolver()} and the default implementation in {@link net.lightbody.bmp.proxy.dns.ChainedHostResolver}.
*/
@Override
@Deprecated
public void setDNSCacheTimeout(int timeout) {
AdvancedHostResolver resolver = getHostNameResolver();
resolver.setPositiveDNSCacheTimeout(timeout, TimeUnit.SECONDS);
resolver.setNegativeDNSCacheTimeout(timeout, TimeUnit.SECONDS);
}
/**
* @deprecated use {@link #setConnectTimeout(int, java.util.concurrent.TimeUnit)}
*/
@Deprecated
@Override
public void setConnectionTimeout(int connectionTimeoutMs) {
setConnectTimeout(connectionTimeoutMs, TimeUnit.MILLISECONDS);
}
/**
* @deprecated use {@link #setIdleConnectionTimeout(int, java.util.concurrent.TimeUnit)}
*/
@Deprecated
@Override
public void setSocketOperationTimeout(int readTimeoutMs) {
setIdleConnectionTimeout(readTimeoutMs, TimeUnit.MILLISECONDS);
}
/**
* @deprecated use {@link #setRequestTimeout(int, java.util.concurrent.TimeUnit)}
*/
@Deprecated
@Override
public void setRequestTimeout(int requestTimeoutMs) {
setRequestTimeout(requestTimeoutMs, TimeUnit.MILLISECONDS);
}
/**
* @deprecated use {@link #autoAuthorization(String, String, String, net.lightbody.bmp.proxy.auth.AuthType)}
*/
@Deprecated
@Override
public void autoBasicAuthorization(String domain, String username, String password) {
autoAuthorization(domain, username, password, AuthType.BASIC);
}
/**
* @deprecated use {@link #setChainedProxy(java.net.InetSocketAddress)} to set an upstream proxy
*/
@Deprecated
public void setOptions(Map<String, String> options) {
if (options == null || options.isEmpty()) {
return;
}
String httpProxy = options.get("httpProxy");
if (httpProxy != null) {
log.warn("Chained proxy support through setOptions is deprecated. Use setUpstreamProxy() to enable chained proxy support.");
HostAndPort hostAndPort = HostAndPort.fromString(httpProxy);
this.setChainedProxy(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPortOrDefault(80)));
} else {
if (errorOnUnsupportedOperation) {
throw new UnsupportedOperationException("The LittleProxy-based implementation of BrowserMob Proxy does not support the setOptions method. Use the methods defined in the BrowserMobProxy interface to set connection parameters.");
} else {
log.warn("The LittleProxy-based implementation of BrowserMob Proxy does not support the setOptions method. Use the methods defined in the BrowserMobProxy interface to set connection parameters.");
}
}
}
}