Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit ba4a4d5

Browse files
authored
Merge pull request #1 from crossbrowsertesting/direct-resolution
Direct resolution
2 parents 679d90d + d761d36 commit ba4a4d5

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

browsermob-core/src/main/java/net/lightbody/bmp/BrowserMobProxyServer.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ public class BrowserMobProxyServer implements BrowserMobProxy {
229229
*/
230230
private volatile boolean useEcc = false;
231231

232+
private volatile boolean useDirect = false;
233+
232234
/**
233235
* Resolver to use when resolving hostnames to IP addresses. This is a bridge between {@link org.littleshoot.proxy.HostResolver} and
234236
* {@link net.lightbody.bmp.proxy.dns.AdvancedHostResolver}. It allows the resolvers to be changed on-the-fly without re-bootstrapping the
@@ -304,11 +306,15 @@ public int getMaximumResponseBufferSizeInBytes() {
304306
.withConnectTimeout(connectTimeoutMs)
305307
.withIdleConnectionTimeout(idleConnectionTimeoutSec)
306308
.withProxyAlias(VIA_HEADER_ALIAS);
309+
307310

308311
if (serverBindAddress != null) {
309312
bootstrap.withNetworkInterface(new InetSocketAddress(serverBindAddress, 0));
310313
}
311314

315+
if (useDirect) {
316+
bootstrap.withDirectResolutionEnabled(true);
317+
}
312318

313319
if (!mitmDisabled) {
314320
if (mitmManager == null) {
@@ -444,6 +450,12 @@ public InetAddress getServerBindAddress() {
444450
return serverBindAddress;
445451
}
446452

453+
public void setIsDirectResolutionEnabled(boolean useDirect) {
454+
if (proxyServer instanceof DefaultHttpProxyServer) {
455+
((DefaultHttpProxyServer)proxyServer).setIsDirectResolutionEnabled(useDirect);
456+
}
457+
}
458+
447459
@Override
448460
public Har getHar() {
449461
return har;
@@ -1004,10 +1016,15 @@ public void setTrustAllServers(boolean trustAllServers) {
10041016
throw new IllegalStateException("Cannot disable upstream server verification after the proxy has been started");
10051017
}
10061018

1019+
System.out.println("trustAllServers is " + trustAllServers);
1020+
10071021
if (trustAllServers) {
1022+
System.out.println("trustAllServers was true, setting trustSource to null");
10081023
trustSource = null;
10091024
} else {
1025+
System.out.println("trustAllServers was false, going to check what trustSource is");
10101026
if (trustSource == null) {
1027+
System.out.println("trustSource was null, setting it to defaultTrustSource");
10111028
trustSource = TrustSource.defaultTrustSource();
10121029
}
10131030
}
@@ -1030,6 +1047,10 @@ public void setUseEcc(boolean useEcc) {
10301047
this.useEcc = useEcc;
10311048
}
10321049

1050+
public void setUseDirect(boolean useDirect) {
1051+
this.useDirect = useDirect;
1052+
}
1053+
10331054
/**
10341055
* Adds the basic browsermob-proxy filters, except for the relatively-expensive HAR capture filter.
10351056
*/

browsermob-rest/src/main/java/net/lightbody/bmp/proxy/ProxyManager.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.lightbody.bmp.proxy;
1+
package net.lightbody.bmp.proxy;
22

33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
@@ -131,7 +131,7 @@ public void onRemoval(RemovalNotification<Integer, LegacyProxyServer> removal) {
131131
}
132132
}
133133

