This commit is contained in:
gamer147
2024-09-05 08:32:54 -04:00
parent 8d62c9f238
commit ee7e276036
45 changed files with 1506 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
using System.Buffers.Text;
using System.Text;
using MessagePack;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SVSim.EmulatedEntrypoint.Models.Dtos;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses;
namespace SVSim.EmulatedEntrypoint.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CheckController : SVSimController
{
private ILogger _logger;
public CheckController(ILogger<CheckController> logger)
{
_logger = logger;
}
[HttpPost("special_title")]
public async Task<DataWrapper<SpecialTitleCheckResponse>> SpecialTitleCheck(SpecialTitleCheckRequest request)
{
int titleId = Random.Shared.Next(8, 33);
var res = new DataWrapper<SpecialTitleCheckResponse>
{
Data = new SpecialTitleCheckResponse
{
TitleImageId = titleId,
TitleSoundId = titleId
},
DataHeaders = new DataHeaders
{
ShortUdid = 411054851,
ViewerId = 906243102,
Sid = string.Empty,
Servertime = DateTime.UtcNow.Ticks,
ResultCode = 1
}
};
return res;
}
[HttpPost("game_start")]
public async Task<DataWrapper<GameStartResponse>> GameStart(GameStartRequest request)
{
return new DataWrapper<GameStartResponse>()
{
DataHeaders = new DataHeaders
{
ShortUdid = 411054851,
ViewerId = 906243102,
Sid = string.Empty,
Servertime = DateTime.UtcNow.Ticks,
ResultCode = 1
},
Data = new GameStartResponse()
{
IsSetTransitionPassword = true,
KorAuthorityId = default,
KorAuthorityState = default,
NowRank = new Dictionary<string, string>()
{
{"1", "RankName_010"},
{"2", "RankName_010"},
{"4", "RankName_017"}
},
NowName = "combusty7",
PolicyState = default,
PolicyId = default,
NowTutorialStep = "100",
NowViewerId = 906243102,
TosId = default,
TosState = default,
TransitionAccountData = new List<TransitionAccountData>()
{
new TransitionAccountData()
{
ConnectedViewerId = "906243102",
SocialAccountType = "5",
SocialAccountId = "76561197970830305"
}
}
}
};
}
}
}

View File

@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SVSim.EmulatedEntrypoint.Security;
namespace SVSim.EmulatedEntrypoint.Controllers
{
/// <summary>
/// A base controller for SVSim with helpers for getting some values.
/// </summary>
[Route("api/[controller]")]
[ApiController]
public abstract class SVSimController : ControllerBase
{
/// <summary>
/// Returns the UdId of the user making the request. Can be null or empty, as only certain requests will send it. Known requests to send this value are: SignUp, CheckSpecialTitle, CheckiCloudUser, MigrateiCloudUser
/// </summary>
public string? UdId => Encryption.Decode(Request.Headers["UDID"]);
}
}

View File

