Skip to content

Commit

Permalink
remove legacy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
danthe1st committed Mar 1, 2024
1 parent 9ca45c5 commit 6b00a60
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 99 deletions.
14 changes: 0 additions & 14 deletions intercept.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package io.github.danthe1st.httpsintercept.config;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -20,43 +14,6 @@ public record HostMatcher(

private static final Logger LOG = LoggerFactory.getLogger(HostMatcher.class);

public static HostMatcher load(Path config) throws IOException {
if(!Files.exists(config)){
Files.createFile(config);
return load(Collections.emptyList());
}
List<String> hostDeclarations = Files.readAllLines(config);
return load(hostDeclarations);
}

static HostMatcher load(List<String> hostDeclarations) {
Set<String> exactHosts = new HashSet<>();
Set<String> hostParts = new HashSet<>();
Set<Pattern> hostRegexes = new HashSet<>();
for(String declaration : hostDeclarations){
processHostDeclaration(declaration, exactHosts, hostParts, hostRegexes);
}
return new HostMatcher(exactHosts, hostParts, hostRegexes);
}

private static void processHostDeclaration(String declaration, Set<String> exactHosts, Set<String> hostParts, Set<Pattern> hostRegexes) {
if(declaration.isBlank() || declaration.startsWith("#")){
return;
}
if(declaration.startsWith("/")){
String hostRegex = declaration.substring(1);
try{
hostRegexes.add(Pattern.compile(hostRegex));
}catch(PatternSyntaxException e){
LOG.error("invalid regex: {}", hostRegex);
}
}else if(declaration.startsWith("*.")){
hostParts.add(declaration.substring(2));
}else{
exactHosts.add(declaration);
}
}

public HostMatcher(Set<String> exactHosts, Set<String> hostParts, Set<Pattern> hostRegexes) {
this.exactHosts = copyOrEmptyIfNull(exactHosts);
this.hostParts = copyOrEmptyIfNull(hostParts);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.github.danthe1st.httpsintercept.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import io.github.danthe1st.httpsintercept.config.HostMatcher;
import org.junit.jupiter.api.Test;

class HostMatcherTests {
Expand Down Expand Up @@ -53,43 +50,4 @@ void testRegexMatch() {
assertFalse(hostMatcher.matches("github.com"));
assertFalse(hostMatcher.matches(""));
}

@Test
void testLoadExact() {
HostMatcher hostMatcher = HostMatcher.load(List.of("example.com"));
assertEquals(Set.of("example.com"), hostMatcher.exactHosts());
assertEquals(Collections.emptySet(), hostMatcher.hostParts());
}

@Test
void testLoadParts() {
HostMatcher hostMatcher = HostMatcher.load(List.of("*.example.com"));
assertEquals(Collections.emptySet(), hostMatcher.exactHosts());
assertEquals(Set.of("example.com"), hostMatcher.hostParts());

assertTrue(hostMatcher.matches("host.example.com"));
}

@Test
void testLoadEmptyPart() {
HostMatcher hostMatcher = HostMatcher.load(List.of("*."));
assertEquals(Collections.emptySet(), hostMatcher.exactHosts());
assertEquals(Set.of(""), hostMatcher.hostParts());

assertFalse(hostMatcher.matches("something"));
assertFalse(hostMatcher.matches("example.com"));
}

@Test
void testLoadRegex() {
HostMatcher hostMatcher = HostMatcher.load(List.of("/ex.+\\.com"));
assertTrue(hostMatcher.matches("example.com"));
assertFalse(hostMatcher.matches("github.com"));

hostMatcher = HostMatcher.load(List.of("/.*"));
assertTrue(hostMatcher.matches("example.com"));
assertTrue(hostMatcher.matches("github.com"));
assertTrue(hostMatcher.matches("localhost"));
assertTrue(hostMatcher.matches(""));
}
}

0 comments on commit 6b00a60

Please sign in to comment.