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

Exclusions list feature #5

Open
wants to merge 2 commits 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ network:
proxy:
enable: true # allows disabling auto-config; enabled by default

alwaysPrint: false #allow the info log showing the proxy used every time a call is made

exclusions: []# remove log for this hosts while alwaysprint is enabled

# explicit list of proxy servers with settings
servers:
# 1: the intranet proxy (doesn't require any authentication)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>com.orange.common</groupId>
<artifactId>spring-boot-autoconfigure-proxy</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<name>spring-boot-autoconfigure-proxy</name>
<description>Spring Boot AutoConfigure Proxy</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.*;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import org.springframework.util.CollectionUtils;


/**
* This is a {@link ProxySelector} implementation able to manage several {@link Proxy} depending on the uri scheme and host
Expand Down Expand Up @@ -85,10 +87,17 @@ public int hashCode() {

private final List<ProxyEntry> proxies;

private final boolean alwaysPrint;

private final List<String> exclusions;

private Map<SchemeAndHost, List<Proxy>> hostname2Proxies = new HashMap<>();

private MultiProxySelector(List<ProxyEntry> proxies) {
private MultiProxySelector(List<ProxyEntry> proxies, boolean alwaysPrint,
List<String> exclusions) {
this.proxies = proxies;
this.alwaysPrint = alwaysPrint;
this.exclusions = exclusions;
}

@Override
Expand Down Expand Up @@ -126,7 +135,13 @@ public List<Proxy> select(URI uri) {
throw new IllegalArgumentException("protocol = " + protocol + " host = " + host);
}

return hostname2Proxies.computeIfAbsent(new SchemeAndHost(protocol, host), this::doGetProxies);
SchemeAndHost schemeAndHost = new SchemeAndHost(protocol, host);
List<Proxy> proxiesList =
hostname2Proxies.computeIfAbsent(schemeAndHost, this::doGetProxies);
if (alwaysPrint && !CollectionUtils.contains(exclusions.iterator(), host)) {
LOGGER.info("Proxies for [{}] : {}", schemeAndHost, proxiesList);
}
return proxiesList;
}

private List<Proxy> doGetProxies(SchemeAndHost schemeAndHost) {
Expand All @@ -151,7 +166,8 @@ public String toString() {
'}';
}

static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> proxies) {
static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> proxies,
boolean alwaysPrint, List<String> exclusions) {
List<ProxyEntry> proxyEntries = new ArrayList<>();
for (int i = 0; i < proxies.size(); i++) {
NetworkProxyProperties.ProxyServerConfig cfg = proxies.get(i);
Expand Down Expand Up @@ -190,6 +206,6 @@ static MultiProxySelector build(List<NetworkProxyProperties.ProxyServerConfig> p
proxyEntries.add(new ProxyEntry(cfg, proxy, positiveMatchers, negativeMatchers));
}

return new MultiProxySelector(proxyEntries);
return new MultiProxySelector(proxyEntries, alwaysPrint, exclusions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void setupProxyConfiguration() {
LOGGER.info("Configuring proxies from Spring Boot configuration");

// install proxy selector
ProxySelector.setDefault(MultiProxySelector.build(properties.getServers()));
ProxySelector.setDefault(MultiProxySelector.build(properties.getServers(),
properties.isAlwaysPrint(), properties.getExclusions()));

// set password authentication for every proxy that need one
for (NetworkProxyProperties.ProxyServerConfig cfg : properties.getServers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ public class NetworkProxyProperties implements Validator {
*/
private boolean enabled = true;

/**
* Whether to enable the print of the proxy used every time.
*/
private boolean alwaysPrint = false;


/**
* Exclude logs for those hosts
*/
private List<String> exclusions = Collections.emptyList();


/**
* Explicit network proxy servers configuration
*/
Expand All @@ -43,10 +55,28 @@ public void setServers(List<ProxyServerConfig> servers) {
this.servers = servers;
}

public List<String> getExclusions() {
return exclusions;
}

public void setExclusions(List<String> exclusions) {
this.exclusions = exclusions;
}

public boolean isAlwaysPrint() {
return alwaysPrint;
}

public void setAlwaysPrint(boolean alwaysPrint) {
this.alwaysPrint = alwaysPrint;
}

@Override
public String toString() {
return "NetworkProxyProperties{" +
"enabled=" + enabled +
", alwaysPrint= " +alwaysPrint +
", exclusions= " +exclusions +
", servers=" + servers +
'}';
}
Expand Down