@@ -0,0 +1,65 @@
using System.Text;
using MessagePack;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using SVSim.EmulatedEntrypoint.Security;
namespace SVSim.EmulatedEntrypoint.Middlewares;
/// <summary>
/// Translates incoming requests and outgoing responses from the Shadowverse client into the messagepack format.
/// </summary>
public class ShadowverseTranslationMiddleware : IMiddleware
{
private readonly IActionDescriptorCollectionProvider _actionDescriptorCollectionProvider;
public ShadowverseTranslationMiddleware(IActionDescriptorCollectionProvider actionDescriptorCollectionProvider)
{
_actionDescriptorCollectionProvider = actionDescriptorCollectionProvider;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
bool isUnity = context.Request.Headers.UserAgent.Any(agent => agent?.Contains("UnityPlayer") ?? false);
string path = context.Request.Path;
var endpointDescriptor =
_actionDescriptorCollectionProvider.ActionDescriptors.Items.FirstOrDefault(ad =>
$"/{ad.AttributeRouteInfo.Template}".Equals(path, StringComparison.InvariantCultureIgnoreCase));
if (!isUnity || endpointDescriptor == null)
{
await next.Invoke(context);
return;
}
using var requestBytesStream = new MemoryStream();
using var tempResponseBody = new MemoryStream();
var originalResponsebody = context.Response.Body;
context.Response.Body = tempResponseBody;
await context.Request.Body.CopyToAsync(requestBytesStream);
byte[] requestBytes = requestBytesStream.ToArray();
// Decrypt incoming data. Placeholder.
requestBytes = Encryption.Decrypt(requestBytes, Encryption.Decode(context.Request.Headers["UDID"]));
object? data = MessagePackSerializer.Deserialize(endpointDescriptor.Parameters.FirstOrDefault().ParameterType,
requestBytes);
var json = JsonConvert.SerializeObject(data);
var newStream = new StringContent(json, Encoding.UTF8, "application/json");
context.Request.Body = newStream.ReadAsStream();
context.Request.Headers.ContentType = new StringValues("application/json");
await next.Invoke(context);
var responseType = ((ControllerActionDescriptor)endpointDescriptor).MethodInfo.ReturnType;
if (responseType.IsGenericType && responseType.GetGenericTypeDefinition() == typeof(Task<>))
{
responseType = responseType.GetGenericArguments()[0];
}
using var responseBytesStream = new MemoryStream();
context.Response.Body.Seek(0, SeekOrigin.Begin);
await context.Response.Body.CopyToAsync(responseBytesStream);
var responseBytes = responseBytesStream.ToArray();
var responseData = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(responseBytes), responseType);
var packedData = MessagePackSerializer.Serialize(responseType, responseData);
packedData = Encryption.Encrypt(packedData, Encryption.Decode(context.Request.Headers["UDID"]));
await originalResponsebody.WriteAsync(Encoding.UTF8.GetBytes(Convert.ToBase64String(packedData)));
context.Response.Body = originalResponsebody;
}
}

View File

@@ -0,0 +1,18 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos;
[MessagePackObject]
public class DataHeaders
{
[Key("short_udid")]
public int ShortUdid { get; set; }
[Key("viewer_id")]
public int ViewerId { get; set; }
[Key("sid")]
public string Sid { get; set; }
[Key("servertime")]
public long Servertime { get; set; }
[Key("result_code")]
public int ResultCode { get; set; }
}

View File

@@ -0,0 +1,12 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos;
[MessagePackObject]
public class DataWrapper<T>
{
[Key("data_headers")]
public DataHeaders DataHeaders { get; set; }
[Key("data")]
public T Data { get; set; }
}

View File

@@ -0,0 +1,14 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
[MessagePackObject]
public abstract class BaseRequest
{
[Key("viewer_id")]
public string ViewerId { get; set; }
[Key("steam_id")]
public long SteamId { get; set; }
[Key("steam_session_ticket")]
public string SteamSessionTicket { get; set; }
}

View File

@@ -0,0 +1,16 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
[MessagePackObject]
public class GameStartRequest : BaseRequest
{
[Key("app_type")]
public int AppType { get; set; }
[Key("campaign_data")]
public string CampaignData { get; set; }
[Key("campaign_sign")]
public string CampaignSign { get; set; }
[Key("campaign_user")]
public int CampaignUser { get; set; }
}

View File

@@ -0,0 +1,9 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
[MessagePackObject]
public class SpecialTitleCheckRequest : BaseRequest
{
}

View File

@@ -0,0 +1,32 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses;
[MessagePackObject]
public class GameStartResponse
{
[Key("now_viewer_id")]
public long NowViewerId { get; set; }
[Key("is_set_transition_password")]
public bool IsSetTransitionPassword { get; set; }
[Key("now_name")]
public string NowName { get; set; }
[Key("now_rank")]
public Dictionary<string, string> NowRank { get; set; }
[Key("now_tutorial_step")]
public string NowTutorialStep { get; set; }
[Key("transition_account_data")]
public List<TransitionAccountData> TransitionAccountData { get; set; }
[Key("tos_state")]
public int TosState { get; set; }
[Key("tos_id")]
public int TosId { get; set; }
[Key("policy_state")]
public int PolicyState { get; set; }
[Key("policy_id")]
public int PolicyId { get; set; }
[Key("kor_authority_id")]
public int KorAuthorityId { get; set; }
[Key("kor_authority_state")]
public int KorAuthorityState { get; set; }
}

View File

