From 6b00a601e68a425aab30d94e0893cb801fcb5aac Mon Sep 17 00:00:00 2001 From: danthe1st Date: Fri, 1 Mar 2024 19:05:08 +0100 Subject: [PATCH] remove legacy loading --- intercept.yaml | 14 ------ .../httpsintercept/config/HostMatcher.java | 43 ------------------- .../config/HostMatcherTests.java | 42 ------------------ 3 files changed, 99 deletions(-) delete mode 100644 intercept.yaml diff --git a/intercept.yaml b/intercept.yaml deleted file mode 100644 index 716a29e..0000000 --- a/intercept.yaml +++ /dev/null @@ -1,14 +0,0 @@ -ignoredHosts: - exactHosts: - - discord.com - - music.youtube.com - - zoom.us - hostParts: - - discord.com - - discordapp.com - - discordapp.net - - discord.media - - discord.gg - - keybaseapi.com - - zoom.us - hostRegexes: [] diff --git a/src/main/java/io/github/danthe1st/httpsintercept/config/HostMatcher.java b/src/main/java/io/github/danthe1st/httpsintercept/config/HostMatcher.java index db7871a..e1e8ca5 100644 --- a/src/main/java/io/github/danthe1st/httpsintercept/config/HostMatcher.java +++ b/src/main/java/io/github/danthe1st/httpsintercept/config/HostMatcher.java @@ -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; @@ -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 hostDeclarations = Files.readAllLines(config); - return load(hostDeclarations); - } - - static HostMatcher load(List hostDeclarations) { - Set exactHosts = new HashSet<>(); - Set hostParts = new HashSet<>(); - Set 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 exactHosts, Set hostParts, Set 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 exactHosts, Set hostParts, Set hostRegexes) { this.exactHosts = copyOrEmptyIfNull(exactHosts); this.hostParts = copyOrEmptyIfNull(hostParts); diff --git a/src/test/java/io/github/danthe1st/httpsintercept/config/HostMatcherTests.java b/src/test/java/io/github/danthe1st/httpsintercept/config/HostMatcherTests.java index 920d2be..09d40ee 100644 --- a/src/test/java/io/github/danthe1st/httpsintercept/config/HostMatcherTests.java +++ b/src/test/java/io/github/danthe1st/httpsintercept/config/HostMatcherTests.java @@ -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 { @@ -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("")); - } }