From ad219b095c0be35ee0ebcc0c7ccdbbda3753392c Mon Sep 17 00:00:00 2001 From: Kyle873 Date: Sun, 26 May 2024 20:17:16 -0400 Subject: [PATCH] More proxy stuff --- Elisa/GameServer.cs | 3 ++- Elisa/Proxy.cs | 40 +++++++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/Elisa/GameServer.cs b/Elisa/GameServer.cs index 2095a3e..d3d7e6a 100644 --- a/Elisa/GameServer.cs +++ b/Elisa/GameServer.cs @@ -28,12 +28,13 @@ public static class GameServer static async Task HandleRequest(HttpContext context) { + // Is this even necessary? if (context.Request.Method == "CONNECT") context.Response.StatusCode = StatusCodes.Status200OK; else { context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; - await context.Response.WriteAsync("Method Not Allowed"); + await context.Response.WriteAsync("Error 405: Method Not Allowed"); } } } diff --git a/Elisa/Proxy.cs b/Elisa/Proxy.cs index 1adfa1c..a2a6987 100644 --- a/Elisa/Proxy.cs +++ b/Elisa/Proxy.cs @@ -7,15 +7,16 @@ namespace Elisa; public static class Proxy { + static int port = 9000; static string serverUrl = "localhost"; public static async Task Start() { Log.Information("Starting Proxy"); - TcpListener listener = new TcpListener(IPAddress.Any, 9000); + TcpListener listener = new TcpListener(IPAddress.Any, port); listener.Start(); - Log.Information($"Proxy: Listening on port 9000"); + Log.Information($"Proxy: Listening on port {port}"); while (true) { @@ -23,25 +24,30 @@ public static class Proxy 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"); + using (TcpClient server = new TcpClient()) + { + await server.ConnectAsync(serverUrl, 80); + Log.Information($"Proxy: Connected to Elisa"); - NetworkStream clientStream = client.GetStream(); - NetworkStream serverStream = server.GetStream(); + using (NetworkStream clientStream = client.GetStream()) + using (NetworkStream serverStream = server.GetStream()) + { + byte[] buffer = new byte[256 * 256]; + int bytesRead; + while ((bytesRead = await clientStream.ReadAsync(buffer, 0, buffer.Length)) > 0) + { + string data = Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim(); + Log.Information($"Received data from client: {data}"); + } - 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); + Task c2sTask = clientStream.CopyToAsync(serverStream); + Task s2cTask = serverStream.CopyToAsync(clientStream); + await Task.WhenAll(c2sTask, s2cTask); + } + } client.Close(); - server.Close(); - Log.Information($"Proxy: Closed client and server connections"); + Log.Information($"Proxy: Closed client connection"); } } }