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)
{
// 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");
}
}
}

View File

@ -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();
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)
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 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");
}
}
}