mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-28 07:58:40 +00:00
Test patchwork user agent with regex instead
This commit is contained in:
parent
b838d805e2
commit
e6c75d2b86
3 changed files with 54 additions and 19 deletions
|
@ -215,7 +215,7 @@ public class LoginController : ControllerBase
|
||||||
|
|
||||||
if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent)
|
if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent)
|
||||||
{
|
{
|
||||||
if (!PatchworkHelper.UserHasValidPatchworkUserAgent(this.Request.Headers.UserAgent.ToString()))
|
if (!PatchworkHelper.IsValidPatchworkUserAgent(this.Request.Headers.UserAgent.ToString()))
|
||||||
{
|
{
|
||||||
return this.Forbid();
|
return this.Forbid();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,31 +1,21 @@
|
||||||
using LBPUnion.ProjectLighthouse.Configuration;
|
using LBPUnion.ProjectLighthouse.Configuration;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
|
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
|
||||||
|
|
||||||
public static class PatchworkHelper
|
public static class PatchworkHelper
|
||||||
{
|
{
|
||||||
static int patchworkMajorVer = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum;
|
static int requiredMajor = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum;
|
||||||
static int patchworkMinorVer = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum;
|
static int requiredMinor = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum;
|
||||||
public static bool UserHasValidPatchworkUserAgent(string userAgent)
|
public static bool IsValidPatchworkUserAgent(string userAgent)
|
||||||
{
|
{
|
||||||
string userAgentPrefix = "PatchworkLBP";
|
Match result = Regex.Match(userAgent, @"^PatchworkLBP[123V] (\d{1,5})\.(\d{1,5})$");
|
||||||
char gameVersion = userAgent[userAgentPrefix.Length];
|
if (!result.Success) return false;
|
||||||
|
|
||||||
if (!userAgent.StartsWith(userAgentPrefix))
|
if (!int.TryParse(result.Groups[1].Value, out int major) || !int.TryParse(result.Groups[2].Value, out int minor))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
switch (gameVersion) {
|
if (major < requiredMajor || minor < requiredMinor)
|
||||||
case '1':
|
|
||||||
case '2':
|
|
||||||
case '3':
|
|
||||||
case 'V':
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] patchworkVer = userAgent.Split(' ')[1].Split('.');
|
|
||||||
if (int.Parse(patchworkVer[0]) !>= patchworkMajorVer || int.Parse(patchworkVer[1]) !>= patchworkMinorVer)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
using System;
|
||||||
|
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Helpers;
|
||||||
|
|
||||||
|
[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}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue