Skip to content

Commit

Permalink
refactor: use service that handles intercepted connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ArachisH committed Mar 7, 2024
1 parent 8ef3ab7 commit 07ebba4
Showing 1 changed file with 5 additions and 42 deletions.
47 changes: 5 additions & 42 deletions Tanji.CLI/Program.cs
@@ -1,23 +1,17 @@
using System.Net;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;

using Tanji.Core;
using Tanji.Core.Network;
using Tanji.Core.Services;
using Tanji.Core.Habbo.Canvas;
using Tanji.Core.Configuration;
using Tanji.Core.Habbo.Network.Buffers;
using Tanji.Core.Habbo.Network.Formats;

using Eavesdrop;

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

using CommunityToolkit.HighPerformance.Buffers;

namespace Tanji.CLI;

public class Program
Expand Down Expand Up @@ -55,7 +49,7 @@ static void CleanUp(PosixSignalContext context)
private readonly IConnectionHandlerService _connectionHandler;
private readonly IClientHandlerService<CachedGame> _clientHandler;

public Program(ILogger<Program> logger, IWebInterceptionService webInterception, IConnectionHandlerService connectionHandler, IClientHandlerService<CachedGame> clientHandler)
public Program(ILogger<Program> logger, IWebInterceptionService webInterception, IClientHandlerService<CachedGame> clientHandler, IConnectionHandlerService connectionHandler)
{
_logger = logger;
_clientHandler = clientHandler;
Expand All @@ -67,48 +61,17 @@ public Program(ILogger<Program> logger, IWebInterceptionService webInterception,

public async Task RunAsync(CancellationToken cancellationToken = default)
{
static IPEndPoint? GetRemoteEndPoint(IHFormat packetFormat, ReadOnlySpan<byte> packetSpan)
{
var pktReader = new HPacketReader(packetFormat, packetSpan);
string hostNameOrAddress = pktReader.ReadUTF8().Split('\0')[0];
int port = pktReader.Read<int>();

if (!IPAddress.TryParse(hostNameOrAddress, out IPAddress? address))
{
IPAddress[] addresses = Dns.GetHostAddresses(hostNameOrAddress);
if (addresses.Length > 0) address = addresses[0];
}

return address != null ? new IPEndPoint(address, port) : null;
}

_logger.LogInformation("Intercepting Game Token(s)...");
do
{
string ticket = await _webInterception.InterceptTicketAsync(cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Game Ticket: {Ticket}", ticket);

CachedGame game = _clientHandler.PatchClient(HPlatform.Flash, null);
_logger.LogInformation("Client Processed : {game.ClientPath}", game.ClientPath);

var connection = new HConnection();
var connectionOptions = new HConnectionOptions(game, game.AppliedPatches);

ValueTask interceptLocalConnectionTask = connection.InterceptLocalConnectionAsync(connectionOptions, cancellationToken);
_ = _clientHandler.LaunchClient(ticket, HPlatform.Flash, game.ClientPath);

await interceptLocalConnectionTask.ConfigureAwait(false);
while (connection.Local!.IsConnected)
{
using var writer = new ArrayPoolBufferWriter<byte>(64);
int written = await connection.Local.ReceivePacketAsync(writer, cancellationToken).ConfigureAwait(false);

IPEndPoint? remoteEndPoint = GetRemoteEndPoint(connectionOptions.SendPacketFormat, writer.WrittenSpan);
await connection.EstablishRemoteConnection(connectionOptions, remoteEndPoint!, cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Client Processed : {game.ClientPath}", game.Path);

// TODO: Read all data
break;
}
var connectionContext = new HConnectionContext(game);
_ = await _connectionHandler.LaunchAndInterceptConnectionAsync(ticket, connectionContext, cancellationToken).ConfigureAwait(false);
}
while (!cancellationToken.IsCancellationRequested);
}
Expand Down

0 comments on commit 07ebba4

Please sign in to comment.