From 07ebba4e38c0c21669a523bb28219837b84bbef9 Mon Sep 17 00:00:00 2001 From: Adolfo Huitron Date: Wed, 6 Mar 2024 21:12:50 -0800 Subject: [PATCH] refactor: use service that handles intercepted connections --- Tanji.CLI/Program.cs | 47 +++++--------------------------------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/Tanji.CLI/Program.cs b/Tanji.CLI/Program.cs index 9e55896..18cd901 100644 --- a/Tanji.CLI/Program.cs +++ b/Tanji.CLI/Program.cs @@ -1,14 +1,10 @@ -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; @@ -16,8 +12,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; -using CommunityToolkit.HighPerformance.Buffers; - namespace Tanji.CLI; public class Program @@ -55,7 +49,7 @@ static void CleanUp(PosixSignalContext context) private readonly IConnectionHandlerService _connectionHandler; private readonly IClientHandlerService _clientHandler; - public Program(ILogger logger, IWebInterceptionService webInterception, IConnectionHandlerService connectionHandler, IClientHandlerService clientHandler) + public Program(ILogger logger, IWebInterceptionService webInterception, IClientHandlerService clientHandler, IConnectionHandlerService connectionHandler) { _logger = logger; _clientHandler = clientHandler; @@ -67,21 +61,6 @@ public Program(ILogger logger, IWebInterceptionService webInterception, public async Task RunAsync(CancellationToken cancellationToken = default) { - static IPEndPoint? GetRemoteEndPoint(IHFormat packetFormat, ReadOnlySpan packetSpan) - { - var pktReader = new HPacketReader(packetFormat, packetSpan); - string hostNameOrAddress = pktReader.ReadUTF8().Split('\0')[0]; - int port = pktReader.Read(); - - 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 { @@ -89,26 +68,10 @@ public async Task RunAsync(CancellationToken cancellationToken = default) _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(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); }