-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial commit * IOC * no lists in toml * maybe * hm * dependent * undo * no tolerance * maybe no instantiate * double * maybe strings * patch * proper string handling * go back * redo * lets try this way eh * auth cmon * test logging * whoops * our own record * put it inside the if * eh * the worlds a stage * or not * hu * cleanup * rearrange the try catch to include the initial connection call as well * shorten it up try to simplify * no jack * locale
- Loading branch information
1 parent
1622c31
commit 652b8e9
Showing
10 changed files
with
136 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
using System.Net.Http.Json; | ||
using System.Net.Http.Headers; | ||
using Content.Shared.CCVar; | ||
using Robust.Shared.Configuration; | ||
using JetBrains.Annotations; | ||
|
||
namespace Content.Server._NF.Auth; | ||
|
||
public sealed class MiniAuthManager | ||
{ | ||
[Dependency] private readonly IConfigurationManager _cfg = default!; | ||
|
||
private readonly HttpClient _http = new(); | ||
|
||
/// <summary> | ||
/// Frontier function to ping a server and check to see if the given player is currently connected to the given server. | ||
/// Servers using this function must share an admin_api token as defined in their respective server_config.toml | ||
/// </summary> | ||
/// <param name="address">The address of the server to ping.</param> | ||
/// <param name="player">the GUID of the player to check for connection.</param> | ||
/// <returns>True if the response from the server is successful and the player is connected. False in any case of error, timeout, or failure.</returns> | ||
public async Task<bool> IsPlayerConnected(string address, Guid player) | ||
{ | ||
var connected = false; | ||
var statusAddress = "http://" + address + "/admin/info"; | ||
|
||
var cancel = new CancellationToken(); | ||
var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancel); | ||
linkedToken.CancelAfter(TimeSpan.FromSeconds(10)); | ||
|
||
_http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("SS14Token", _cfg.GetCVar(CCVars.AdminApiToken)); | ||
|
||
//We need to do a try catch here because theres essentially no way to guarantee our json response is proper. | ||
//Throughout all of this, we want it to fail to deny, not fail to allow, so if any step of our auth goes wrong, | ||
//people can still connect. | ||
try | ||
{ | ||
var status = await _http.GetFromJsonAsync<InfoResponse>(statusAddress, linkedToken.Token); | ||
|
||
foreach (var connectedPlayer in status!.Players) | ||
{ | ||
if (connectedPlayer.UserId == player) | ||
{ | ||
connected = true; | ||
break; | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
} | ||
return connected; | ||
} | ||
|
||
/// <summary> | ||
/// Record used to send the response for the info endpoint. | ||
/// Frontier - This is a direct copy of ServerAPI.InfoResponse to match the json format. they kept it private so i just copied it | ||
/// </summary> | ||
[UsedImplicitly] | ||
private sealed record InfoResponse | ||
{ | ||
public required int RoundId { get; init; } | ||
public required List<Player> Players { get; init; } | ||
public required List<string> GameRules { get; init; } | ||
public required string? GamePreset { get; init; } | ||
public required MapInfo? Map { get; init; } | ||
public required string? MOTD { get; init; } | ||
public required Dictionary<string, object> PanicBunker { get; init; } | ||
|
||
public sealed class Player | ||
{ | ||
public required Guid UserId { get; init; } | ||
public required string Name { get; init; } | ||
public required bool IsAdmin { get; init; } | ||
public required bool IsDeadminned { get; init; } | ||
} | ||
|
||
public sealed class MapInfo | ||
{ | ||
public required string Id { get; init; } | ||
public required string Name { get; init; } | ||
} | ||
} | ||
} |
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