Compare commits

..

2 Commits

Author SHA1 Message Date
Kyle873 ad219b095c More proxy stuff 2024-05-26 20:17:16 -04:00
Kyle873 c6a5683fbc Keep counts on the tables loaded 2024-05-26 20:11:10 -04:00
3 changed files with 33 additions and 19 deletions

View File

@ -86,6 +86,10 @@ public static class Table
public static void Load() public static void Load()
{ {
int catchDataCount = 0;
int stcCount = 0;
int textCount = 0;
foreach (PropertyInfo? prop in typeof(Table).GetProperties().Where(x => x.GetCustomAttribute<LoadDataAttribute>() is not null)) foreach (PropertyInfo? prop in typeof(Table).GetProperties().Where(x => x.GetCustomAttribute<LoadDataAttribute>() is not null))
{ {
LoadDataAttribute attr = prop.GetCustomAttribute<LoadDataAttribute>()!; LoadDataAttribute attr = prop.GetCustomAttribute<LoadDataAttribute>()!;
@ -98,18 +102,21 @@ public static class Table
object? value = JsonSerializer.Deserialize(entry.Value.GetRawText(), prop.PropertyType, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); object? value = JsonSerializer.Deserialize(entry.Value.GetRawText(), prop.PropertyType, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
prop.SetValue(null, value); prop.SetValue(null, value);
Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} catchdata table"); Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} catchdata table");
catchDataCount++;
break; break;
case LoadDataType.STC: case LoadDataType.STC:
prop.SetValue(null, typeof(JSON).GetMethod(nameof(JSON.Load))!.MakeGenericMethod(prop.PropertyType).Invoke(null, [Path.Combine(GetPath(attr.DataType), attr.FileName), false])); prop.SetValue(null, typeof(JSON).GetMethod(nameof(JSON.Load))!.MakeGenericMethod(prop.PropertyType).Invoke(null, [Path.Combine(GetPath(attr.DataType), attr.FileName), false]));
Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} stc table"); Log.Information($"Loaded {(prop.GetValue(null) as IList)?.Count} entries from {prop.Name} stc table");
stcCount++;
break; break;
case LoadDataType.Text: case LoadDataType.Text:
// TODO: textdata is just a flat list of key-value pairs that are looked up from stc // TODO: textdata is just a flat list of key-value pairs that are looked up from stc
textCount++;
break; break;
} }
} }
Log.Information("All data tables loaded"); Log.Information($"Finished loading data tables ({catchDataCount} catchdata, {stcCount} stc, {textCount} text)");
} }
public static string GetPath(LoadDataType type) public static string GetPath(LoadDataType type)

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); {
Log.Information($"Proxy: Connected to Elisa"); await server.ConnectAsync(serverUrl, 80);
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];
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]; Task c2sTask = clientStream.CopyToAsync(serverStream);
int bytesRead; Task s2cTask = serverStream.CopyToAsync(clientStream);
while ((bytesRead = await clientStream.ReadAsync(buffer, 0, buffer.Length)) > 0) await Task.WhenAll(c2sTask, s2cTask);
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(); client.Close();
server.Close(); Log.Information($"Proxy: Closed client connection");
Log.Information($"Proxy: Closed client and server connections");
} }
} }
} }