@@ -0,0 +1,12 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses;
[MessagePackObject]
public class SpecialTitleCheckResponse
{
[Key("title_image_id")]
public int TitleImageId { get; set; }
[Key("title_sound_id")]
public int TitleSoundId { get; set; }
}

View File

@@ -0,0 +1,14 @@
using MessagePack;
namespace SVSim.EmulatedEntrypoint.Models.Dtos;
[MessagePackObject]
public class TransitionAccountData
{
[Key("social_account_id")]
public string SocialAccountId { get; set; }
[Key("social_account_type")]
public string SocialAccountType { get; set; }
[Key("connected_viewer_id")]
public string ConnectedViewerId { get; set; }
}

View File

@@ -0,0 +1,59 @@
using System.Reflection;
using DCGEngine.Database.Configuration;
using Microsoft.EntityFrameworkCore;
using SVSim.Database;
using SVSim.Database.Models;
using SVSim.EmulatedEntrypoint.Middlewares;
namespace SVSim.EmulatedEntrypoint;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpLogging(opt =>
{
});
builder.Services.AddDbContext<SVSimDbContext>(opt =>
{
opt.UseSqlite();
});
builder.Services.AddTransient<ShadowverseTranslationMiddleware>();
builder.Services.Configure<DCGEDatabaseConfiguration>(opt =>
{
opt.DbSetSearchAssemblies = new List<Assembly> { Assembly.GetAssembly(typeof(SVSimDbContext)) };
});
var app = builder.Build();
app.UseHttpLogging();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseMiddleware<ShadowverseTranslationMiddleware>();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}

View File

@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:11677",
"sslPort": 44324
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5148",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7267;http://localhost:5148",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Facepunch.Steamworks" Version="2.3.3" />
<PackageReference Include="MessagePack" Version="2.5.172" />
<PackageReference Include="MessagePackAnalyzer" Version="2.5.172">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Configuration\" />
<Folder Include="Controllers\" />
<Folder Include="Utility\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SVSim.Database\SVSim.Database.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,6 @@
@SVSim.EmulatedEntrypoint_HostAddress = http://localhost:5148
GET {{SVSim.EmulatedEntrypoint_HostAddress}}/weatherforecast/
Accept: application/json
###

View File

