mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-02-25 07:50:52 +01:00
Fix proxy exclusions with glob patterns
This commit is contained in:
committed by
Thomas Zerr
parent
676628ce1e
commit
78ef9a50e8
2
gradle/changelog/proxy_exclusions.yaml
Normal file
2
gradle/changelog/proxy_exclusions.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
- type: fixed
|
||||
description: Proxy exclusions with glob patterns
|
||||
@@ -124,15 +124,11 @@ public final class HttpURLConnectionFactory {
|
||||
private boolean isProxyEnabled(ProxyConfiguration proxyConfiguration, URL url) {
|
||||
return !options.isIgnoreProxySettings()
|
||||
&& proxyConfiguration.isEnabled()
|
||||
&& !isHostExcluded(proxyConfiguration, url);
|
||||
&& isHostIncluded(proxyConfiguration, url);
|
||||
}
|
||||
|
||||
private boolean isHostExcluded(ProxyConfiguration proxyConfiguration, URL url) {
|
||||
Collection<String> excludes = proxyConfiguration.getExcludes();
|
||||
if (excludes == null) {
|
||||
return false;
|
||||
}
|
||||
return excludes.contains(url.getHost());
|
||||
private boolean isHostIncluded(ProxyConfiguration proxyConfiguration, URL url) {
|
||||
return Proxies.isEnabledForHost(proxyConfiguration.getExcludes(), url.getHost());
|
||||
}
|
||||
|
||||
private HttpURLConnection openProxyConnection(ProxyConfiguration configuration, URL url) throws IOException {
|
||||
|
||||
@@ -24,6 +24,8 @@ import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.util.GlobUtil;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Util class for proxy settings.
|
||||
@@ -48,54 +50,44 @@ public final class Proxies
|
||||
*
|
||||
* @return true if proxy settings should be used
|
||||
*/
|
||||
public static boolean isEnabled(ScmConfiguration configuration, String url)
|
||||
{
|
||||
boolean result = false;
|
||||
|
||||
if (configuration.isEnableProxy())
|
||||
{
|
||||
result = true;
|
||||
|
||||
public static boolean isEnabled(ScmConfiguration configuration, String url) {
|
||||
if (configuration.isEnableProxy()) {
|
||||
int index = url.indexOf("://");
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
if (index > 0) {
|
||||
url = url.substring(index + 3);
|
||||
}
|
||||
|
||||
index = url.indexOf('/');
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
if (index > 0) {
|
||||
url = url.substring(0, index);
|
||||
}
|
||||
|
||||
index = url.indexOf(':');
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
if (index > 0) {
|
||||
url = url.substring(0, index);
|
||||
}
|
||||
|
||||
for (String exclude : configuration.getProxyExcludes())
|
||||
{
|
||||
if (GlobUtil.matches(exclude, url))
|
||||
{
|
||||
logger.debug(
|
||||
"disable proxy settings for url {}, because exclude {} matches",
|
||||
url, exclude);
|
||||
result = false;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Set<String> proxyExcludes = configuration.getProxyExcludes();
|
||||
return isEnabledForHost(proxyExcludes, url);
|
||||
} else {
|
||||
logger.trace("proxy settings are disabled");
|
||||
}
|
||||
|
||||
return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isEnabledForHost(Collection<String> proxyExcludes, String host) {
|
||||
for (String exclude : proxyExcludes) {
|
||||
if (GlobUtil.matches(exclude, host)) {
|
||||
logger.debug(
|
||||
"disable proxy settings for host {}, because exclude {} matches", host, exclude);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -210,6 +210,18 @@ class HttpURLConnectionFactoryTest {
|
||||
assertThat(usedProxy).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCreateProxyConnectionIfHostIsOnTheExcludeListWithGlobPattern() throws IOException {
|
||||
configuration.setEnableProxy(true);
|
||||
configuration.setProxyServer("proxy.hitchhiker.com");
|
||||
configuration.setProxyPort(3128);
|
||||
configuration.setProxyExcludes(ImmutableSet.of("localhost", "*.hitchhiker.org", "127.0.0.1"));
|
||||
|
||||
connectionFactory.create(new URL("https://scm.hitchhiker.org"));
|
||||
|
||||
assertThat(usedProxy).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotCreateProxyConnectionWithIgnoreOption() throws IOException {
|
||||
configuration.setEnableProxy(true);
|
||||
|
||||
Reference in New Issue
Block a user