-
Notifications
You must be signed in to change notification settings - Fork 56
Implement checking for the Patchwork user agent #1090
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
68dbf50
Implement checking for the Patchwork user agent, move logout into sta…
FeTetra 0c8ff50
Quick fixes (awesome name)
FeTetra b84bf02
403 user at login instead of logging out at /announce
FeTetra 63b61d8
Move configuration and revert logout changes
FeTetra b77b7b3
Rework parsing to check against GameVersion enum and game token GameV…
FeTetra bab8a0f
Fix logic error
FeTetra ef5bfd4
Fix Zaprit suggestions
FeTetra c29eb38
Fix Zaprit suggestions
FeTetra b838d80
Simplify patchwork game version test
FeTetra e6c75d2
Test patchwork user agent with regex instead
FeTetra f681ef3
Fix Qodana warnings
FeTetra d90b4d2
Fix remaining Qodana warnings
FeTetra 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
24 changes: 24 additions & 0 deletions
24
ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs
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,24 @@ | ||
| using LBPUnion.ProjectLighthouse.Configuration; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers; | ||
|
|
||
| public static partial class PatchworkHelper | ||
| { | ||
| static readonly int requiredMajor = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum; | ||
FeTetra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static readonly int requiredMinor = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum; | ||
FeTetra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| [GeneratedRegex(@"^PatchworkLBP[123V] (\d{1,5})\.(\d{1,5})$")] | ||
| private static partial Regex PatchworkUserAgentRegex(); | ||
|
|
||
| public static bool IsValidPatchworkUserAgent(string userAgent) | ||
| { | ||
| Match result = PatchworkUserAgentRegex().Match(userAgent); | ||
| if (!result.Success) return false; | ||
|
|
||
| if (!int.TryParse(result.Groups[1].Value, out int major) || !int.TryParse(result.Groups[2].Value, out int minor)) | ||
| return false; | ||
|
|
||
| return major >= requiredMajor && minor >= requiredMinor; | ||
| } | ||
| } | ||
44 changes: 44 additions & 0 deletions
44
ProjectLighthouse.Tests.GameApiTests/Unit/PatchworkUserAgentTests.cs
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,44 @@ | ||
| using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers; | ||
| using Xunit; | ||
|
|
||
| namespace ProjectLighthouse.Tests.GameApiTests.Unit; | ||
|
|
||
| [Trait("Category", "Unit")] | ||
| public class PatchworkUserAgentTests | ||
| { | ||
| [Fact] | ||
| public void CanValidatePatchworkUserAgents() | ||
| { | ||
| string[] validUserAgents = { | ||
| "PatchworkLBP1 1.0", | ||
| "PatchworkLBP2 2.0", | ||
| "PatchworkLBP3 3.0", | ||
| "PatchworkLBPV 4.0", | ||
| "PatchworkLBP1 1.5", | ||
| }; | ||
|
|
||
| string[] invalidUserAgents = { | ||
| // Matching | ||
| "patchworklbp1 1.0", // Case sensitive | ||
| "ptchwrklbp1 1.0", // Misspelled | ||
| "PatchworkLBP1 1", // Missing major/minor | ||
| "PatchworkLBP1 1.000001", // Major/minor too long | ||
|
|
||
| // Data | ||
| "PatchworkLBP1 0.5", // Version number too low | ||
| "PatchworkLBP1 A.0" // Int cannot be parsed | ||
| }; | ||
|
|
||
| bool result; | ||
| for (int i = 0; i < validUserAgents.Length; i++) | ||
| { | ||
| result = PatchworkHelper.IsValidPatchworkUserAgent(validUserAgents[i]); | ||
| Assert.True(result, $"Valid user agent: \"{validUserAgents[i]}\" was evaluated as {result}."); | ||
| } | ||
| for (int i = 0; i < invalidUserAgents.Length; i++) | ||
| { | ||
| result = PatchworkHelper.IsValidPatchworkUserAgent(invalidUserAgents[i]); | ||
| Assert.False(result, $"Invalid user agent: \"{invalidUserAgents[i]}\" was evaluated as {result}."); | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.