@@ -0,0 +1,152 @@
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
namespace SVSim.EmulatedEntrypoint.Security;
/// <summary>
/// Helper class for encrypting/decrypting requests bodies and responses to/from the game client.
/// </summary>
public static class Encryption
{
private const int EncryptionKeySize = 256;
private const int EncryptionBlockSize = 128;
private const CipherMode EncryptionMode = CipherMode.CBC;
private const int UdIdKeySize = 16;
private const int KeyStringSize = 32;
private const int EncodingValueOffset = 10;
/// <summary>
/// Encrypts an array of bytes using RJ256 with a subset of the user's UdId as the key.
/// </summary>
/// <param name="sourceData">the data to encrypt</param>
/// <param name="udId">the UdId of the user this data is encrypted for</param>
/// <returns>the encrypted bytes</returns>
public static byte[] Encrypt(byte[] sourceData, string udId)
{
using (var rj = Aes.Create())
{
rj.KeySize = EncryptionKeySize;
rj.Mode = EncryptionMode;
rj.BlockSize = EncryptionBlockSize;
string keyString = GenerateKeyString();
string udIdKey = udId.Replace("-", string.Empty).Substring(0, UdIdKeySize);
byte[] keyStringBytes = Encoding.UTF8.GetBytes(keyString);
byte[] rgbIV = Encoding.UTF8.GetBytes(udIdKey);
ICryptoTransform transform = rj.CreateEncryptor(keyStringBytes, rgbIV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
{
cs.Write(sourceData);
cs.FlushFinalBlock();
byte[] encryptedResults = ms.ToArray();
byte[] encryptedResultsAndKey = new byte[encryptedResults.Length + keyStringBytes.Length];
Array.Copy(encryptedResults, 0, encryptedResultsAndKey, 0, encryptedResults.Length);
Array.Copy(keyStringBytes, 0, encryptedResultsAndKey, encryptedResults.Length, keyStringBytes.Length);
return encryptedResultsAndKey;
}
}
}
}
/// <summary>
/// Decrypts data that has been encrypted with the given UdId.
/// </summary>
/// <param name="encryptedData">Previously encrypted data</param>
/// <param name="udId">The UdId previously used to encrypt the data</param>
/// <returns>the decrypted bytes</returns>
public static byte[] Decrypt(byte[] encryptedData, string udId)
{
using (var rj = Aes.Create())
{
rj.KeySize = EncryptionKeySize;
rj.Mode = EncryptionMode;
rj.BlockSize = EncryptionBlockSize;
byte[] rgbIv = Encoding.UTF8.GetBytes(udId.Replace("-", string.Empty).Substring(0, UdIdKeySize));
byte[] keyBytes = new byte[KeyStringSize];
byte[] encryptedValueBytes = new byte[encryptedData.Length - KeyStringSize];
Array.Copy(encryptedData, encryptedData.Length - keyBytes.Length, keyBytes, 0, keyBytes.Length);
Array.Copy(encryptedData, 0, encryptedValueBytes, 0, encryptedValueBytes.Length);
ICryptoTransform transform = rj.CreateDecryptor(keyBytes, rgbIv);
using (MemoryStream ms = new MemoryStream(encryptedValueBytes))
{
using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read))
{
byte[] decryptedValueBytes = new byte[encryptedValueBytes.Length];
cs.Read(decryptedValueBytes, 0, encryptedValueBytes.Length);
cs.FlushFinalBlock();
return decryptedValueBytes;
}
}
}
}
public static string Encode(string sourceData)
{
int length = sourceData.Length;
string encodeBuf = $"{length:x4}";
foreach (char value in sourceData)
{
encodeBuf += $"{GetRandom(),1:x}";
encodeBuf += $"{GetRandom(),1:x}";
encodeBuf += ((char)(Convert.ToInt32(value) + EncodingValueOffset)).ToString();
encodeBuf += $"{GetRandom(),1:x}";
}
encodeBuf += GenerateIvString();
return encodeBuf;
}
public static string? Decode(string? encodedData)
{
if (encodedData == null || encodedData.Length < 4)
{
return encodedData;
}
int num = int.Parse(encodedData.Substring(0, 4), NumberStyles.AllowHexSpecifier);
string text = "";
int num2 = 2;
foreach (char value in encodedData.Substring(4, encodedData.Length - 4))
{
if (num2 % 4 == 0)
{
text += ((char)(Convert.ToInt32(value) - EncodingValueOffset)).ToString();
}
num2++;
if (text.Length >= num)
{
break;
}
}
return text;
}
// TODO Clean this up and de-magic number it
private static string GenerateIvString()
{
string text = "";
for (int i = 0; i < KeyStringSize; i++)
{
text += $"{GetRandom()}";
}
return text;
}
private static string GenerateKeyString()
{
string text = "";
for (int i = 0; i < KeyStringSize; i++)
{
text += $"{Random.Shared.Next(0, ushort.MaxValue):x}";
}
return Convert.ToBase64String(Encoding.ASCII.GetBytes(text)).Substring(0, KeyStringSize);
}
private static int GetRandom()
{
const int MinRandomValue = 1;
const int MaxRandomValue = 9;
return Random.Shared.Next(MinRandomValue, MaxRandomValue);
}
}

View File

@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.Authentication;
namespace SVSim.EmulatedEntrypoint.Security.SteamSessionAuthentication;
public class SteamAuthenticationHandlerOptions : AuthenticationSchemeOptions
{
}

View File

@@ -0,0 +1,21 @@
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
namespace SVSim.EmulatedEntrypoint.Security.SteamSessionAuthentication;
public class SteamSessionAuthenticationHandler : AuthenticationHandler<SteamAuthenticationHandlerOptions>
{
public SteamSessionAuthenticationHandler(IOptionsMonitor<SteamAuthenticationHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
public SteamSessionAuthenticationHandler(IOptionsMonitor<SteamAuthenticationHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder)
{
}
protected async override Task<AuthenticateResult> HandleAuthenticateAsync()
{
return AuthenticateResult.Fail("Not implemented");
}
}

View File

@@ -0,0 +1,56 @@
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();
}
}

View File

@@ -0,0 +1,12 @@
namespace SVSim.EmulatedEntrypoint;
public class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},
"AllowedHosts": "*"
}

Binary file not shown.

Binary file not shown.