diff --git a/src/NSmartProxy.ClientRouter/Router.cs b/src/NSmartProxy.ClientRouter/Router.cs index b9a96c1..7ddd770 100644 --- a/src/NSmartProxy.ClientRouter/Router.cs +++ b/src/NSmartProxy.ClientRouter/Router.cs @@ -379,12 +379,13 @@ private async Task OpenTransmission(int appId, TcpClient providerClient) NetworkStream providerClientStream = providerClient.GetStream(); //接收首条消息,首条消息中返回的是appid和客户端 //消费端长连接,需要在server端保活 - try + ControlMethod controlMethod; + //TODO 5 处理应用级的keepalive + while (true) { - ControlMethod controlMethod; - //TODO 5 处理应用级的keepalive - while (true) + try { + int readByteCount = await providerClientStream.ReadAsync(buffer, 0, buffer.Length); //双端标记S0001 if (readByteCount == 0) { @@ -393,29 +394,31 @@ private async Task OpenTransmission(int appId, TcpClient providerClient) return; } + } + catch (Exception ex) + { + //反弹连接出错为致命错误 + //此处出错后,应用程序需要重置,并重启 + _waiter.TrySetResult(ex); + throw; + } - //TODO 4 如果是UDP则直接转发,之后返回上层 - controlMethod = (ControlMethod)buffer[0]; + //TODO 4 如果是UDP则直接转发,之后返回上层 + controlMethod = (ControlMethod)buffer[0]; + + switch (controlMethod) + { + case ControlMethod.KeepAlive: continue; + case ControlMethod.UDPTransfer: + await OpenUdpTransmission(appId, providerClient); + continue;//udp 发送后继续循环,方法里的ConnectAppToServer会再拉起一个新连接 + case ControlMethod.TCPTransfer: + await OpenTcpTransmission(appId, providerClient, toTargetServer); + return;//tcp 开启隧道,并且不再利用此连接 + default: throw new Exception("非法请求:" + buffer[0]); + } + } //while (controlMethod == ControlMethod.KeepAlive) ; - switch (controlMethod) - { - case ControlMethod.KeepAlive: continue; - case ControlMethod.UDPTransfer: - await OpenUdpTransmission(appId, providerClient); - continue;//udp 发送后继续循环,方法里的ConnectAppToServer会再拉起一个新连接 - case ControlMethod.TCPTransfer: - await OpenTcpTransmission(appId, providerClient, toTargetServer); - return;//tcp 开启隧道,并且不再利用此连接 - } - } //while (controlMethod == ControlMethod.KeepAlive) ; - } - catch (Exception ex) - { - //反弹连接出错为致命错误 - //此处出错后,应用程序需要重置,并重启 - _waiter.TrySetResult(ex); - throw; - } } catch (Exception ex) { diff --git a/src/NSmartProxyWinform/App.config b/src/NSmartProxyWinform/App.config deleted file mode 100644 index c7138b2..0000000 --- a/src/NSmartProxyWinform/App.config +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/src/NSmartProxyWinform/NSmartProxyWinform.csproj b/src/NSmartProxyWinform/NSmartProxyWinform.csproj index 20aed6c..c93b813 100644 --- a/src/NSmartProxyWinform/NSmartProxyWinform.csproj +++ b/src/NSmartProxyWinform/NSmartProxyWinform.csproj @@ -80,18 +80,76 @@ ..\packages\Microsoft.Extensions.Primitives.2.2.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll + + ..\packages\Microsoft.Win32.Primitives.4.3.0\lib\net46\Microsoft.Win32.Primitives.dll + True + True + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll + + ..\packages\Snappy.Sharp.1.0.0\lib\netstandard1.1\Snappy.Sharp.dll + + + ..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll + True + True + ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll + + + ..\packages\System.Console.4.3.0\lib\net46\System.Console.dll + True + True + + + ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll + + + ..\packages\System.Globalization.Calendars.4.3.0\lib\net46\System.Globalization.Calendars.dll + True + True + + + ..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll + True + True + + + + ..\packages\System.IO.Compression.ZipFile.4.3.0\lib\net46\System.IO.Compression.ZipFile.dll + True + True + + + ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll + True + True + + + ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll + True + True + ..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll + + ..\packages\System.Net.Http.4.3.0\lib\net46\System.Net.Http.dll + True + True + + + ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll + True + True + ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll @@ -99,6 +157,31 @@ ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll + True + True + + + ..\packages\System.Security.Cryptography.Algorithms.4.3.0\lib\net461\System.Security.Cryptography.Algorithms.dll + True + True + + + ..\packages\System.Security.Cryptography.Encoding.4.3.0\lib\net46\System.Security.Cryptography.Encoding.dll + True + True + + + ..\packages\System.Security.Cryptography.Primitives.4.3.0\lib\net46\System.Security.Cryptography.Primitives.dll + True + True + + + ..\packages\System.Security.Cryptography.X509Certificates.4.3.0\lib\net461\System.Security.Cryptography.X509Certificates.dll + True + True + @@ -106,9 +189,13 @@ - + + ..\packages\System.Xml.ReaderWriter.4.3.0\lib\net46\System.Xml.ReaderWriter.dll + True + True + @@ -156,7 +243,6 @@ Resources.resx True - PreserveNewest diff --git a/src/NSmartProxyWinform/app.config b/src/NSmartProxyWinform/app.config deleted file mode 100644 index c7138b2..0000000 --- a/src/NSmartProxyWinform/app.config +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - diff --git a/src/NSmartProxyWinform/packages.config b/src/NSmartProxyWinform/packages.config index b2d7e68..008e667 100644 --- a/src/NSmartProxyWinform/packages.config +++ b/src/NSmartProxyWinform/packages.config @@ -10,9 +10,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/TcpClient/Program.cs b/test/TcpClient/Program.cs index b552e88..8147fd3 100644 --- a/test/TcpClient/Program.cs +++ b/test/TcpClient/Program.cs @@ -13,8 +13,9 @@ class Program { static void Main(string[] args) { + Console.WriteLine("*** Tcp Client ***"); TcpClient tcpClient = new TcpClient(); - tcpClient.Connect("127.0.0.1", 5944); + tcpClient.Connect("127.0.0.1", 64321); //tcpClient.Connect("192.168.1.2", 12306); var stream = tcpClient.GetStream(); @@ -24,10 +25,18 @@ static void Main(string[] args) byte[] buffer = new byte[4096]; while (true) { - int readResultLength = stream.Read(buffer, 0, buffer.Length); + try + { + int readResultLength = stream.Read(buffer, 0, buffer.Length); - //if(readResultLength) - Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer, 0, readResultLength).Trim()); + if (readResultLength == 0) break; + Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer, 0, readResultLength).Trim()); + } + catch + { + Console.WriteLine("stream closed ungracefully"); + break; + } } }); int x = 0; @@ -50,6 +59,8 @@ static void Main(string[] args) Thread.Sleep(1000); count++; } + + Console.WriteLine("*** Test null port ***"); //异常数据测试 } diff --git a/test/TcpServer/Program.cs b/test/TcpServer/Program.cs index c811d65..9884b64 100644 --- a/test/TcpServer/Program.cs +++ b/test/TcpServer/Program.cs @@ -14,53 +14,54 @@ class Program { static void Main(string[] args) { + Console.WriteLine("*** Tcp Sever ***"); //int port = int.Parse(args[0]); - int port = 5944; + int port = 12346; Console.WriteLine("start listen " + port.ToString()); TcpListener tcpListener = TcpListener.Create(port); tcpListener.Start(); TcpClient client = null; CancellationTokenSource cts = null; - // var thread = new Thread(() => - // { - int count = 5; + // var thread = new Thread(() => + // { + int count = 5; + //while (count > 0) + //{ + client = tcpListener.AcceptTcpClient(); + //client.SetKeepAlive(out _); + + //Task.Run(() => + //{ + var stream = client.GetStream(); + while (count > 0) { - client = tcpListener.AcceptTcpClient(); - //client.SetKeepAlive(out _); - - Task.Run(() => + byte[] bytes = new byte[4096]; + try { - var stream = client.GetStream(); - - while (count > 0) - { - byte[] bytes = new byte[4096]; - try - { - cts = new CancellationTokenSource(); - Task result = stream.ReadAsync(bytes, 0, bytes.Length, cts.Token); - result.Wait(cts.Token); - var length = result.Result; - Console.WriteLine("continue..." + length); - Console.WriteLine(ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim()); - string retMessage = "received++" + ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim() + "+++"; - byte[] retMessageBytes = ASCIIEncoding.ASCII.GetBytes(retMessage); - stream.Write(retMessageBytes, 0, retMessageBytes.Length); - - } - catch - { - break; - } - - } - - }); + cts = new CancellationTokenSource(); + Task result = stream.ReadAsync(bytes, 0, bytes.Length, cts.Token); + result.Wait(cts.Token); + var length = result.Result; + Console.WriteLine("continue..." + length); + Console.WriteLine(ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim()); + string retMessage = "received++" + ASCIIEncoding.ASCII.GetString(bytes, 0, length).Trim() + "+++"; + byte[] retMessageBytes = ASCIIEncoding.ASCII.GetBytes(retMessage); + stream.Write(retMessageBytes, 0, retMessageBytes.Length); + if (length == 0) { stream.Close(); break; } + } + catch + { + break; + } count--; } - Environment.Exit(0); + + //}); + + //} + Environment.Exit(0); //}); diff --git a/test/UdpClient/Program.cs b/test/UdpClient/Program.cs index 2ee3d77..19150ae 100644 --- a/test/UdpClient/Program.cs +++ b/test/UdpClient/Program.cs @@ -9,11 +9,12 @@ namespace ConsoleApp15 { class Program { - const int TargetPort = 30000; + const int TargetPort = 30001; static async Task Main(string[] args) { - Console.WriteLine("*** client ***"); + await Task.Delay(2000);//慢点启动,以防服务端还没起来消息就发出去了 + Console.WriteLine("*** Udp Client ***"); UdpClient udpClient = new UdpClient(); udpClient.Connect("127.0.0.1", TargetPort); @@ -32,8 +33,9 @@ static async Task Main(string[] args) } var bytes = Encoding.ASCII.GetBytes(str); - udpClient.Send(bytes, bytes.Length); - System.Threading.Thread.Sleep(1000); + await udpClient.SendAsync(bytes, bytes.Length); + Console.WriteLine($"发送{str}"); + await Task.Delay(1000); count--; } @@ -44,7 +46,7 @@ private static async Task ReceiveUdpClientAsync(UdpClient udpClient) { while (true) { - var udpReceiveResult = await udpClient.ReceiveAsync(); + var udpReceiveResult = await udpClient.ReceiveAsync();//如果服务端没起来 这里将会永久阻塞,即使之后起来了也不行 Console.WriteLine("接收到" + Encoding.ASCII.GetString(udpReceiveResult.Buffer)); } diff --git a/test/UdpServer/Program.cs b/test/UdpServer/Program.cs index f3992ba..c31c041 100644 --- a/test/UdpServer/Program.cs +++ b/test/UdpServer/Program.cs @@ -7,11 +7,11 @@ namespace UdpServer { class Program { - const int listenPort = 30000; + const int listenPort = 30001; static async Task Main(string[] args) { - Console.WriteLine("*** server ***"); + Console.WriteLine("*** Udp Sever ***"); UdpClient udpClient = new UdpClient(listenPort); //UdpClient udpClient = new UdpClient(9999); int count = 5; @@ -20,7 +20,7 @@ static async Task Main(string[] args) var udpReceiveResult = await udpClient.ReceiveAsync(); var str = Encoding.ASCII.GetString(udpReceiveResult.Buffer); var receiveStr = Encoding.ASCII.GetBytes("hello" + str); - udpClient.Send(receiveStr, receiveStr.Length, udpReceiveResult.RemoteEndPoint); + await udpClient.SendAsync(receiveStr, receiveStr.Length, udpReceiveResult.RemoteEndPoint); Console.WriteLine($"[{udpReceiveResult.RemoteEndPoint.ToString()}]{str}"); count--; } diff --git a/test/auto_test.cmd b/test/auto_test.cmd index 3ad004d..7502116 100644 --- a/test/auto_test.cmd +++ b/test/auto_test.cmd @@ -1,16 +1,39 @@ -rem autotest -set testPath=%~dp0/build/netcoreapp3.0 -set nspClientPath=%~dp0/build/nspclient/netcoreapp3.0 -set nspServerPath=%~dp0/build/nspserver/netcoreapp3.0 +@echo off +echo *** NSP TEST *** + +set buildPath=%~dp0\build +set testPath=%buildPath%\netcoreapp3.0 +set nspClientPath=%buildPath%\nspclient\netcoreapp3.0 +set nspServerPath=%buildPath%\nspserver\netcoreapp3.0 + +REM BUILD +MSBuild .\TestBed.sln /t:build +dotnet build ..\src\NSmartProxyClient\NSmartProxyClient.csproj +dotnet build ..\src\NSmartProxy.ServerHost\NSmartProxy.ServerHost.csproj + + +rem run nsmartproxy +start "" cmd /c "%nspServerPath%\NSmartProxy.ServerHost.exe" +start "" cmd /c "%nspClientPath%\NSmartProxyClient.exe" + +rem Wait them ready... +powershell Start-Sleep -Seconds 3 + +rem appsettings +copy "%buildPath%\_appsettings_server.txt" "%nspServerPath%\appsettings.json" /y +copy "%buildPath%\_appsettings_client.txt" "%nspClientPath%\appsettings.json" /y rem start server start "" cmd /c "%testPath%/TcpServer.exe" %testPath%/TcpClient.exe rem start client -start "" cmd /c "%testPath%/UdpServer.exe" -%testPath%/UdpClient.exe +start "" cmd /c "dotnet %testPath%/UdpServer.dll" +dotnet %testPath%/UdpClient.dll +echo =============================================== +echo TEST ACCOMPLISHeD +pause rem test service1 TCP rem test service2 UDP rem test service3 HTTP1