Proxy server that kinda works maybe sorta????

This commit is contained in:
Kyle873 2024-05-26 07:23:26 -04:00
parent 431fdb6f9e
commit bc16f0c421
5 changed files with 74 additions and 10 deletions

View File

@ -9,7 +9,7 @@ namespace Elisa.Controllers;
public class IndexController : ControllerBase
{
[HttpGet("version")]
public async Task<IResult> Version([FromQuery] string req_idreturn)
public async Task<IResult> Version([FromQuery] string req_id)
{
return Results.Json(new
{

View File

@ -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<KestrelServerOptions>(op => op.AllowSynchronousIO = true);
builder.Services.Configure<KestrelServerOptions>(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");
}
}
}

View File

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

47
Elisa/Proxy.cs Normal file
View File

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

View File

@ -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();
}
}
internal static class DateTimeExtensions
{
public static long ToUnixSeconds(this DateTime dateTime)
=> (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}