-
-
Notifications
You must be signed in to change notification settings - Fork 893
Add wildcard forced hosts (fix #1587) #1826
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
Open
lllincoln
wants to merge
10
commits into
PaperMC:dev/3.0.0
Choose a base branch
from
lllincoln:feature/wildcard-forced-hosts
base: dev/3.0.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−4
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f0d336
Add wildcard forced hosts support (#1587)
lllincoln cdbf7b5
Add additional test cases
lllincoln b777c79
Reformat test to pass checkstyle
lllincoln 415d127
Add missing GPL header
lllincoln 70efa55
Use fictious domains, add more test cases, and update GPL year to 2026
lllincoln 268f942
Split tests into separate methods
lllincoln 9093623
Update javadoc, add @Nullable, and flip comparison for `isHostMatchin…
lllincoln 36fba4a
Move tests to AddressUtilTest in correct package, import isHostMatchi…
lllincoln 54d6816
Extract server resolution logic to separate method and optimize reada…
lllincoln 845de16
Use method reference in lambda
lllincoln File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
71 changes: 71 additions & 0 deletions
71
proxy/src/test/java/com/velocitypowered/proxy/connection/client/HostPatternMatchingTest.java
This file contains hidden or 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,71 @@ | ||
| /* | ||
| * Copyright (C) 2018-2026 Velocity Contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.velocitypowered.proxy.connection.client; | ||
|
|
||
| import com.velocitypowered.proxy.util.AddressUtil; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class HostPatternMatchingTest { | ||
|
lllincoln marked this conversation as resolved.
Outdated
|
||
| @Test | ||
| void testOneWildcardMatches() { | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("*.example.com", "play.example.com")); | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("*.example.com", "a.example.com")); | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("b.*.example.com", "b.a.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testMultipleWildcardsMatch() { | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("*.*.example.com", "a.b.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testDifferentNumberOfLabelsDoNotMatch() { | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern("*.example.com", "a.b.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testWildcardsDoNotMatchApexDomain() { | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern("*.example.com", "example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testExactMatchesMatch() { | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("example.com", "example.com")); | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("play.example.com", "play.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testDifferentDomainsDoNotMatch() { | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern("otherdomain.com", "example.com")); | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern("a.otherdomain.com", "a.example.com")); | ||
| } | ||
|
|
||
| @Test | ||
| void testMalformedArgumentsDoNotMatch() { | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern(null, "example.com")); | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern("example.com", null)); | ||
| Assertions.assertFalse(AddressUtil.isHostMatchingPattern(null, null)); | ||
| } | ||
|
|
||
| @Test | ||
| void testCaseInsensitivityMatches() { | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("Example.COM", "example.com")); | ||
| Assertions.assertTrue(AddressUtil.isHostMatchingPattern("*.Example.com", "play.EXAMPLE.com")); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three nits:
1 - This is a pretty large method chain that's duplicated twice, it would probably make sense to extract this out into its own method.
2 - the "or default" value is always computed, even in the case where the exact
virtualHostStrmapping is present. And the.orElse(Collections.emptyList())also always returns an empty list. Fortunately this doesn't comput or allocate anything, but this means that all three cases - exact match, pattern match, empty list - are always computed, even when an exact match could abort the latter two, which is a smell3 - Essentially this is a giant double if-else statement:
However this is disguised in
getOrDefault(first twoifandif else) and.orElse(Collections.emptyList()(lastelse). I would probably make this more explicit, either something similar to the above pseudo code or using an optional chain with 2x.orElseGet(), keeping it all in optional-land.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I was thinking about this too. Where do you think the right place to hold this new method could be? I would have put it in
ConnectedPlayer, but this same pattern is inServerListPingHandleras well, which are two separate things from each other. Thoughts?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AddressUtilseems like the right place, as you have already introduced the newisHostMatchingPatternthere.Consider making it return an
Optional<String>, allowing the caller to change an empty result to an empty list (ConnectedPlayer) orserver.getConfiguration().getAttemptConnectionOrder()(ServerListPingHandler). (withorElseGet()to avoid computing when the optional is present)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See 54d6816. I've implemented it using
Optional<String>but not 2x.orElseGet()since I find using if/else and a for loop a bit more readable, but let me know if you think otherwise