56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Globalization;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Steamworks;
|
|
|
|
namespace SVSim.EmulatedEntrypoint.Services;
|
|
|
|
public class SteamSessionService : IDisposable
|
|
{
|
|
private readonly ConcurrentDictionary<string, ulong> _validatedSessionTickets;
|
|
|
|
private const int ShadowVerseAppId = 453480;
|
|
|
|
public SteamSessionService()
|
|
{
|
|
_validatedSessionTickets = new ConcurrentDictionary<string, ulong>();
|
|
SteamServer.Init(ShadowVerseAppId, new SteamServerInit
|
|
{
|
|
GamePort = default,
|
|
QueryPort = default
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates if a given session ticket is valid, and matches up with the given steamid.
|
|
/// </summary>
|
|
/// <param name="ticket">the ticket, represented as a hexadecimal string</param>
|
|
/// <param name="steamId">the steamid that should be associated with the ticket</param>
|
|
/// <returns>whether the ticket is valid for the given steamid</returns>
|
|
public bool IsTicketValidForUser(string ticket, ulong steamId)
|
|
{
|
|
if (_validatedSessionTickets.TryGetValue(ticket, out ulong storedSteamId))
|
|
{
|
|
return storedSteamId == steamId;
|
|
}
|
|
|
|
List<byte> ticketBytes = new List<byte>();
|
|
for (int i = 0; i < ticket.Length; i += 2)
|
|
{
|
|
ticketBytes.Add(Convert.ToByte(ticket.Substring(i, 2), 16));
|
|
}
|
|
|
|
var steamCheckResults = SteamServer.BeginAuthSession(ticketBytes.ToArray(), new SteamId { Value = steamId });
|
|
if (steamCheckResults)
|
|
{
|
|
_validatedSessionTickets.TryAdd(ticket, storedSteamId);
|
|
}
|
|
|
|
return steamCheckResults;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
SteamServer.Shutdown();
|
|
}
|
|
} |