Skip to content

Commit

Permalink
Issue jetty#8329 - support wildcards in proxy exclusion hosts
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Gisi <lukasgisi@gmail.com>
  • Loading branch information
sugilite committed Sep 17, 2023
1 parent fd88723 commit 419e718
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The following is a typical configuration:
include::../../{doc_code}/org/eclipse/jetty/docs/programming/client/http/HTTPClientDocs.java[tag=proxy]
----

You specify the proxy host and proxy port, and optionally also the addresses that you do not want to be proxied, and then add the proxy configuration on the `ProxyConfiguration` instance.
You specify the proxy host and proxy port, and optionally also the addresses that you do not want to be proxied (with wildcard '*' supported at the start or end), and then add the proxy configuration on the `ProxyConfiguration` instance.

Configured in this way, `HttpClient` makes requests to the HTTP proxy (for plain-text HTTP requests) or establishes a tunnel via HTTP `CONNECT` (for encrypted HTTPS requests).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ public void proxy() throws Exception

// Do not proxy requests for localhost:8080.
proxy.getExcludedAddresses().add("localhost:8080");
// Do not proxy requests for any address starting with "127.".
proxy.getExcludedAddresses().add("127.*");

// Add the new proxy to the list of proxies already registered.
ProxyConfiguration proxyConfig = httpClient.getProxyConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;

import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.io.ClientConnectionFactory;
Expand Down Expand Up @@ -198,7 +199,7 @@ public boolean matches(Origin origin)
}
for (String excluded : this.excluded)
{
if (matches(address, excluded))
if (matchesWithWildcards(address, excluded))
{
result = false;
break;
Expand All @@ -216,6 +217,36 @@ private boolean matches(Origin.Address address, String pattern)
return host.equals(address.getHost()) && (port <= 0 || port == address.getPort());
}

private boolean matchesWithWildcards(Origin.Address address, String pattern)
{
HostPort hostPort = new HostPort(pattern);
String host = hostPort.getHost();
int port = hostPort.getPort();
String hostRegex = extractHostRegex(host);
return Pattern.matches(hostRegex, address.getHost()) && (port <= 0 || port == address.getPort());
}

private String extractHostRegex(String host)
{
if (host.equals("*"))
{
return ".*";
}
if (host.startsWith("*") && host.endsWith("*"))
{
return ".*" + Pattern.quote(host.substring(1, host.length() - 1)) + ".*";
}
if (host.startsWith("*"))
{
return ".*" + Pattern.quote(host.substring(1));
}
if (host.endsWith("*"))
{
return Pattern.quote(host.substring(0, host.length() - 1)) + ".*";
}
return Pattern.quote(host);
}

/**
* @param connectionFactory the nested {@link ClientConnectionFactory}
* @return a new {@link ClientConnectionFactory} for this Proxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,56 @@ public void testProxyMatchesWithIncludesAndExcludesIPv6() throws Exception
assertTrue(proxy.matches(new Origin("http", "[1::2:3:4]", 0)));
assertFalse(proxy.matches(new Origin("http", "[1::2:3:4]", 5)));
}

@Test
public void testProxyMatchesWithExclusionsWithWildcardAtEnd()
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("1.2.*");

assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 0)));
assertFalse(proxy.matches(new Origin("http", "1.2.3.5", 0)));
assertFalse(proxy.matches(new Origin("http", "1.2.4.4", 0)));
assertFalse(proxy.matches(new Origin("http", "1.2.4.5", 0)));
}

@Test
public void testProxyMatchesWithExclusionsWithWildcardAtStart()
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("*.localhost");

assertFalse(proxy.matches(new Origin("http", "local.localhost", 0)));
assertFalse(proxy.matches(new Origin("http", "local.test.localhost", 0)));
assertTrue(proxy.matches(new Origin("http", "1.2.4.5", 0)));
}

@Test
public void testProxyMatchesWithExclusionsWithWildcardAtStartAndEnd()
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("*.local*");

assertFalse(proxy.matches(new Origin("http", "local.localhost", 0)));
assertFalse(proxy.matches(new Origin("http", "local.test.localhost.test", 0)));
assertTrue(proxy.matches(new Origin("http", "1.2.4.5", 0)));
}

@Test
public void testProxyMatchesWithExclusionsWithWildcardAndPort()
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("1.2.3.*:5");

assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 5)));
}

@Test
public void testProxyMatchesWithExclusionWithWildcardOnly()
{
HttpProxy proxy = new HttpProxy("host", 0);
proxy.getExcludedAddresses().add("*");

assertFalse(proxy.matches(new Origin("http", "1.2.3.4", 0)));
}
}

0 comments on commit 419e718

Please sign in to comment.