From 9ebc8fbcc6771e308e5587fec37c46549b30c3e2 Mon Sep 17 00:00:00 2001 From: "Gui.H" <740360381@qq.com> Date: Tue, 26 Jul 2022 14:32:48 +0800 Subject: [PATCH 1/2] Update Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1fe6f53..62b97a6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base +FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /src COPY ["FastTunnel.Server/FastTunnel.Server.csproj", "FastTunnel.Server/"] COPY ["FastTunnel.Core/FastTunnel.Core.csproj", "FastTunnel.Core/"] From 42766d1294fb6682a15f95da74b8c3fc11f6baba Mon Sep 17 00:00:00 2001 From: "Gui.H" Date: Mon, 12 Sep 2022 23:15:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4TCP=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=95=B0=E4=B8=BA=E5=AE=9E=E6=97=B6=E8=BF=9E=E6=8E=A5=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastTunnel.Core/FastTunnel.Core.csproj | 5 ++++ .../Handlers/Server/ForwardDispatcher.cs | 10 ++++---- FastTunnel.Core/Listener/PortProxyListener.cs | 23 ++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/FastTunnel.Core/FastTunnel.Core.csproj b/FastTunnel.Core/FastTunnel.Core.csproj index e33ca86..edbc1a6 100644 --- a/FastTunnel.Core/FastTunnel.Core.csproj +++ b/FastTunnel.Core/FastTunnel.Core.csproj @@ -18,6 +18,11 @@ true FastTunnel.Core + + + + + diff --git a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs index 119ee56..01d9263 100644 --- a/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs +++ b/FastTunnel.Core/Handlers/Server/ForwardDispatcher.cs @@ -7,6 +7,7 @@ using FastTunnel.Core.Client; using FastTunnel.Core.Exceptions; using FastTunnel.Core.Extensions; +using FastTunnel.Core.Listener; using FastTunnel.Core.Models; using FastTunnel.Core.Sockets; using Microsoft.AspNetCore.Hosting.Server; @@ -41,7 +42,7 @@ namespace FastTunnel.Core.Handlers.Server /// 用户请求 /// FastTunnel客户端 /// - public async Task DispatchAsync(Socket _socket, WebSocket client) + public async Task DispatchAsync(Socket _socket, WebSocket client, PortProxyListener listener) { var msgId = Guid.NewGuid().ToString().Replace("-", ""); @@ -51,7 +52,6 @@ namespace FastTunnel.Core.Handlers.Server logger.LogDebug($"[Forward]Swap开始 {msgId}|{_config.RemotePort}=>{_config.LocalIp}:{_config.LocalPort}"); var tcs = new TaskCompletionSource(); - tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Dispatch TimeOut]:{msgId}"); }); _server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None)); @@ -76,13 +76,11 @@ namespace FastTunnel.Core.Handlers.Server return; } - using (var stream1 = await tcs.Task) + using (var stream1 = await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10))) using (var stream2 = new NetworkStream(_socket, true) { ReadTimeout = 1000 * 60 * 10 }) { await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1)); } - - logger.LogDebug($"[Forward]Swap OK {msgId}"); } catch (Exception ex) { @@ -90,7 +88,9 @@ namespace FastTunnel.Core.Handlers.Server } finally { + logger.LogDebug($"[Forward]Swap结束 {msgId}"); _server.ResponseTasks.TryRemove(msgId, out _); + listener.DecrementClients(); } } diff --git a/FastTunnel.Core/Listener/PortProxyListener.cs b/FastTunnel.Core/Listener/PortProxyListener.cs index 1cf33c5..3fef3bd 100644 --- a/FastTunnel.Core/Listener/PortProxyListener.cs +++ b/FastTunnel.Core/Listener/PortProxyListener.cs @@ -74,19 +74,16 @@ namespace FastTunnel.Core.Listener } } - private void ProcessAcceptAsync(SocketAsyncEventArgs e) + private async void ProcessAcceptAsync(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { var accept = e.AcceptSocket; - Interlocked.Increment(ref m_numConnectedSockets); - - _logerr.LogInformation($"【{ListenIp}:{ListenPort}】Accepted. There are {{0}} clients connected to the port", - m_numConnectedSockets); + IncrementClients(); // 将此客户端交由Dispatcher进行管理 - _requestDispatcher.DispatchAsync(accept, client); + _requestDispatcher.DispatchAsync(accept, client, this); // Accept the next connection request StartAccept(e); @@ -123,5 +120,19 @@ namespace FastTunnel.Core.Listener listenSocket.Close(); } } + + internal void IncrementClients() + { + Interlocked.Increment(ref m_numConnectedSockets); + _logerr.LogInformation($"[Listener:{ListenPort}] Accepted. There are {{0}} clients connected", m_numConnectedSockets); + + } + + internal void DecrementClients() + { + Interlocked.Decrement(ref m_numConnectedSockets); + _logerr.LogInformation($"[Listener:{ListenPort}] DisConnet. There are {{0}} clients connecting", m_numConnectedSockets); + + } } }