134-
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
134+
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers, boolean useDirect) {
135135
LOG.debug("Instantiate ProxyServer...");
136136
LegacyProxyServer proxy = proxyServerProvider.get();
137137

@@ -146,9 +146,20 @@ public LegacyProxyServer create(Map<String, String> options, Integer port, Strin
146146
}
147147

148148
if (trustAllServers) {
149+
System.out.println("trustAllServers is true");
149150
if (proxy instanceof BrowserMobProxyServer) {
151+
System.out.println("Going to trust all servers");
150152
((BrowserMobProxyServer) proxy).setTrustAllServers(true);
151153
}
154+
} else {
155+
System.out.println("Disabling mitm");
156+
((BrowserMobProxyServer) proxy).setMitmDisabled(true);
157+
}
158+
159+
if (useDirect) {
160+
if (proxy instanceof BrowserMobProxyServer) {
161+
((BrowserMobProxyServer) proxy).setUseDirect(true);
162+
}
152163
}
153164

154165
if (options != null) {
@@ -205,24 +216,28 @@ public LegacyProxyServer create(Map<String, String> options, Integer port, Strin
205216
throw new ProxyPortsExhaustedException();
206217
}
207218

219+
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, boolean useEcc, boolean trustAllServers, boolean useDirect) {
220+
return create(options, port, null, null, false, false, useDirect);
221+
}
222+
208223
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, boolean useEcc, boolean trustAllServers) {
209-
return create(options, port, null, null, false, false);
224+
return create(options, port, null, null, false, false, false);
210225
}
211226

212227
public LegacyProxyServer create(Map<String, String> options, Integer port) {
213-
return create(options, port, null, null, false, false);
228+
return create(options, port, null, null, false, false, false);
214229
}
215230

216231
public LegacyProxyServer create(Map<String, String> options) {
217-
return create(options, null, null, null, false, false);
232+
return create(options, null, null, null, false, false, false);
218233
}
219234

220235
public LegacyProxyServer create() {
221-
return create(null, null, null, null, false, false);
236+
return create(null, null, null, null, false, false, false);
222237
}
223238

224239
public LegacyProxyServer create(int port) {
225-
return create(null, port, null, null, false, false);
240+
return create(null, port, null, null, false, false, false);
226241
}
227242

228243
public LegacyProxyServer get(int port) {

browsermob-rest/src/main/java/net/lightbody/bmp/proxy/bricks/ProxyResource.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,19 @@ public Reply<?> newProxy(Request<String> request) {
103103
String trustAllServersString = request.param("trustAllServers");
104104
boolean trustAllServers = Boolean.parseBoolean(trustAllServersString);
105105

106+
String useDirectString = request.param("direct");
107+
boolean useDirect = false;
108+
if (httpProxy != null && useDirectString != null) {
109+
useDirect = Boolean.parseBoolean(useDirectString);
110+
}
111+
112+
System.out.println("Chase -> useDirect is " + useDirect);
113+
106114
LOG.debug("POST proxy instance on bindAddress `{}` & port `{}` & serverBindAddress `{}`",
107115
paramBindAddr, paramPort, paramServerBindAddr);
108116
LegacyProxyServer proxy;
109117
try {
110-
proxy = proxyManager.create(options, paramPort, paramBindAddr, paramServerBindAddr, useEcc, trustAllServers);
118+
proxy = proxyManager.create(options, paramPort, paramBindAddr, paramServerBindAddr, useEcc, trustAllServers, useDirect);
111119
} catch (ProxyExistsException ex) {
112120
return Reply.with(new ProxyDescriptor(ex.getPort())).status(455).as(Json.class);
113121
} catch (ProxyPortsExhaustedException ex) {
@@ -120,6 +128,25 @@ public Reply<?> newProxy(Request<String> request) {
120128
return Reply.with(new ProxyDescriptor(proxy.getPort())).as(Json.class);
121129
}
122130

131+
@Put
132+
@At("/:port/direct")
133+
public Reply<?> setDirect(@Named("port") int port, Request<String> request) {
134+
LegacyProxyServer proxy = proxyManager.get(port);
135+
if (proxy == null) {
136+
return Reply.saying().notFound();
137+
}
138+
139+
String useDirectString = request.param("direct");
140+
boolean useDirect = Boolean.parseBoolean(useDirectString);
141+
System.out.println("Chase -> useDirect is " + useDirect);
142+
143+
if (proxy instanceof BrowserMobProxyServer) {
144+
((BrowserMobProxyServer)proxy).setIsDirectResolutionEnabled(useDirect);
145+
}
146+
147+
return Reply.saying().ok();
148+
}
149+
123150
@Get
124151
@At("/:port/har")
125152
public Reply<?> getHar(@Named("port") int port) {

0 commit comments

Comments
 (0)