Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow proxying requests through an outgoing HTTP proxy. #251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@
<version>11.0.15</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.littleshoot</groupId>
<artifactId>littleproxy</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/mitre/dsmiley/httpproxy/ProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
import org.apache.http.client.methods.AbortableHttpRequest;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.message.BasicHttpRequest;
Expand Down Expand Up @@ -111,6 +113,11 @@ public class ProxyServlet extends HttpServlet {
/** The parameter name for the target (destination) URI to proxy to. */
public static final String P_TARGET_URI = "targetUri";

/**
* Parameter for an (optional) HTTP proxy that should be used for any request to the target. Format: <code>host:port</code>.
*/
public static final String P_HTTP_PROXY_HOST_AND_PORT = "http.proxy.host-and-port";

protected static final String ATTR_TARGET_URI =
ProxyServlet.class.getSimpleName() + ".targetUri";
protected static final String ATTR_TARGET_HOST =
Expand Down Expand Up @@ -139,6 +146,7 @@ public class ProxyServlet extends HttpServlet {
protected String targetUri;
protected URI targetUriObj;//new URI(targetUri)
protected HttpHost targetHost;//URIUtils.extractHost(targetUriObj);
protected HttpHost httpProxyHost;

private HttpClient proxyClient;

Expand Down Expand Up @@ -226,6 +234,15 @@ public void init() throws ServletException {
this.doHandleCompression = Boolean.parseBoolean(doHandleCompression);
}

String proxyHostAndPortString = this.getConfigParam(P_HTTP_PROXY_HOST_AND_PORT);
if (proxyHostAndPortString != null) {
String[] proxyParams = proxyHostAndPortString.split(":");
if (proxyParams.length != 2) {
throw new ServletException("Invalid config parameter: " + P_HTTP_PROXY_HOST_AND_PORT + " - format must be: HOST:PORT");
}
this.httpProxyHost = new HttpHost(proxyParams[0], Integer.parseInt(proxyParams[1]));
}

initTarget();//sets target*

proxyClient = createHttpClient();
Expand Down Expand Up @@ -289,6 +306,12 @@ protected HttpClient createHttpClient() {

if (useSystemProperties)
clientBuilder = clientBuilder.useSystemProperties();

if (httpProxyHost != null) {
HttpRoutePlanner proxyRoutePlanner = new DefaultProxyRoutePlanner(httpProxyHost);
clientBuilder.setRoutePlanner(proxyRoutePlanner);
}

return buildHttpClient(clientBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.mitre.dsmiley.httpproxy;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.littleshoot.proxy.HttpProxyServer;
import org.littleshoot.proxy.impl.DefaultHttpProxyServer;

import java.util.Properties;

public class ProxyServletThroughHttpProxyTest extends ProxyServletTest {

private static HttpProxyServer proxyServer;

@BeforeClass
public static void setUpBeforeClass() {
proxyServer = DefaultHttpProxyServer.bootstrap().start();
}

@AfterClass
public static void tearDownAfterClass() {
proxyServer.stop();
}

@Override
protected void setUpServlet(Properties servletProps) {
servletProps.setProperty(ProxyServlet.P_HTTP_PROXY_HOST_AND_PORT, "localhost:" + proxyServer.getListenAddress().getPort());
super.setUpServlet(servletProps);
}

}