-
Notifications
You must be signed in to change notification settings - Fork 57
Implement refresh autodiscover #1091
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2204f7b
Implement refresh autodiscover
Zaprit 5656c84
Make record fields required
Zaprit aeba706
Implement checking for the Patchwork user agent (#1090)
FeTetra 91e10d2
Implement refresh autodiscover
Zaprit 92f9113
Make record fields required
Zaprit ec9f8f3
Merge remote-tracking branch 'origin/autodiscover-endpoint' into auto…
Zaprit 96f4882
Update ProjectLighthouse.Servers.Website/Controllers/AutoDiscoverCont…
Zaprit 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 | ||
| { | ||
| private static readonly int requiredMajor = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum; | ||
| private static readonly int requiredMinor = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum; | ||
|
|
||
| [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; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
ProjectLighthouse.Servers.Website/Controllers/AutoDiscoverController.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,26 @@ | ||
| using LBPUnion.ProjectLighthouse.Configuration; | ||
| using LBPUnion.ProjectLighthouse.Servers.Website.Types; | ||
| using Microsoft.AspNetCore.Mvc; | ||
|
|
||
| namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers; | ||
|
|
||
| [ApiController] | ||
| public class AutoDiscoverController: ControllerBase | ||
| { | ||
| [ResponseCache(Duration = 86400)] | ||
| [HttpGet("autodiscover")] | ||
| [Produces("application/json")] | ||
| public IActionResult AutoDiscover() | ||
| { | ||
| AutoDiscoverResponse resp = new() | ||
| { | ||
| Version = 3, | ||
| Url = ServerConfiguration.Instance.GameApiExternalUrl, | ||
| ServerBrand = ServerConfiguration.Instance.Customization.ServerName, | ||
| UsesCustomDigestKey = false, | ||
| BannerImageUrl = null, | ||
| ServerDescription = ServerConfiguration.Instance.Customization.ServerDescription, | ||
| }; | ||
| return this.Ok(resp); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
ProjectLighthouse.Servers.Website/Types/AutoDiscoverResponse.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,19 @@ | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace LBPUnion.ProjectLighthouse.Servers.Website.Types; | ||
|
|
||
| public record AutoDiscoverResponse | ||
| { | ||
| [JsonProperty("version")] | ||
| public required uint Version { get; set; } | ||
| [JsonProperty("serverBrand")] | ||
| public required string ServerBrand { get; set; } | ||
| [JsonProperty("serverDescription")] | ||
| public required string ServerDescription { get; set; } | ||
| [JsonProperty("url")] | ||
| public required string Url { get; set; } | ||
| [JsonProperty("bannerImageUrl")] | ||
| public string? BannerImageUrl { get; set; } | ||
| [JsonProperty("usesCustomDigestKey")] | ||
| public required bool UsesCustomDigestKey { get; set; } | ||
| } |
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; | ||
| foreach (string userAgent in validUserAgents) | ||
| { | ||
| result = PatchworkHelper.IsValidPatchworkUserAgent(userAgent); | ||
| Assert.True(result, $"Valid user agent: \"{userAgent}\" was evaluated as {result}."); | ||
| } | ||
| foreach (string userAgent in invalidUserAgents) | ||
| { | ||
| result = PatchworkHelper.IsValidPatchworkUserAgent(userAgent); | ||
| Assert.False(result, $"Invalid user agent: \"{userAgent}\" 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
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.