From 8887cf1e4b8bd9546f2d47dd216fd874bfa19a3e Mon Sep 17 00:00:00 2001 From: xiaojiuwo Date: Fri, 3 Nov 2023 10:45:33 +0800 Subject: [PATCH] fix(lib): network error --- .../src/Network/Http/Server/HttpConnection.cs | 4 ++++ .../Http/Server/HttpConnectionManager.cs | 24 +++++++++---------- .../src/Network/Tcp/Server/TcpConnection.cs | 2 +- .../Tcp/Server/TcpConnectionManager.cs | 3 --- .../src/Network/Udp/Server/UdpConnection.cs | 1 + .../Udp/Server/UdpConnectionManager.cs | 17 ++++++++++--- src/Servers/WebServer/test/Auth/RSATest.cs | 20 ++++++++++++++++ 7 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/Servers/WebServer/test/Auth/RSATest.cs diff --git a/src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs b/src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs index cb56307a3..6cb69c90f 100644 --- a/src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs +++ b/src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs @@ -23,6 +23,10 @@ public HttpConnection(HttpListenerContext context, IConnectionManager manager) public void OnReceived(IHttpRequest request) { OnReceive(request); + if (Context.Request.Headers["Connection"] == "close") + { + Context.Response.Close(); + } } public void Send(string response) diff --git a/src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs b/src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs index 63f0659e8..738423d01 100644 --- a/src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs +++ b/src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Net; using System.Threading.Tasks; using UniSpy.Server.Core.Abstraction.Interface; @@ -23,19 +24,16 @@ public void Start() { while (true) { - try - { - var context = Listener.GetContext(); - var raw = context.Request; - var request = new HttpRequest(raw); - var conn = new HttpConnection(context, this); - OnInitialization(conn); - conn.OnReceived(request); - } - catch (Exception ex) - { - LogWriter.LogError(ex); - } + var context = Listener.GetContext(); + Task.Run(() => + { + + var conn = new HttpConnection(context, this); + OnInitialization(conn); + var raw = context.Request; + var request = new HttpRequest(raw); + conn.OnReceived(request); + }); } }); } diff --git a/src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs b/src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs index c2139462f..76a741e2d 100644 --- a/src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs +++ b/src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs @@ -31,8 +31,8 @@ public TcpConnection(TcpClient client, IConnectionManager manager) } public void Disconnect() { - OnDisconnected(); Client.Client.Disconnect(true); + OnDisconnected(); } public void Send(string response) => Send(UniSpyEncoding.GetBytes(response)); diff --git a/src/Libraries/Core/src/Network/Tcp/Server/TcpConnectionManager.cs b/src/Libraries/Core/src/Network/Tcp/Server/TcpConnectionManager.cs index f6233cae5..63c032003 100644 --- a/src/Libraries/Core/src/Network/Tcp/Server/TcpConnectionManager.cs +++ b/src/Libraries/Core/src/Network/Tcp/Server/TcpConnectionManager.cs @@ -12,14 +12,11 @@ public class TcpConnectionManager : IConnectionManager, IDisposable { public event OnConnectingEventHandler OnInitialization; public TcpListener Listener; - public ConcurrentDictionary ConnectionPool = new(); - public TcpConnectionManager(IPEndPoint endPoint) { Listener = new TcpListener(endPoint); } - public void Start() { Listener.Start(); diff --git a/src/Libraries/Core/src/Network/Udp/Server/UdpConnection.cs b/src/Libraries/Core/src/Network/Udp/Server/UdpConnection.cs index 75c863e50..59b924988 100644 --- a/src/Libraries/Core/src/Network/Udp/Server/UdpConnection.cs +++ b/src/Libraries/Core/src/Network/Udp/Server/UdpConnection.cs @@ -14,6 +14,7 @@ public class UdpConnection : IUdpConnection public NetworkConnectionType ConnectionType { get; } = NetworkConnectionType.Udp; public event OnReceivedEventHandler OnReceive; + public UdpConnection(IPEndPoint endPoint, IConnectionManager manager) { RemoteIPEndPoint = endPoint; diff --git a/src/Libraries/Core/src/Network/Udp/Server/UdpConnectionManager.cs b/src/Libraries/Core/src/Network/Udp/Server/UdpConnectionManager.cs index 215a7456d..aaa23b246 100644 --- a/src/Libraries/Core/src/Network/Udp/Server/UdpConnectionManager.cs +++ b/src/Libraries/Core/src/Network/Udp/Server/UdpConnectionManager.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System; using System.Net; using System.Net.Sockets; @@ -11,6 +12,7 @@ public class UdpConnectionManager : IConnectionManager, IDisposable { public event OnConnectingEventHandler OnInitialization; public UdpClient Listener { get; private set; } + public ConcurrentDictionary Pool = new(); public UdpConnectionManager(IPEndPoint endPoint) { Listener = new UdpClient(endPoint); @@ -24,9 +26,18 @@ public void Start() { var clientEndPoint = new IPEndPoint(IPAddress.Any, 0); var data = Listener.Receive(ref clientEndPoint); - var conn = new UdpConnection(clientEndPoint, this); - // OnInitialization?.Invoke(conn); - Task.Run(() => conn.OnReceived(data)); + + Task.Run(() => + { + UdpConnection conn; + if (!Pool.TryGetValue(clientEndPoint, out conn)) + { + conn = new UdpConnection(clientEndPoint, this); + Pool.TryAdd(clientEndPoint, conn); + } + OnInitialization(conn); + conn.OnReceived(data); + }); } }); } diff --git a/src/Servers/WebServer/test/Auth/RSATest.cs b/src/Servers/WebServer/test/Auth/RSATest.cs new file mode 100644 index 000000000..ce5b54aa0 --- /dev/null +++ b/src/Servers/WebServer/test/Auth/RSATest.cs @@ -0,0 +1,20 @@ +using System.Security.Cryptography; +using Xunit; + +namespace UniSpy.Server.WebServer.Test.Auth +{ + public class RSATest + { + [Fact] + public void KeyGen() + { + using (var rsa = new RSACryptoServiceProvider(1024)) + { + var privateKey = rsa.ExportParameters(true); + + // The public key is used for verifying the signature + var publicKey = rsa.ExportParameters(false); + } + } + } +} \ No newline at end of file