-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/io/github/danthe1st/httpsintercept/control/HostMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.danthe1st.httpsintercept.control; | ||
|
||
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; | ||
|
||
public class HostMatcher { | ||
private Set<String> exactHosts = new HashSet<>(); | ||
private Set<String> hostParts = new HashSet<>(); | ||
|
||
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<>(); | ||
for(String ignored : hostDeclarations){ | ||
if(ignored.startsWith("*.")){ | ||
hostParts.add(ignored.substring(2)); | ||
}else{ | ||
exactHosts.add(ignored); | ||
} | ||
} | ||
return new HostMatcher(exactHosts, hostParts); | ||
} | ||
|
||
HostMatcher(Set<String> exactHosts, Set<String> hostParts) { | ||
this.exactHosts = Set.copyOf(exactHosts); | ||
this.hostParts = Set.copyOf(hostParts); | ||
} | ||
|
||
public boolean matches(String hostname) { | ||
return exactHosts.contains(hostname) || doesMatchPart(hostname); | ||
} | ||
|
||
private boolean doesMatchPart(String hostname) { | ||
if(hostParts.isEmpty()){ | ||
return false; | ||
} | ||
|
||
int index = 0; | ||
while((index = hostname.indexOf('.', index) + 1) != 0 && index < hostname.length()){ | ||
if(hostParts.contains(hostname.substring(index))){ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
Set<String> getExactHosts() { | ||
return Collections.unmodifiableSet(exactHosts); | ||
} | ||
|
||
Set<String> getHostParts() { | ||
return Collections.unmodifiableSet(hostParts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/io/github/danthe1st/httpsintercept/control/HostMatcherTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.github.danthe1st.httpsintercept.control; | ||
|
||
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 org.junit.jupiter.api.Test; | ||
|
||
class HostMatcherTests { | ||
@Test | ||
void testExactMatch() { | ||
HostMatcher matcher = new HostMatcher(Set.of("example.com"), Collections.emptySet()); | ||
assertTrue(matcher.matches("example.com")); | ||
assertFalse(matcher.matches("github.com")); | ||
} | ||
|
||
@Test | ||
void testPartMatch() { | ||
HostMatcher matcher = new HostMatcher(Collections.emptySet(), Set.of("example.com")); | ||
assertTrue(matcher.matches("host.example.com")); | ||
assertTrue(matcher.matches(".example.com")); | ||
assertFalse(matcher.matches("host.github.com")); | ||
assertFalse(matcher.matches("example.com")); | ||
assertFalse(matcher.matches("example.com.")); | ||
assertFalse(matcher.matches("")); | ||
assertFalse(matcher.matches(".")); | ||
} | ||
|
||
@Test | ||
void testLoadExact() { | ||
HostMatcher hostMatcher = HostMatcher.load(List.of("example.com")); | ||
assertEquals(Set.of("example.com"), hostMatcher.getExactHosts()); | ||
assertEquals(Collections.emptySet(), hostMatcher.getHostParts()); | ||
} | ||
|
||
@Test | ||
void testLoadParts() { | ||
HostMatcher hostMatcher = HostMatcher.load(List.of("*.example.com")); | ||
assertEquals(Collections.emptySet(), hostMatcher.getExactHosts()); | ||
assertEquals(Set.of("example.com"), hostMatcher.getHostParts()); | ||
|
||
assertTrue(hostMatcher.matches("host.example.com")); | ||
} | ||
|
||
@Test | ||
void testLoadEmptyPart() { | ||
HostMatcher hostMatcher = HostMatcher.load(List.of("*.")); | ||
assertEquals(Collections.emptySet(), hostMatcher.getExactHosts()); | ||
assertEquals(Set.of(""), hostMatcher.getHostParts()); | ||
|
||
assertFalse(hostMatcher.matches("something")); | ||
assertFalse(hostMatcher.matches("example.com")); | ||
} | ||
|
||
} |