Skip to content

Commit 0dae510

Browse files
authored
Merge pull request #521 from Schamper/master
Add support for serverBindAddress in the REST API
2 parents 1116cd7 + bf6e57c commit 0dae510

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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, boolean useEcc, boolean trustAllServers) {
134+
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
135135
LOG.debug("Instantiate ProxyServer...");
136136
LegacyProxyServer proxy = proxyServerProvider.get();
137137

@@ -177,43 +177,59 @@ public LegacyProxyServer create(Map<String, String> options, Integer port, Strin
177177
proxy.setLocalHost(inetAddress);
178178
}
179179

180+
InetAddress serverInetAddress = null;
181+
if (serverBindAddr != null) {
182+
LOG.debug("Bind ProxyServer serverAddress to `{}`...", serverBindAddr);
183+
try {
184+
serverInetAddress = InetAddress.getByName(serverBindAddr);
185+
} catch (UnknownHostException e) {
186+
LOG.error("Unable to bind proxy to server address: " + serverBindAddr + "; proxy will not be created.", e);
187+
188+
throw new RuntimeException("Unable to bind proxy to server address: ", e);
189+
}
190+
}
191+
180192
if (port != null) {
181-
return startProxy(proxy, port);
193+
return startProxy(proxy, port, serverInetAddress);
182194
}
183195

184196
while (proxies.size() <= maxPort - minPort) {
185197
LOG.debug("Use next available port for new ProxyServer...");
186198
port = nextPort();
187199
try {
188-
return startProxy(proxy, port);
200+
return startProxy(proxy, port, serverInetAddress);
189201
} catch (ProxyExistsException ex) {
190202
LOG.debug("Proxy already exists at port {}", port);
191203
}
192204
}
193205
throw new ProxyPortsExhaustedException();
194206
}
195207

208+
public LegacyProxyServer create(Map<String, String> options, Integer port, String bindAddr, boolean useEcc, boolean trustAllServers) {
209+
return create(options, port, null, null, false, false);
210+
}
211+
196212
public LegacyProxyServer create(Map<String, String> options, Integer port) {
197-
return create(options, port, null, false, false);
213+
return create(options, port, null, null, false, false);
198214
}
199215

200216
public LegacyProxyServer create(Map<String, String> options) {
201-
return create(options, null, null, false, false);
217+
return create(options, null, null, null, false, false);
202218
}
203219

204220
public LegacyProxyServer create() {
205-
return create(null, null, null, false, false);
221+
return create(null, null, null, null, false, false);
206222
}
207223

208224
public LegacyProxyServer create(int port) {
209-
return create(null, port, null, false, false);
225+
return create(null, port, null, null, false, false);
210226
}
211227

212228
public LegacyProxyServer get(int port) {
213229
return proxies.get(port);
214230
}
215231

216-
private LegacyProxyServer startProxy(LegacyProxyServer proxy, int port) {
232+
private LegacyProxyServer startProxy(LegacyProxyServer proxy, int port, InetAddress serverBindAddr) {
217233
if (port != 0) {
218234
proxy.setPort(port);
219235
LegacyProxyServer old = proxies.putIfAbsent(port, proxy);
@@ -224,7 +240,13 @@ private LegacyProxyServer startProxy(LegacyProxyServer proxy, int port) {
224240
}
225241

226242
try {
227-
proxy.start();
243+
if (serverBindAddr != null && proxy instanceof BrowserMobProxyServer) {
244+
BrowserMobProxyServer bProxy = (BrowserMobProxyServer) proxy;
245+
bProxy.start(port, null, serverBindAddr);
246+
} else {
247+
proxy.start();
248+
}
249+
228250
if (port == 0) {
229251
int realPort = proxy.getPort();
230252
proxies.put(realPort, proxy);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public Reply<?> newProxy(Request<String> request) {
9494
}
9595

9696
String paramBindAddr = request.param("bindAddress");
97+
String paramServerBindAddr = request.param("serverBindAddress");
9798
Integer paramPort = request.param("port") == null ? null : Integer.parseInt(request.param("port"));
9899

99100
String useEccString = request.param("useEcc");
@@ -102,11 +103,11 @@ public Reply<?> newProxy(Request<String> request) {
102103
String trustAllServersString = request.param("trustAllServers");
103104
boolean trustAllServers = Boolean.parseBoolean(trustAllServersString);
104105

105-
LOG.debug("POST proxy instance on bindAddress `{}` & port `{}`",
106-
paramBindAddr, paramPort);
106+
LOG.debug("POST proxy instance on bindAddress `{}` & port `{}` & serverBindAddress `{}`",
107+
paramBindAddr, paramPort, paramServerBindAddr);
107108
LegacyProxyServer proxy;
108109
try {
109-
proxy = proxyManager.create(options, paramPort, paramBindAddr, useEcc, trustAllServers);
110+
proxy = proxyManager.create(options, paramPort, paramBindAddr, paramServerBindAddr, useEcc, trustAllServers);
110111
} catch (ProxyExistsException ex) {
111112
return Reply.with(new ProxyDescriptor(ex.getPort())).status(455).as(Json.class);
112113
} catch (ProxyPortsExhaustedException ex) {

0 commit comments

Comments
 (0)