This commit is contained in:
Gui.H 2022-09-13 15:56:46 +08:00
commit 98d2b82653
4 changed files with 29 additions and 13 deletions

View File

@ -1,11 +1,11 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. #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 WORKDIR /app
EXPOSE 80 EXPOSE 80
EXPOSE 443 EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src WORKDIR /src
COPY ["FastTunnel.Server/FastTunnel.Server.csproj", "FastTunnel.Server/"] COPY ["FastTunnel.Server/FastTunnel.Server.csproj", "FastTunnel.Server/"]
COPY ["FastTunnel.Core/FastTunnel.Core.csproj", "FastTunnel.Core/"] COPY ["FastTunnel.Core/FastTunnel.Core.csproj", "FastTunnel.Core/"]

View File

@ -18,6 +18,11 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes> <PackageReleaseNotes>FastTunnel.Core</PackageReleaseNotes>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Listener\PortProxyListenerV2.cs" />
<Compile Remove="Listener\TcpServerHandler.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />

View File

@ -7,6 +7,7 @@
using FastTunnel.Core.Client; using FastTunnel.Core.Client;
using FastTunnel.Core.Exceptions; using FastTunnel.Core.Exceptions;
using FastTunnel.Core.Extensions; using FastTunnel.Core.Extensions;
using FastTunnel.Core.Listener;
using FastTunnel.Core.Models; using FastTunnel.Core.Models;
using FastTunnel.Core.Sockets; using FastTunnel.Core.Sockets;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
@ -41,7 +42,7 @@ namespace FastTunnel.Core.Handlers.Server
/// <param name="_socket">用户请求</param> /// <param name="_socket">用户请求</param>
/// <param name="client">FastTunnel客户端</param> /// <param name="client">FastTunnel客户端</param>
/// <returns></returns> /// <returns></returns>
public async Task DispatchAsync(Socket _socket, WebSocket client) public async Task DispatchAsync(Socket _socket, WebSocket client, PortProxyListener listener)
{ {
var msgId = Guid.NewGuid().ToString().Replace("-", ""); 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}"); logger.LogDebug($"[Forward]Swap开始 {msgId}|{_config.RemotePort}=>{_config.LocalIp}:{_config.LocalPort}");
var tcs = new TaskCompletionSource<Stream>(); var tcs = new TaskCompletionSource<Stream>();
tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Dispatch TimeOut]:{msgId}"); });
_server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None)); _server.ResponseTasks.TryAdd(msgId, (tcs, CancellationToken.None));
@ -76,13 +76,11 @@ namespace FastTunnel.Core.Handlers.Server
return; 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 }) using (var stream2 = new NetworkStream(_socket, true) { ReadTimeout = 1000 * 60 * 10 })
{ {
await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1)); await Task.WhenAny(stream1.CopyToAsync(stream2), stream2.CopyToAsync(stream1));
} }
logger.LogDebug($"[Forward]Swap OK {msgId}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -90,7 +88,9 @@ namespace FastTunnel.Core.Handlers.Server
} }
finally finally
{ {
logger.LogDebug($"[Forward]Swap结束 {msgId}");
_server.ResponseTasks.TryRemove(msgId, out _); _server.ResponseTasks.TryRemove(msgId, out _);
listener.DecrementClients();
} }
} }

View File

@ -74,19 +74,16 @@ namespace FastTunnel.Core.Listener
} }
} }
private void ProcessAcceptAsync(SocketAsyncEventArgs e) private async void ProcessAcceptAsync(SocketAsyncEventArgs e)
{ {
if (e.SocketError == SocketError.Success) if (e.SocketError == SocketError.Success)
{ {
var accept = e.AcceptSocket; var accept = e.AcceptSocket;
Interlocked.Increment(ref m_numConnectedSockets); IncrementClients();
_logerr.LogInformation($"【{ListenIp}:{ListenPort}】Accepted. There are {{0}} clients connected to the port",
m_numConnectedSockets);
// 将此客户端交由Dispatcher进行管理 // 将此客户端交由Dispatcher进行管理
_requestDispatcher.DispatchAsync(accept, client); _requestDispatcher.DispatchAsync(accept, client, this);
// Accept the next connection request // Accept the next connection request
StartAccept(e); StartAccept(e);
@ -123,5 +120,19 @@ namespace FastTunnel.Core.Listener
listenSocket.Close(); 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);
}
} }
} }