diff --git a/FastTunnel.Client/FastTunnel.Client.csproj b/FastTunnel.Client/FastTunnel.Client.csproj
index 9d20edd..8a4cd72 100644
--- a/FastTunnel.Client/FastTunnel.Client.csproj
+++ b/FastTunnel.Client/FastTunnel.Client.csproj
@@ -5,7 +5,6 @@
-
diff --git a/FastTunnel.Client/Program.cs b/FastTunnel.Client/Program.cs
index 0a2de75..5560b5e 100644
--- a/FastTunnel.Client/Program.cs
+++ b/FastTunnel.Client/Program.cs
@@ -49,6 +49,7 @@ class Program
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.UseSerilog((context, services, configuration) => configuration
+ .MinimumLevel.Debug()
.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
.WriteTo.Console())
.ConfigureServices((hostContext, services) =>
diff --git a/FastTunnel.Core/Extensions/ServicesExtensions.cs b/FastTunnel.Core/Extensions/ServicesExtensions.cs
index 5d1e0a5..ec43b9b 100644
--- a/FastTunnel.Core/Extensions/ServicesExtensions.cs
+++ b/FastTunnel.Core/Extensions/ServicesExtensions.cs
@@ -6,7 +6,6 @@
using FastTunnel.Core.Client;
using FastTunnel.Core.Config;
-using FastTunnel.Core.Forwarder.MiddleWare;
using FastTunnel.Core.Handlers.Client;
using FastTunnel.Core.Handlers.Server;
using FastTunnel.Core.Server;
diff --git a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/HandleHttpConnectionMiddleware.cs b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/HandleHttpConnectionMiddleware.cs
index ed38482..772dab0 100644
--- a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/HandleHttpConnectionMiddleware.cs
+++ b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/HandleHttpConnectionMiddleware.cs
@@ -35,6 +35,13 @@ internal class FastTunnelConnectionMiddleware
await ftContext.TryAnalysisPipeAsync();
logger.LogInformation("=========TryAnalysisPipeAsync END===========");
- await next(ftContext.IsFastTunnel ? ftContext : context);
+ if (ftContext.IsFastTunnel)
+ {
+ await next(ftContext.IsFastTunnel ? ftContext : context);
+ }
+ else
+ {
+ await next(context);
+ }
}
}
diff --git a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/SwapConnectionMiddleware.cs b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/SwapConnectionMiddleware.cs
index e7bd940..414d343 100644
--- a/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/SwapConnectionMiddleware.cs
+++ b/FastTunnel.Core/Forwarder/Kestrel/MiddleWare/SwapConnectionMiddleware.cs
@@ -7,6 +7,7 @@
using System;
using System.Buffers;
using System.IO;
+using System.IO.Pipelines;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
@@ -17,6 +18,7 @@ using FastTunnel.Core.Forwarder.MiddleWare;
using FastTunnel.Core.Models.Massage;
using FastTunnel.Core.Protocol;
using FastTunnel.Core.Server;
+using FastTunnel.Core.Utilitys;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Features;
using Microsoft.Extensions.Logging;
@@ -46,11 +48,11 @@ internal class SwapConnectionMiddleware
{
if (ctx.Method == ProtocolConst.HTTP_METHOD_SWAP)
{
- await doSwap(ctx);
+ await setResponse(ctx);
}
else if (ctx.MatchWeb != null)
{
- await waitSwap(ctx);
+ await waitResponse(ctx);
}
else
{
@@ -63,17 +65,19 @@ internal class SwapConnectionMiddleware
}
}
- private async Task waitSwap(FastTunnelConnectionContext context)
+ private async Task waitResponse(FastTunnelConnectionContext context)
{
var requestId = Guid.NewGuid().ToString().Replace("-", "");
var web = context.MatchWeb;
- TaskCompletionSource tcs = new();
+ TaskCompletionSource tcs = new();
logger.LogDebug($"[Http]Swap开始 {requestId}|{context.Host}=>{web.WebConfig.LocalIp}:{web.WebConfig.LocalPort}");
tcs.SetTimeOut(10000, () => { logger.LogDebug($"[Proxy TimeOut]:{requestId}"); });
fastTunnelServer.ResponseTasks.TryAdd(requestId, tcs);
+ IDuplexPipe res = null;
+
try
{
try
@@ -89,29 +93,34 @@ internal class SwapConnectionMiddleware
throw new ClienOffLineException("客户端离线");
}
- using var res = await tcs.Task;
- using var reverseConnection = new DuplexPipeStream(context.Transport.Input, context.Transport.Output, true);
+ var lifetime = context.Features.Get();
- var t1 = res.CopyToAsync(reverseConnection);
- var t2 = reverseConnection.CopyToAsync(res);
+ res = await tcs.Task;
- await Task.WhenAll(t1, t2);
+ // using var reverseConnection = new DuplexPipeStream(context.Transport.Input, context.Transport.Output, true);
- logger.LogDebug("[Http]Swap结束");
+ var t1 = res.Input.CopyToAsync(context.Transport.Output, lifetime.ConnectionClosed);
+ var t2 = context.Transport.Input.CopyToAsync(res.Output, lifetime.ConnectionClosed);
+ await Task.WhenAny(t1, t2);
}
- catch (Exception)
+ catch (Exception ex)
{
- throw;
+ logger.LogError(ex, "[waitResponse]");
}
finally
{
+ logger.LogInformation("[Http] waitSwap结束");
fastTunnelServer.ResponseTasks.TryRemove(requestId, out _);
- context.Transport.Input.Complete();
- context.Transport.Output.Complete();
+
+ await context.Transport.Input.CompleteAsync();
+ await context.Transport.Output.CompleteAsync();
+
+ await res.Input.CompleteAsync();
+ await res.Output.CompleteAsync();
}
}
- private async Task doSwap(FastTunnelConnectionContext context)
+ private async Task setResponse(FastTunnelConnectionContext context)
{
var requestId = context.MessageId;
if (!fastTunnelServer.ResponseTasks.TryRemove(requestId, out var responseStream))
@@ -119,31 +128,26 @@ internal class SwapConnectionMiddleware
throw new Exception($"[PROXY]:RequestId不存在 {requestId}");
};
- using var reverseConnection = new DuplexPipeStream(context.Transport.Input, context.Transport.Output, true);
- responseStream.TrySetResult(reverseConnection);
+ //using var reverseConnection = new DuplexPipeStream(context.Transport.Input, context.Transport.Output, true);
+ responseStream.TrySetResult(context.Transport);
var lifetime = context.Features.Get();
-
var closedAwaiter = new TaskCompletionSource