Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit 9bff41b

Browse files
authored
Merge pull request #123 from jrgp/proxy_https
Add support for hitting chained proxy via HTTPS
2 parents b840862 + 77f0325 commit 9bff41b

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ Sometimes you will want to route requests through an upstream proxy server. In t
145145
[~]$ curl -X POST http://localhost:8080/proxy?httpProxy=yourproxyserver.com:8080
146146
{"port":8081}
147147

148+
If your upstream proxy server uses https, you can enable connecting using https like this:
149+
150+
[~]$ curl -X POST http://localhost:8080/proxy?httpProxy=yourproxyserver.com:8080&proxyHTTPS=true
151+
{"port":8081}
152+
153+
148154
Alternatively, you can specify the upstream proxy config for all proxies created using the standard JVM [system properties for HTTP proxies](http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html).
149155
Note that you can still override the default upstream proxy via the POST payload, but if you omit the payload the JVM
150156
system properties will be used to specify the upstream proxy.

browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxy.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public interface BrowserUpProxy {
7878
* @throws java.lang.IllegalStateException if the proxy has not been started.
7979
*/
8080
void stop();
81-
81+
8282
/**
8383
* Like {@link #stop()}, shuts down the proxy server and no longer accepts incoming connections, but does not wait for any existing
8484
* network traffic to cease. Any existing connections to clients or to servers may be force-killed immediately.
@@ -564,6 +564,13 @@ public interface BrowserUpProxy {
564564
*/
565565
void setChainedProxy(InetSocketAddress chainedProxyAddress);
566566

567+
/**
568+
* Instructs this proxy to route traffic through an upstream proxy using HTTPS.
569+
*
570+
* @param chainedProxyHTTPS address of the upstream proxy
571+
*/
572+
void setChainedProxyHTTPS(boolean chainedProxyHTTPS);
573+
567574
/**
568575
* Returns the address and port of the upstream proxy.
569576
*

browserup-proxy-core/src/main/java/com/browserup/bup/BrowserUpProxyServer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import com.browserup.bup.proxy.dns.DelegatingHostResolver;
6666
import com.browserup.bup.util.BrowserUpHttpUtil;
6767
import com.browserup.bup.util.BrowserUpProxyUtil;
68+
import org.littleshoot.proxy.SslEngineSource;
69+
import org.littleshoot.proxy.extras.SelfSignedSslEngineSource;
6870
import org.apache.commons.lang3.StringUtils;
6971
import org.littleshoot.proxy.ChainedProxy;
7072
import org.littleshoot.proxy.ChainedProxyAdapter;
@@ -102,6 +104,7 @@
102104
import java.util.concurrent.atomic.AtomicInteger;
103105
import java.util.concurrent.atomic.AtomicReference;
104106
import java.util.regex.Pattern;
107+
import javax.net.ssl.SSLEngine;
105108

106109
import static java.util.stream.Collectors.toCollection;
107110

@@ -243,6 +246,11 @@ public class BrowserUpProxyServer implements BrowserUpProxy {
243246
*/
244247
private volatile InetSocketAddress upstreamProxyAddress;
245248

249+
/**
250+
* Whether to connect to that upstream chained proxy using https, rather than http
251+
*/
252+
private volatile boolean upstreamProxyHTTPS;
253+
246254
/**
247255
* The chained proxy manager that manages upstream proxies.
248256
*/
@@ -376,6 +384,7 @@ public int getMaximumResponseBufferSizeInBytes() {
376384
public void lookupChainedProxies(HttpRequest httpRequest, Queue<ChainedProxy> chainedProxies, ClientDetails clientDetails) {
377385
final InetSocketAddress upstreamProxy = upstreamProxyAddress;
378386
if (upstreamProxy != null) {
387+
final boolean useEncryption = upstreamProxyHTTPS;
379388
chainedProxies.add(new ChainedProxyAdapter() {
380389
@Override
381390
public InetSocketAddress getChainedProxyAddress() {
@@ -393,6 +402,21 @@ public void filterRequest(HttpObject httpObject) {
393402
}
394403
}
395404
}
405+
406+
@Override
407+
public boolean requiresEncryption() {
408+
return useEncryption;
409+
}
410+
411+
@Override
412+
public SSLEngine newSslEngine() {
413+
if (useEncryption) {
414+
return new SelfSignedSslEngineSource(
415+
true, false).newSslEngine();
416+
} else {
417+
return null;
418+
}
419+
}
396420
});
397421
}
398422
}
@@ -929,6 +953,11 @@ public void setChainedProxy(InetSocketAddress chainedProxyAddress) {
929953
upstreamProxyAddress = chainedProxyAddress;
930954
}
931955

956+
@Override
957+
public void setChainedProxyHTTPS(boolean chainedProxyHTTPS) {
958+
upstreamProxyHTTPS = chainedProxyHTTPS;
959+
}
960+
932961
@Override
933962
public InetSocketAddress getChainedProxy() {
934963
return upstreamProxyAddress;

browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/ProxyManager.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void onRemoval(RemovalNotification<Integer, BrowserUpProxyServer> removal
133133
}
134134
}
135135

136-
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
136+
public BrowserUpProxyServer create(String upstreamHttpProxy, boolean upstreamProxyHttps, String proxyUsername, String proxyPassword, Integer port, String bindAddr, String serverBindAddr, boolean useEcc, boolean trustAllServers) {
137137
LOG.debug("Instantiate ProxyServer...");
138138
BrowserUpProxyServer proxy = new BrowserUpProxyServer();
139139

@@ -161,6 +161,8 @@ public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsernam
161161
LOG.error("Invalid upstream http proxy specified: " + upstreamHttpProxy + ". Must use host:port format.");
162162
throw new RuntimeException("Invalid upstream http proxy");
163163
}
164+
165+
proxy.setChainedProxyHTTPS(upstreamProxyHttps);
164166
}
165167

166168
InetAddress clientBindAddress = null;
@@ -204,23 +206,23 @@ public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsernam
204206
}
205207

206208
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port, String bindAddr, boolean useEcc, boolean trustAllServers) {
207-
return create(upstreamHttpProxy, proxyUsername, proxyPassword, port, null, null, false, false);
209+
return create(upstreamHttpProxy, false, proxyUsername, proxyPassword, port, null, null, false, false);
208210
}
209211

210212
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword, Integer port) {
211-
return create(upstreamHttpProxy, proxyUsername, proxyPassword, port, null, null, false, false);
213+
return create(upstreamHttpProxy, false, proxyUsername, proxyPassword, port, null, null, false, false);
212214
}
213215

214216
public BrowserUpProxyServer create(String upstreamHttpProxy, String proxyUsername, String proxyPassword) {
215-
return create(upstreamHttpProxy, proxyUsername, proxyPassword, null, null, null, false, false);
217+
return create(upstreamHttpProxy, false, proxyUsername, proxyPassword, null, null, null, false, false);
216218
}
217219

218220
public BrowserUpProxyServer create() {
219-
return create(null, null, null, null, null, null, false, false);
221+
return create(null, false, null, null, null, null, null, false, false);
220222
}
221223

222224
public BrowserUpProxyServer create(int port) {
223-
return create(null, null, null, port, null, null, false, false);
225+
return create(null, false,null , null, port, null, null, false, false);
224226
}
225227

226228
public BrowserUpProxyServer get(int port) {

browserup-proxy-rest/src/main/java/com/browserup/bup/proxy/bricks/ProxyResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public Reply<?> newProxy(Request request) {
7676
String httpProxy = request.param("httpProxy");
7777
String proxyUsername = request.param("proxyUsername");
7878
String proxyPassword = request.param("proxyPassword");
79+
boolean upstreamProxyHttps = "true".equals(request.param("proxyHTTPS"));
7980

8081
Hashtable<String, String> options = new Hashtable<String, String>();
8182

@@ -101,7 +102,7 @@ public Reply<?> newProxy(Request request) {
101102
paramBindAddr, paramPort, paramServerBindAddr);
102103
BrowserUpProxyServer proxy;
103104
try {
104-
proxy = proxyManager.create(upstreamHttpProxy, proxyUsername, proxyPassword, paramPort, paramBindAddr, paramServerBindAddr, useEcc, trustAllServers);
105+
proxy = proxyManager.create(upstreamHttpProxy, upstreamProxyHttps, proxyUsername, proxyPassword, paramPort, paramBindAddr, paramServerBindAddr, useEcc, trustAllServers);
105106
} catch (ProxyExistsException ex) {
106107
return Reply.with(new ProxyDescriptor(ex.getPort())).status(455).as(Json.class);
107108
} catch (ProxyPortsExhaustedException ex) {

0 commit comments

Comments
 (0)