mirror of
https://github.com/MeowCraftMC/CrystalConnector
synced 2025-05-22 02:43:00 +00:00
@@ -8,19 +8,19 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-preview.7.23375.6"/>
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-preview.7.23375.6" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0-preview.7.23375.6"/>
|
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0-preview.7.23375.6" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0-preview.7.23375.6"/>
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0-preview.7.23375.6" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.7.23375.6"/>
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-preview.7.23375.6" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.7.23375.6"/>
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0-preview.7.23375.6" />
|
||||||
<PackageReference Include="NLog" Version="5.2.4"/>
|
<PackageReference Include="NLog" Version="5.2.4" />
|
||||||
<PackageReference Include="NLog.Extensions.Hosting" Version="5.3.4"/>
|
<PackageReference Include="NLog.Extensions.Hosting" Version="5.3.4" />
|
||||||
<PackageReference Include="Spectre.Console" Version="0.47.1-preview.0.12"/>
|
<PackageReference Include="Spectre.Console" Version="0.47.1-preview.0.12" />
|
||||||
<PackageReference Include="Telegram.Bot" Version="19.0.0"/>
|
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
|
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -32,9 +32,4 @@
|
|||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Web\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Net.WebSockets;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
|
||||||
|
namespace CrystalConnector.Handlers;
|
||||||
|
|
||||||
|
public interface IWebSocketHandler
|
||||||
|
{
|
||||||
|
public void StartHandle(HttpContext context, WebSocket socket, TaskCompletionSource<WebSocketHandleResult> taskCompletionSource);
|
||||||
|
|
||||||
|
public void Disconnect(WebSocket socket);
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace CrystalConnector.Handlers;
|
||||||
|
|
||||||
|
public enum WebSocketHandleResult
|
||||||
|
{
|
||||||
|
Successful = 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace CrystalConnector.Handlers;
|
||||||
|
|
||||||
|
public class WebSocketHandler : IWebSocketHandler
|
||||||
|
{
|
||||||
|
private ILogger<WebSocketHandler> Logger { get; }
|
||||||
|
|
||||||
|
// Todo: qyl27: Maybe we need a MultiKeyMap? <string Id, IPEndPoint ip, Tuple<WebSocket, TaskCompletionSource<WebSocketHandleResult>>>
|
||||||
|
private Dictionary<IPEndPoint, Tuple<WebSocket, TaskCompletionSource<WebSocketHandleResult>>> WebSockets { get; } = new();
|
||||||
|
|
||||||
|
public WebSocketHandler(ILogger<WebSocketHandler> logger)
|
||||||
|
{
|
||||||
|
Logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StartHandle(HttpContext context, WebSocket socket, TaskCompletionSource<WebSocketHandleResult> taskCompletionSource)
|
||||||
|
{
|
||||||
|
var remoteEndPoint = new IPEndPoint(context.Connection.RemoteIpAddress, context.Connection.RemotePort);
|
||||||
|
WebSockets.Add(remoteEndPoint, new Tuple<WebSocket, TaskCompletionSource<WebSocketHandleResult>>(socket, taskCompletionSource));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Disconnect(WebSocket socket)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using CrystalConnector;
|
using CrystalConnector;
|
||||||
|
using CrystalConnector.Handlers;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -14,6 +15,8 @@ var builder = Host.CreateDefaultBuilder(args);
|
|||||||
builder.ConfigureServices(services =>
|
builder.ConfigureServices(services =>
|
||||||
{
|
{
|
||||||
services.AddHostedService<CrystalService>();
|
services.AddHostedService<CrystalService>();
|
||||||
|
|
||||||
|
ConfigureServices(services);
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.ConfigureAppConfiguration((context, config) =>
|
builder.ConfigureAppConfiguration((context, config) =>
|
||||||
@@ -27,15 +30,13 @@ builder.ConfigureAppConfiguration((context, config) =>
|
|||||||
|
|
||||||
builder.UseContentRoot(Environment.CurrentDirectory);
|
builder.UseContentRoot(Environment.CurrentDirectory);
|
||||||
|
|
||||||
builder.ConfigureLogging(logging =>
|
builder.ConfigureLogging(ConfigureLogging);
|
||||||
{
|
|
||||||
logging.ClearProviders()
|
|
||||||
.SetMinimumLevel(LogLevel.Trace)
|
|
||||||
.AddNLog();
|
|
||||||
});
|
|
||||||
|
|
||||||
builder.ConfigureWebHost(webHost =>
|
builder.ConfigureWebHost(webHost =>
|
||||||
{
|
{
|
||||||
|
webHost.ConfigureServices(ConfigureServices);
|
||||||
|
webHost.ConfigureLogging(ConfigureLogging);
|
||||||
|
|
||||||
webHost.UseStartup<Startup>();
|
webHost.UseStartup<Startup>();
|
||||||
|
|
||||||
webHost.UseKestrel();
|
webHost.UseKestrel();
|
||||||
@@ -81,3 +82,15 @@ builder.ConfigureWebHost(webHost =>
|
|||||||
using var host = builder.Build();
|
using var host = builder.Build();
|
||||||
|
|
||||||
await host.RunAsync();
|
await host.RunAsync();
|
||||||
|
|
||||||
|
void ConfigureLogging(ILoggingBuilder logging)
|
||||||
|
{
|
||||||
|
logging.ClearProviders()
|
||||||
|
.SetMinimumLevel(LogLevel.Trace)
|
||||||
|
.AddNLog();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<IWebSocketHandler, WebSocketHandler>();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using CrystalConnector.Handlers;
|
||||||
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using NLog;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace CrystalConnector;
|
namespace CrystalConnector;
|
||||||
|
|
||||||
@@ -9,7 +10,8 @@ public class Startup
|
|||||||
{
|
{
|
||||||
private IConfiguration Config { get; }
|
private IConfiguration Config { get; }
|
||||||
|
|
||||||
private Logger Logger { get; } = LogManager.GetCurrentClassLogger();
|
private ILogger<Startup> Logger { get; set; }
|
||||||
|
private IWebSocketHandler WebSocketHandler { get; set; }
|
||||||
|
|
||||||
public Startup(IConfiguration config)
|
public Startup(IConfiguration config)
|
||||||
{
|
{
|
||||||
@@ -18,6 +20,10 @@ public class Startup
|
|||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
var provider = services.BuildServiceProvider();
|
||||||
|
|
||||||
|
Logger = provider.GetService<ILogger<Startup>>()!;
|
||||||
|
WebSocketHandler = provider.GetService<IWebSocketHandler>()!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app)
|
public void Configure(IApplicationBuilder app)
|
||||||
@@ -28,11 +34,17 @@ public class Startup
|
|||||||
{
|
{
|
||||||
if (context.WebSockets.IsWebSocketRequest)
|
if (context.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
await context.WebSockets.AcceptWebSocketAsync();
|
var websocket = await context.WebSockets.AcceptWebSocketAsync();
|
||||||
Logger.Info("Got WebSocket connection from {Ip}:{Port}", context.Connection.RemoteIpAddress?.ToString(), context.Connection.RemotePort);
|
Logger.LogInformation("Got WebSocket connection from {Ip}:{Port}", context.Connection.RemoteIpAddress?.ToString(), context.Connection.RemotePort);
|
||||||
}
|
|
||||||
|
|
||||||
|
var taskCompletionSource = new TaskCompletionSource<WebSocketHandleResult>();
|
||||||
|
WebSocketHandler.StartHandle(context, websocket, taskCompletionSource);
|
||||||
|
await taskCompletionSource.Task;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
await next(context);
|
await next(context);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user