Skip to content

Commit b77b7b3

Browse files
committed
Rework parsing to check against GameVersion enum and game token GameVersion
1 parent 63b61d8 commit b77b7b3

3 files changed

Lines changed: 28 additions & 13 deletions

File tree

ProjectLighthouse.Servers.GameServer/Controllers/Login/LoginController.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ public async Task<IActionResult> Login()
6767

6868
UserEntity? user;
6969

70-
if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent)
71-
{
72-
if (!PatchworkHelper.UserHasValidPatchworkUserAgent(this.Request.Headers.UserAgent.ToString()))
73-
{
74-
return this.Forbid();
75-
}
76-
}
77-
7870
switch (npTicket.Platform)
7971
{
8072
case Platform.RPCS3:
@@ -221,6 +213,14 @@ public async Task<IActionResult> Login()
221213
return this.Forbid();
222214
}
223215

216+
if (ServerConfiguration.Instance.Authentication.RequirePatchworkUserAgent)
217+
{
218+
if (!PatchworkHelper.UserHasValidPatchworkUserAgent(token, this.Request.Headers.UserAgent.ToString()))
219+
{
220+
return this.Forbid();
221+
}
222+
}
223+
224224
Logger.Success($"Successfully logged in user {user.Username} as {token.GameVersion} client", LogArea.Login);
225225

226226
user.LastLogin = TimeHelper.TimestampMillis;

ProjectLighthouse.Servers.GameServer/Controllers/Login/LogoutController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using LBPUnion.ProjectLighthouse.Database;
22
using LBPUnion.ProjectLighthouse.Extensions;
3-
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
43
using LBPUnion.ProjectLighthouse.Helpers;
54
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
65
using LBPUnion.ProjectLighthouse.Types.Entities.Token;

ProjectLighthouse.Servers.GameServer/Helpers/PatchworkHelper.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
using LBPUnion.ProjectLighthouse.Configuration;
2+
using LBPUnion.ProjectLighthouse.Configuration.ConfigurationCategories;
3+
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
4+
using LBPUnion.ProjectLighthouse.Types.Users;
25

36
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
47

58
public static class PatchworkHelper
69
{
7-
static int patchworkMajorVer = ServerConfiguration.Instance.PatchworkMajorVersionMinimum;
8-
static int patchworkMinorVer = ServerConfiguration.Instance.PatchworkMinorVersionMinimum;
9-
public static bool UserHasValidPatchworkUserAgent(string userAgent)
10+
static int patchworkMajorVer = ServerConfiguration.Instance.Authentication.PatchworkMajorVersionMinimum;
11+
static int patchworkMinorVer = ServerConfiguration.Instance.Authentication.PatchworkMinorVersionMinimum;
12+
public static bool UserHasValidPatchworkUserAgent(GameTokenEntity token, string userAgent)
1013
{
1114
string userAgentPrefix = "PatchworkLBP";
1215
char gameVersion = userAgent[userAgentPrefix.Length];
16+
int numericVersion = 0;
1317

1418
if (userAgent.StartsWith(userAgentPrefix))
1519
return false;
1620

17-
if (gameVersion is not '1' or '2' or '3' or 'V')
21+
if (char.IsLetterOrDigit(gameVersion))
22+
{
23+
if (gameVersion == 'V')
24+
numericVersion = 4;
25+
}
26+
else
27+
numericVersion = gameVersion - '0';
28+
29+
// Don't want it to be 0 still because of Unknown (-1) in GameVersion
30+
if (numericVersion == 0)
31+
return false;
32+
33+
if (numericVersion - 1 != (int)token.GameVersion && !Enum.IsDefined(typeof(GameVersion), numericVersion))
1834
return false;
1935

2036
string[] patchworkVer = userAgent.Split(' ')[1].Split('.');

0 commit comments

Comments
 (0)