Skip to content

Commit 68dbf50

Browse files
committed
Implement checking for the Patchwork user agent, move logout into standalone method
1 parent df7ebf9 commit 68dbf50

File tree

7 files changed

+72
-8
lines changed

7 files changed

+72
-8
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,11 @@ public async Task<IActionResult> Login()
214214

215215
Logger.Success($"Successfully logged in user {user.Username} as {token.GameVersion} client", LogArea.Login);
216216

217-
user.LastLogin = TimeHelper.TimestampMillis;
217+
string userAgent = this.Request.Headers.UserAgent.ToString();
218+
if (!String.IsNullOrWhiteSpace(userAgent))
219+
{
220+
user.UserAgent = userAgent;
221+
}
218222

219223
await database.SaveChangesAsync();
220224

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using LBPUnion.ProjectLighthouse.Database;
22
using LBPUnion.ProjectLighthouse.Extensions;
3-
using LBPUnion.ProjectLighthouse.Helpers;
3+
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
44
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
55
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
66
using Microsoft.AspNetCore.Authorization;
@@ -30,10 +30,7 @@ public async Task<IActionResult> OnPost()
3030
UserEntity? user = await this.database.UserFromGameToken(token);
3131
if (user == null) return this.Forbid();
3232

33-
user.LastLogout = TimeHelper.TimestampMillis;
34-
35-
await this.database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
36-
await this.database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
33+
await LogoutHelper.Logout(token, user, database);
3734

3835
return this.Ok();
3936
}

ProjectLighthouse.Servers.GameServer/Controllers/MessageController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Globalization;
2+
using System.Text;
23
using LBPUnion.ProjectLighthouse.Configuration;
34
using LBPUnion.ProjectLighthouse.Database;
45
using LBPUnion.ProjectLighthouse.Extensions;
@@ -7,6 +8,7 @@
78
using LBPUnion.ProjectLighthouse.Localization.StringLists;
89
using LBPUnion.ProjectLighthouse.Logging;
910
using LBPUnion.ProjectLighthouse.Serialization;
11+
using LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
1012
using LBPUnion.ProjectLighthouse.Types.Entities.Notifications;
1113
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
1214
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
@@ -54,6 +56,10 @@ public MessageController(DatabaseContext database)
5456
public async Task<IActionResult> Announce()
5557
{
5658
GameTokenEntity token = this.GetToken();
59+
UserEntity? user = await this.database.UserFromGameToken(token);
60+
61+
if (user == null)
62+
return this.Forbid();
5763

5864
string username = await this.database.UsernameFromGameToken(token);
5965

@@ -67,6 +73,18 @@ public async Task<IActionResult> Announce()
6773
announceText.Insert(0, BaseLayoutStrings.ReadOnlyWarn.Translate(LocalizationManager.DefaultLang) + "\n\n");
6874
}
6975

76+
if (ServerConfiguration.Instance.RequirePatchworkUserAgent)
77+
{
78+
announceText.Append("This server instance requires the use of the Patchwork plugin for LBP.\n\n");
79+
80+
if (PatchworkHelper.userHasValidPatchworkUserAgent(user.UserAgent))
81+
{
82+
announceText.Append("It appears you do not have the Patchwork plugin installed correctly." +
83+
"Since this server instance requires it, you will not be able to play until you so.");
84+
}
85+
await LogoutHelper.Logout(token, user, database);
86+
}
87+
7088
#if DEBUG
7189
announceText.Append("\n\n---DEBUG INFO---\n" +
7290
$"user.UserId: {token.UserId}\n" +
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using LBPUnion.ProjectLighthouse.Helpers;
2+
using LBPUnion.ProjectLighthouse.Database;
3+
using LBPUnion.ProjectLighthouse.Extensions;
4+
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
5+
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
6+
7+
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
8+
9+
public class LogoutHelper
10+
{
11+
public static async Task Logout(GameTokenEntity token, UserEntity user, DatabaseContext database)
12+
{
13+
user.LastLogout = TimeHelper.TimestampMillis;
14+
15+
await database.GameTokens.RemoveWhere(t => t.TokenId == token.TokenId);
16+
await database.LastContacts.RemoveWhere(c => c.UserId == token.UserId);
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using LBPUnion.ProjectLighthouse.Configuration;
2+
3+
namespace LBPUnion.ProjectLighthouse.Servers.GameServer.Helpers;
4+
5+
public static class PatchworkHelper
6+
{
7+
static int patchworkMajorVer = ServerConfiguration.Instance.PatchworkMajorVersionMinimum;
8+
static int patchworkMinorVer = ServerConfiguration.Instance.PatchworkMinorVersionMinimum;
9+
public static bool userHasValidPatchworkUserAgent(string userAgent)
10+
{
11+
if (userAgent.StartsWith("PatchworkLBP"))
12+
return false;
13+
14+
string[] patchworkVer = userAgent.Split(' ')[1].Split('.');
15+
if (int.Parse(patchworkVer[0]) >= patchworkMajorVer && int.Parse(patchworkVer[1]) >= patchworkMinorVer)
16+
return true;
17+
18+
return false;
19+
}
20+
}

ProjectLighthouse/Configuration/ServerConfiguration.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ServerConfiguration : ConfigurationBase<ServerConfiguration>
1111
// This is so Lighthouse can properly identify outdated configurations and update them with newer settings accordingly.
1212
// If you are modifying anything here, this value MUST be incremented.
1313
// Thanks for listening~
14-
public override int ConfigVersion { get; set; } = 27;
14+
public override int ConfigVersion { get; set; } = 28;
1515

1616
public override string ConfigName { get; set; } = "lighthouse.yml";
1717
public string WebsiteListenUrl { get; set; } = "http://localhost:10060";
@@ -32,6 +32,11 @@ public class ServerConfiguration : ConfigurationBase<ServerConfiguration>
3232
public bool LogChatFiltering { get; set; } = false;
3333
public bool LogChatMessages { get; set; } = false;
3434

35+
// Require use of Zaprit's "Patchwork" prx plugin's user agent when connecting to the server
36+
// Major and minor version minimums can be left alone if patchwork is not required
37+
public bool RequirePatchworkUserAgent { get; set; } = false;
38+
public int PatchworkMajorVersionMinimum { get; set; } = 0;
39+
public int PatchworkMinorVersionMinimum { get; set; } = 0;
3540
public AuthenticationConfiguration Authentication { get; set; } = new();
3641
public CaptchaConfiguration Captcha { get; set; } = new();
3742
public DigestKeyConfiguration DigestKey { get; set; } = new();

ProjectLighthouse/Types/Entities/Profile/UserEntity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class UserEntity
2828

2929
public string IconHash { get; set; }
3030

31+
public string UserAgent { get; set;}
32+
3133
/// <summary>
3234
/// Markup that displays the username next to a polaroid with the user's icon.
3335
/// This can be used everywhere markup works ingame, e.g. news or notifications

0 commit comments

Comments
 (0)