More proxy stuff

This commit is contained in:
Kyle873 2024-05-26 20:17:16 -04:00
parent c6a5683fbc
commit ad219b095c
2 changed files with 25 additions and 18 deletions

View File

@ -28,12 +28,13 @@ public static class GameServer
static async Task HandleRequest(HttpContext context) static async Task HandleRequest(HttpContext context)
{ {
// Is this even necessary?
if (context.Request.Method == "CONNECT") if (context.Request.Method == "CONNECT")
context.Response.StatusCode = StatusCodes.Status200OK; context.Response.StatusCode = StatusCodes.Status200OK;
else else
{ {
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed; context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
await context.Response.WriteAsync("Method Not Allowed"); await context.Response.WriteAsync("Error 405: Method Not Allowed");
} }
} }
} }

View File

@ -7,15 +7,16 @@ namespace Elisa;
public static class Proxy public static class Proxy
{ {
static int port = 9000;
static string serverUrl = "localhost"; static string serverUrl = "localhost";
public static async Task Start() public static async Task Start()
{ {
Log.Information("Starting Proxy"); Log.Information("Starting Proxy");
TcpListener listener = new TcpListener(IPAddress.Any, 9000); TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start(); listener.Start();
Log.Information($"Proxy: Listening on port 9000"); Log.Information($"Proxy: Listening on port {port}");
while (true) while (true)
{ {
@ -23,25 +24,30 @@ public static class Proxy
string clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); string clientAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString();
Log.Information($"Proxy: Accepted client connection from {clientAddress}"); Log.Information($"Proxy: Accepted client connection from {clientAddress}");
TcpClient server = new TcpClient(); using (TcpClient server = new TcpClient())
{
await server.ConnectAsync(serverUrl, 80); await server.ConnectAsync(serverUrl, 80);
Log.Information($"Proxy: Connected to Elisa"); Log.Information($"Proxy: Connected to Elisa");
NetworkStream clientStream = client.GetStream(); using (NetworkStream clientStream = client.GetStream())
NetworkStream serverStream = server.GetStream(); using (NetworkStream serverStream = server.GetStream())
{
byte[] buffer = new byte[256 * 256]; byte[] buffer = new byte[256 * 256];
int bytesRead; int bytesRead;
while ((bytesRead = await clientStream.ReadAsync(buffer, 0, buffer.Length)) > 0) while ((bytesRead = await clientStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
Log.Information($"Received data from client: {Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim()}"); {
string data = Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim();
Log.Information($"Received data from client: {data}");
}
Task clientToServerTask = clientStream.CopyToAsync(serverStream); Task c2sTask = clientStream.CopyToAsync(serverStream);
Task serverToClientTask = serverStream.CopyToAsync(clientStream); Task s2cTask = serverStream.CopyToAsync(clientStream);
await Task.WhenAll(clientToServerTask, serverToClientTask); await Task.WhenAll(c2sTask, s2cTask);
}
}
client.Close(); client.Close();
server.Close(); Log.Information($"Proxy: Closed client connection");
Log.Information($"Proxy: Closed client and server connections");
} }
} }
} }