-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add presence server + hook up to APIv3 setAsOverride
- Loading branch information
Showing
29 changed files
with
967 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
namespace Refresh.Common.Constants; | ||
|
||
public static class EndpointRoutes | ||
{ | ||
public const string PresenceBaseRoute = "/_internal/presence/"; | ||
} |
This file contains 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 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 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 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 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 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 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
49 changes: 49 additions & 0 deletions
49
Refresh.GameServer/Endpoints/Internal/Presence/PresenceEndpoints.cs
This file contains 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,49 @@ | ||
using Bunkum.Core; | ||
using Bunkum.Core.Endpoints; | ||
using Bunkum.Core.Responses; | ||
using Bunkum.Protocols.Http; | ||
using Refresh.GameServer.Authentication; | ||
using Refresh.GameServer.Database; | ||
using Refresh.GameServer.Types.UserData; | ||
|
||
namespace Refresh.GameServer.Endpoints.Internal.Presence; | ||
|
||
public class PresenceEndpoints : EndpointGroup | ||
{ | ||
// Test endpoint to allow the presence server to make sure presence support is enabled and configured correctly | ||
[PresenceEndpoint("test", HttpMethods.Post), Authentication(false)] | ||
public Response TestSecret(RequestContext context) | ||
{ | ||
return OK; | ||
} | ||
|
||
[PresenceEndpoint("informConnection", HttpMethods.Post), Authentication(false)] | ||
public Response InformConnection(RequestContext context, GameDatabaseContext database, string body) | ||
{ | ||
GameUser? user = database.GetUserFromTokenData(body, TokenType.Game); | ||
|
||
if (user == null) | ||
return NotFound; | ||
|
||
database.SetUserPresenceAuthToken(user, body); | ||
|
||
context.Logger.LogInfo(RefreshContext.Presence, $"User {user} connected to the presence server"); | ||
|
||
return OK; | ||
} | ||
|
||
[PresenceEndpoint("informDisconnection", HttpMethods.Post), Authentication(false)] | ||
public Response InformDisconnection(RequestContext context, GameDatabaseContext database, string body) | ||
{ | ||
GameUser? user = database.GetUserFromTokenData(body, TokenType.Game); | ||
|
||
if (user == null) | ||
return NotFound; | ||
|
||
database.SetUserPresenceAuthToken(user, null); | ||
|
||
context.Logger.LogInfo(RefreshContext.Presence, $"User {user} disconnected from the presence server"); | ||
|
||
return OK; | ||
} | ||
} |
This file contains 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 Bunkum.Protocols.Http; | ||
using JetBrains.Annotations; | ||
using Refresh.Common.Constants; | ||
|
||
namespace Refresh.GameServer.Endpoints; | ||
|
||
[MeansImplicitUse] | ||
public class PresenceEndpointAttribute : HttpEndpointAttribute | ||
{ | ||
public const string BaseRoute = EndpointRoutes.PresenceBaseRoute; | ||
|
||
public PresenceEndpointAttribute(string route, HttpMethods method = HttpMethods.Get, string contentType = Bunkum.Listener.Protocol.ContentType.Plaintext) | ||
: base(BaseRoute + route, method, contentType) | ||
{} | ||
|
||
public PresenceEndpointAttribute(string route, string contentType, HttpMethods method = HttpMethods.Get) | ||
: base(BaseRoute + route, method, contentType) | ||
{} | ||
} |
39 changes: 39 additions & 0 deletions
39
Refresh.GameServer/Middlewares/PresenceAuthenticationMiddleware.cs
This file contains 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,39 @@ | ||
using Bunkum.Core.Database; | ||
using Bunkum.Core.Endpoints.Middlewares; | ||
using Bunkum.Listener.Request; | ||
using Refresh.GameServer.Configuration; | ||
using Refresh.GameServer.Endpoints; | ||
|
||
namespace Refresh.GameServer.Middlewares; | ||
|
||
public class PresenceAuthenticationMiddleware : IMiddleware | ||
{ | ||
private IntegrationConfig _config; | ||
|
||
public PresenceAuthenticationMiddleware(IntegrationConfig config) | ||
{ | ||
this._config = config; | ||
} | ||
|
||
public void HandleRequest(ListenerContext context, Lazy<IDatabaseContext> database, Action next) | ||
{ | ||
if (context.Uri.AbsolutePath.StartsWith(PresenceEndpointAttribute.BaseRoute)) | ||
{ | ||
// Block presence requests if not enabled | ||
if (!this._config.PresenceEnabled) | ||
{ | ||
context.ResponseCode = NotImplemented; | ||
return; | ||
} | ||
|
||
// Block presence requests with a bad auth token | ||
if (context.RequestHeaders["Authorization"] != this._config.PresenceSharedSecret) | ||
{ | ||
context.ResponseCode = Unauthorized; | ||
return; | ||
} | ||
} | ||
|
||
next(); | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -10,4 +10,5 @@ public enum RefreshContext | |
CoolLevels, | ||
Publishing, | ||
Aipi, | ||
Presence, | ||
} |
This file contains 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.