Skip to content

Commit

Permalink
match all if no matcher provided
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Mar 8, 2024
1 parent fd491c7 commit 7316782
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package io.github.danthe1st.httpsintercept.config;

import java.util.Objects;
import java.util.Collections;
import java.util.Set;

public record HostMatcherConfig(Set<String> exactHosts,
Set<String> hostParts,
Set<String> hostRegexes) {
public HostMatcherConfig {
Objects.requireNonNull(exactHosts);
Objects.requireNonNull(hostParts);
Objects.requireNonNull(hostRegexes);

public HostMatcherConfig(Set<String> exactHosts, Set<String> hostParts, Set<String> hostRegexes) {
this.exactHosts = emptyIfNull(exactHosts);
this.hostParts = emptyIfNull(hostParts);
this.hostRegexes = emptyIfNull(hostRegexes);
}

private Set<String> emptyIfNull(Set<String> data) {
if(data == null){
return Collections.emptySet();
}
return Set.copyOf(data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public ServerHandlersInit(Bootstrap clientBootstrap, Config config) throws KeySt
List<PreForwardRule> preForwardRules = config.preForwardRules();
List<Map.Entry<HostMatcherConfig, PreForwardRule>> rules = new ArrayList<>();
for(PreForwardRule preForwardRule : preForwardRules){
rules.add(Map.entry(preForwardRule.hostMatcher(), preForwardRule));
HostMatcherConfig hostMatcher = preForwardRule.hostMatcher();
if(hostMatcher == null){
hostMatcher = new HostMatcherConfig(null, null, null);
}
rules.add(Map.entry(hostMatcher, preForwardRule));
}
preForwardMatcher = new IterativeHostMatcher<>(rules);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ public final class IterativeHostMatcher<T> {
private final Map<String, List<T>> exactHosts;
private final Map<String, List<T>> hostParts;
private final Map<Pattern, List<T>> hostRegexes;
private final List<T> wildcards;

public IterativeHostMatcher(List<Map.Entry<HostMatcherConfig, T>> configs) {
Map<String, List<T>> hosts = new HashMap<>();
Map<String, List<T>> parts = new HashMap<>();
Map<Pattern, List<T>> regexes = new HashMap<>();
List<T> wildcardElements = new ArrayList<>();
for(Map.Entry<HostMatcherConfig, T> entry : configs){
HostMatcherConfig config = entry.getKey();
T value = entry.getValue();
addToMap(hosts, value, config.exactHosts(), Function.identity());
addToMap(parts, value, config.hostParts(), Function.identity());
addToMap(regexes, value, config.hostRegexes(), Pattern::compile);
if(config.exactHosts().isEmpty() && config.hostParts().isEmpty() && config.hostRegexes().isEmpty()){
wildcardElements.add(value);
}else{
addToMap(hosts, value, config.exactHosts(), Function.identity());
addToMap(parts, value, config.hostParts(), Function.identity());
addToMap(regexes, value, config.hostRegexes(), Pattern::compile);
}
}
this.exactHosts = toImmutable(hosts);
this.hostParts = toImmutable(parts);
this.hostRegexes = toImmutable(regexes);
this.wildcards = List.copyOf(wildcardElements);
}

private <K> void addToMap(Map<K, List<T>> multimap, T value, Set<String> configValue, Function<String, K> keyTransformer) {
Expand All @@ -59,6 +66,7 @@ public Iterator<T> allMatches(String hostname) {
iterators.add(new HostPartIterator<>(hostname, hostParts));
}
iterators.add(new RegexIterator<>(hostRegexes, hostname));
iterators.add(wildcards.iterator());

Iterator<T> it = new IteratingIterator<>() {
private Iterator<T> current = iterators.poll();
Expand Down

0 comments on commit 7316782

Please sign in to comment.