From 27af6ca356d14bf6dca5e1f29871406743d59cb0 Mon Sep 17 00:00:00 2001 From: shaohaozhi Date: Sun, 21 Jul 2024 21:36:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E4=BC=A0=E8=BE=93=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=EF=BC=8C=E5=8D=87=E7=BA=A7=E9=83=A8=E5=88=86=E5=BA=93?= =?UTF-8?q?=E4=BB=A5=E6=8F=90=E5=8D=87=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/NSmartProxy.ClientRouter/Router.cs | 80 +++++++++----- .../NSmartProxy.Infrastructure.csproj | 3 +- .../Shared/Global.cs | 4 +- src/NSmartProxy.Infrastructure/StringUtil.cs | 73 +++++++----- src/NSmartProxy/Server.cs | 104 +++++++++++------- src/NSmartProxyClient/appsettings.json | 10 +- .../NSmartProxyWinform.csproj | 8 +- src/NSmartProxyWinform/packages.config | 2 +- 8 files changed, 178 insertions(+), 106 deletions(-) diff --git a/src/NSmartProxy.ClientRouter/Router.cs b/src/NSmartProxy.ClientRouter/Router.cs index d7e60b2..f077e22 100644 --- a/src/NSmartProxy.ClientRouter/Router.cs +++ b/src/NSmartProxy.ClientRouter/Router.cs @@ -15,6 +15,7 @@ using NSmartProxy.ClientRouter.Dispatchers; using NSmartProxy.Data.Models; using NSmartProxy.Infrastructure; +using System.IO.Compression; namespace NSmartProxy.Client { @@ -95,8 +96,7 @@ public Router() public Router(INSmartLogger logger) : this() { - Logger = logger;//TODO 重复了,待优化 - + Logger = logger; Global.Logger = logger; } @@ -458,7 +458,7 @@ private async Task OpenTransmission(int appId, TcpClient providerClient) return;//tcp 开启隧道,并且不再利用此连接 case ControlMethod.ForceClose: Logger.Info("客户端在别处被抢登,当前被强制下线。"); - Close(); + await Close(); return; default: throw new Exception("非法请求:" + buffer[0]); } @@ -651,26 +651,36 @@ private async Task TcpTransferAsync(TcpClient providerClient, TcpClient toTarget private async Task StreamTransfer(CancellationToken ct, NetworkStream fromStream, NetworkStream toStream, string epString, ClientApp item) { - byte[] buffer = new byte[Global.ClientTunnelBufferSize]; - using (fromStream) + //if (item.IsCompress) + //{ + // await StringUtil.DecompressInSnappierAsync(fromStream, toStream, ct); + //} + //else { - int bytesRead; - while ((bytesRead = - await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + byte[] buffer = new byte[Global.ClientTunnelBufferSize]; + if (item.IsCompress) { - if (item.IsCompress) + using (GZipStream gZipStream = new GZipStream(fromStream, CompressionMode.Decompress)) { - var compressBuffer = StringUtil.DecompressInSnappy(buffer, 0, bytesRead); - bytesRead = compressBuffer.Length; - await toStream.WriteAsync(compressBuffer, 0, bytesRead, ct).ConfigureAwait(false); + int bytesRead; + while ((bytesRead = await gZipStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + } } - else + } + else + { + using (fromStream) { - //bytesRead = buffer.Length; - await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); - } + int bytesRead; + while ((bytesRead = + await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); - // await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + } + } } } Router.Logger.Debug($"{epString}对节点传输关闭。"); @@ -682,23 +692,35 @@ await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) private async Task ToStaticTransfer(CancellationToken ct, NetworkStream fromStream, NetworkStream toStream, string epString, ClientApp item) { - byte[] buffer = new byte[Global.ClientTunnelBufferSize]; - using (fromStream) + //if (item.IsCompress) + //{ + // await StringUtil.CompressInSnappierAsync(fromStream, toStream, ct); + //} + //else { - int bytesRead; - while ((bytesRead = - await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + byte[] buffer = new byte[Global.ClientTunnelBufferSize]; + if (item.IsCompress) { - if (item.IsCompress) + using(GZipStream gZipStream = new GZipStream(fromStream, CompressionMode.Compress)) { - var compressInSnappy = StringUtil.CompressInSnappy(buffer, 0, bytesRead); - var compressedBuffer = compressInSnappy.ContentBytes; - bytesRead = compressInSnappy.Length; - await toStream.WriteAsync(compressedBuffer, 0, bytesRead, ct).ConfigureAwait(false); + int bytesRead; + while ((bytesRead =await gZipStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + } } - else + } + else + { + using (fromStream) { - await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + int bytesRead; + while ((bytesRead = + await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + } } } } diff --git a/src/NSmartProxy.Infrastructure/NSmartProxy.Infrastructure.csproj b/src/NSmartProxy.Infrastructure/NSmartProxy.Infrastructure.csproj index f351579..8727c88 100644 --- a/src/NSmartProxy.Infrastructure/NSmartProxy.Infrastructure.csproj +++ b/src/NSmartProxy.Infrastructure/NSmartProxy.Infrastructure.csproj @@ -3,11 +3,12 @@ netstandard2.0 7.2 + True + - diff --git a/src/NSmartProxy.Infrastructure/Shared/Global.cs b/src/NSmartProxy.Infrastructure/Shared/Global.cs index 3dbc368..e030370 100644 --- a/src/NSmartProxy.Infrastructure/Shared/Global.cs +++ b/src/NSmartProxy.Infrastructure/Shared/Global.cs @@ -27,7 +27,7 @@ public sealed class Global public const int ClientUdpReceiveTimeout = 3000;//客户端udp接收超时时长(毫秒) - public const int ClientTunnelBufferSize = 81920; //客户端数据包大小 + public const int ClientTunnelBufferSize = 1024 * 1024 * 1024; //客户端数据包大小 public const int ClientUdpBufferSize = 65535;//服务端udp数据包大小 #region 服务端配置 @@ -35,7 +35,7 @@ public sealed class Global public const string NSPServerDisplayName = "NSPServer";//windows服务显示名 public const string NSPServerServiceName = "NSPServer";//windows服务名 - public const int ServerTunnelBufferSize = 81920;//服务端数据包大小 + public const int ServerTunnelBufferSize = 1024 * 1024 * 1024;//服务端数据包大小 public const int ServerUdpBufferSize = 65535;//服务端udp数据包大小 diff --git a/src/NSmartProxy.Infrastructure/StringUtil.cs b/src/NSmartProxy.Infrastructure/StringUtil.cs index f04410d..2b93f51 100644 --- a/src/NSmartProxy.Infrastructure/StringUtil.cs +++ b/src/NSmartProxy.Infrastructure/StringUtil.cs @@ -7,13 +7,18 @@ using System.Text; using NSmartProxy.Data; using NSmartProxy.Infrastructure; -using Snappy.Sharp; -using NSmartProxy.Shared; +using EasyCompressor; +using System.Threading; +using System.Threading.Tasks; namespace NSmartProxy { public static class StringUtil { + //Lazy初始化一个snappier + private static Lazy _snappyCompressor = new Lazy(() => SnappierCompressor.Shared); + + /// /// 整型转双字节 /// @@ -218,19 +223,20 @@ public static bool IsNum(this string str) /// /// /// - public static byte[] DecompressInSnappy(byte[] compressed, int offset, int length) - { - SnappyDecompressor sd = new SnappyDecompressor(); - try - { - return sd.Decompress(compressed, offset, length); - } - catch (Exception ex) - { - Global.Logger.Error("snappy解压缩失败", ex); - return null; - } - } + //public static byte[] DecompressInSnappy(byte[] compressed, int offset, int length) + //{ + // SnappyDecompressor sd = new SnappyDecompressor(); + // try + // { + // return sd.Decompress(compressed, offset, length); + // } + // catch (Exception ex) + // { + // _ = ex; + // //啥情況? + // return null; + // } + //} /// /// 使用snappy算法压缩 @@ -239,25 +245,32 @@ public static byte[] DecompressInSnappy(byte[] compressed, int offset, int lengt /// /// /// - public static CompressedBytes CompressInSnappy(byte[] uncompressed, int offset, int uncompressedLength) - { - try - { - SnappyCompressor sc = new SnappyCompressor(); + //public static CompressedBytes CompressInSnappy(byte[] uncompressed, int offset, int uncompressedLength) + //{ + // SnappyCompressor sc = new SnappyCompressor(); - //var bytes = Encoding.ASCII.GetBytes("HelloWor134ertegsdfgsfdgsdfgsdfgsfdgsdfgsdfgsdfgsdfgdsfgsdfgdsfgdfgdsfgld"); - byte[] outBytes = new byte[sc.MaxCompressedLength(uncompressed.Length)]; + // //var bytes = Encoding.ASCII.GetBytes("HelloWor134ertegsdfgsfdgsdfgsdfgsfdgsdfgsdfgsdfgsdfgdsfgsdfgdsfgdfgdsfgld"); + // byte[] outBytes = new byte[sc.MaxCompressedLength(uncompressed.Length)]; - int actualLength = sc.Compress(uncompressed, 0, uncompressedLength, outBytes); - return new CompressedBytes() { ContentBytes = outBytes, Length = actualLength }; - } - catch (Exception ex) - { - Global.Logger.Error("snappy压缩失败", ex); - return null; - } + // int actualLength = sc.Compress(uncompressed, 0, uncompressedLength, outBytes); + // return new CompressedBytes() { ContentBytes = outBytes, Length = actualLength }; + //} + + + public async static Task CompressInSnappierAsync(Stream inputStream, Stream outputStream, CancellationToken ct) + { + await _snappyCompressor.Value.CompressAsync(inputStream, outputStream,ct); } + public async static Task DecompressInSnappierAsync(Stream inputStream, Stream outputStream, CancellationToken ct) + { + await _snappyCompressor.Value.DecompressAsync(inputStream, outputStream, ct); + } + + + + + /// /// 压缩专用对象 /// diff --git a/src/NSmartProxy/Server.cs b/src/NSmartProxy/Server.cs index c6b1a0e..5302013 100644 --- a/src/NSmartProxy/Server.cs +++ b/src/NSmartProxy/Server.cs @@ -23,6 +23,7 @@ using NSmartProxy.Database; using NSmartProxy.Infrastructure.Extension; using System.Collections.Concurrent; +using System.IO.Compression; namespace NSmartProxy { @@ -62,7 +63,7 @@ public class Server public Server(INSmartLogger logger) { //initialize - Logger = logger;//TODO 重复了,后面要去掉,待优化 + Logger = logger; ServerContext = new NSPServerContext(); Global.Logger = logger; } @@ -863,68 +864,95 @@ async Task OpenTcpTransmission(Stream consumerStream, Stream providerStream, } + private async Task StreamTransfer(CancellationToken ct, Stream fromStream, Stream toStream, NSPApp nspApp) { - using (fromStream) + //if (nspApp.IsCompress) + //{ + // await StringUtil.DecompressInSnappierAsync(fromStream, toStream, ct); + //} + //else { - byte[] buffer = new byte[Global.ServerTunnelBufferSize]; - try + using (fromStream) { - int bytesRead; - while ((bytesRead = - await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + byte[] buffer = new byte[Global.ServerTunnelBufferSize]; + if (nspApp.IsCompress) + { + //gzip + using (var gzipStream = new GZipStream(fromStream, CompressionMode.Decompress)) + { + int bytesRead; + while ((bytesRead = await gzipStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + ServerContext.TotalReceivedBytes += bytesRead; //下行 + } + } + } + else { - if (nspApp.IsCompress) + try { - var compressBuffer = StringUtil.DecompressInSnappy(buffer, 0, bytesRead); - bytesRead = compressBuffer.Length; - await toStream.WriteAsync(compressBuffer, 0, bytesRead, ct).ConfigureAwait(false); + int bytesRead; + while ((bytesRead = + await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + ServerContext.TotalSentBytes += bytesRead; //上行 + } } - else + catch (Exception ioe) { - await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + if (ioe is IOException) { return; } //Suppress this exception. + throw; } - ServerContext.TotalSentBytes += bytesRead; //上行 } } - catch (Exception ioe) - { - if (ioe is IOException) { return; } //Suppress this exception. - throw; - } } } + private async Task ToStaticTransfer(CancellationToken ct, Stream fromStream, Stream toStream, NSPApp nspApp) { - using (fromStream) + + { - byte[] buffer = new byte[Global.ServerTunnelBufferSize]; - try + using (fromStream) { - int bytesRead; - while ((bytesRead = - await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + if (nspApp.IsCompress) { - if (nspApp.IsCompress) + //gzip + using (var gzipStream = new GZipStream(fromStream, CompressionMode.Compress)) { - var compressInSnappy = StringUtil.CompressInSnappy(buffer, 0, bytesRead); - var compressedBuffer = compressInSnappy.ContentBytes; - bytesRead = compressInSnappy.Length; - await toStream.WriteAsync(compressedBuffer, 0, bytesRead, ct).ConfigureAwait(false); + byte[] buffer = new byte[Global.ServerTunnelBufferSize]; + int bytesRead; + while ((bytesRead = await gzipStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + ServerContext.TotalReceivedBytes += bytesRead; //下行 + } } - else + } + else + { + byte[] buffer = new byte[Global.ServerTunnelBufferSize]; + try + { + int bytesRead; + while ((bytesRead = + await fromStream.ReadAsync(buffer, 0, buffer.Length, ct).ConfigureAwait(false)) != 0) + { + await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + ServerContext.TotalReceivedBytes += bytesRead; //下行 + } + } + catch (Exception ioe) { - await toStream.WriteAsync(buffer, 0, bytesRead, ct).ConfigureAwait(false); + if (ioe is IOException) { return; } //Suppress this exception. + throw; } - ServerContext.TotalReceivedBytes += bytesRead; //下行 } } - catch (Exception ioe) - { - if (ioe is IOException) { return; } //Suppress this exception. - throw; - } } } diff --git a/src/NSmartProxyClient/appsettings.json b/src/NSmartProxyClient/appsettings.json index ad7e7f3..5d8eab4 100644 --- a/src/NSmartProxyClient/appsettings.json +++ b/src/NSmartProxyClient/appsettings.json @@ -23,11 +23,17 @@ { "IP": "127.0.0.1", "TargetServicePort": "8000", - "ConsumerPort": "52995", + "ConsumerPort": "52999", "protocol": "TCP", "IsCompress": true + }, - + { + "IP": "127.0.0.1", + "TargetServicePort": "2996", + "ConsumerPort": "62997", + "protocol": "TCP", + "IsCompress": true }, { "IP": "127.0.0.1", diff --git a/src/NSmartProxyWinform/NSmartProxyWinform.csproj b/src/NSmartProxyWinform/NSmartProxyWinform.csproj index 965e8d2..c6c3055 100644 --- a/src/NSmartProxyWinform/NSmartProxyWinform.csproj +++ b/src/NSmartProxyWinform/NSmartProxyWinform.csproj @@ -274,6 +274,9 @@ PreserveNewest + + Designer + PreserveNewest @@ -310,6 +313,7 @@ + PreserveNewest @@ -347,13 +351,11 @@ del *.xml del *.pdb move .\DLLs\NSmartProxy.ClientRouter.dll .\ - + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 - - \ No newline at end of file diff --git a/src/NSmartProxyWinform/packages.config b/src/NSmartProxyWinform/packages.config index 8822402..915f128 100644 --- a/src/NSmartProxyWinform/packages.config +++ b/src/NSmartProxyWinform/packages.config @@ -1,6 +1,6 @@  - +