From bc16f0c421e64f6a8b5dacc603b161d6a710ffa5 Mon Sep 17 00:00:00 2001 From: Kyle873 Date: Sun, 26 May 2024 07:23:26 -0400 Subject: [PATCH] Proxy server that kinda works maybe sorta???? --- Elisa/Controllers/IndexController.cs | 2 +- Elisa/GameServer.cs | 20 ++++++++++-- Elisa/Program.cs | 1 + Elisa/Proxy.cs | 47 ++++++++++++++++++++++++++++ Elisa/Utils/Time.cs | 14 ++++----- 5 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 Elisa/Proxy.cs diff --git a/Elisa/Controllers/IndexController.cs b/Elisa/Controllers/IndexController.cs index 726f3e6..dfcc1c0 100644 --- a/Elisa/Controllers/IndexController.cs +++ b/Elisa/Controllers/IndexController.cs @@ -9,7 +9,7 @@ namespace Elisa.Controllers; public class IndexController : ControllerBase { [HttpGet("version")] - public async Task Version([FromQuery] string req_idreturn) + public async Task Version([FromQuery] string req_id) { return Results.Json(new { diff --git a/Elisa/GameServer.cs b/Elisa/GameServer.cs index fbe50eb..2095a3e 100644 --- a/Elisa/GameServer.cs +++ b/Elisa/GameServer.cs @@ -1,23 +1,39 @@ using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Serilog; namespace Elisa; -public class GameServer +public static class GameServer { public static async Task Start() { WebApplicationBuilder builder = WebApplication.CreateBuilder(Environment.GetCommandLineArgs()); builder.Host.UseSerilog(); - builder.Services.Configure(op => op.AllowSynchronousIO = true); + builder.Services.Configure(options => + { + options.AllowSynchronousIO = true; + }); builder.Services.AddControllers(); WebApplication app = builder.Build(); app.UseAuthorization(); app.UseSerilogRequestLogging(); + app.Map("/", HandleRequest); app.MapControllers(); app.Run(); } + + static async Task HandleRequest(HttpContext context) + { + if (context.Request.Method == "CONNECT") + context.Response.StatusCode = StatusCodes.Status200OK; + else + { + context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; + await context.Response.WriteAsync("Method Not Allowed"); + } + } } diff --git a/Elisa/Program.cs b/Elisa/Program.cs index e35b61d..7c5f97b 100644 --- a/Elisa/Program.cs +++ b/Elisa/Program.cs @@ -36,6 +36,7 @@ internal static class Program stopwatch.Stop(); Log.Information($"Done! Loaded in {stopwatch.ElapsedMilliseconds}ms"); + Task.Run(Proxy.Start); Task.Run(GameServer.Start).Wait(); } diff --git a/Elisa/Proxy.cs b/Elisa/Proxy.cs new file mode 100644 index 0000000..1adfa1c --- /dev/null +++ b/Elisa/Proxy.cs @@ -0,0 +1,47 @@ +using Serilog; +using System.Net; +using System.Net.Sockets; +using System.Text; + +namespace Elisa; + +public static class Proxy +{ + static string serverUrl = "localhost"; + + public static async Task Start() + { + Log.Information("Starting Proxy"); + + TcpListener listener = new TcpListener(IPAddress.Any, 9000); + listener.Start(); + Log.Information($"Proxy: Listening on port 9000"); + + while (true) + { + TcpClient client = await listener.AcceptTcpClientAsync(); + string clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); + Log.Information($"Proxy: Accepted client connection from {clientAddress}"); + + TcpClient server = new TcpClient(); + await server.ConnectAsync(serverUrl, 80); + Log.Information($"Proxy: Connected to Elisa"); + + NetworkStream clientStream = client.GetStream(); + NetworkStream serverStream = server.GetStream(); + + byte[] buffer = new byte[256 * 256]; + int bytesRead; + while ((bytesRead = await clientStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + Log.Information($"Received data from client: {Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim()}"); + + Task clientToServerTask = clientStream.CopyToAsync(serverStream); + Task serverToClientTask = serverStream.CopyToAsync(clientStream); + await Task.WhenAll(clientToServerTask, serverToClientTask); + + client.Close(); + server.Close(); + Log.Information($"Proxy: Closed client and server connections"); + } + } +} diff --git a/Elisa/Utils/Time.cs b/Elisa/Utils/Time.cs index 88697ec..56c905e 100644 --- a/Elisa/Utils/Time.cs +++ b/Elisa/Utils/Time.cs @@ -1,11 +1,5 @@ namespace Elisa.Utils; -internal static class DateTimeExtensions -{ - public static long ToUnixSeconds(this DateTime dateTime) - => (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; -} - public static class Time { public static long GetTomorrowZero() @@ -16,4 +10,10 @@ public static class Time public static long GetNextMonthZero() => new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1).ToUnixSeconds(); -} \ No newline at end of file +} + +internal static class DateTimeExtensions +{ + public static long ToUnixSeconds(this DateTime dateTime) + => (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; +}