diff --git a/FastTunnel.Api/Controllers/SystemController.cs b/FastTunnel.Api/Controllers/SystemController.cs
index d4e4baa..553e125 100644
--- a/FastTunnel.Api/Controllers/SystemController.cs
+++ b/FastTunnel.Api/Controllers/SystemController.cs
@@ -5,7 +5,7 @@
// Copyright (c) 2019 Gui.H
using FastTunnel.Api.Models;
-using FastTunnel.Core.Client;
+using FastTunnel.Core.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
diff --git a/FastTunnel.Core/Client/FastTunnelClient.cs b/FastTunnel.Core/Client/FastTunnelClient.cs
index ddbbdd4..e60b77e 100644
--- a/FastTunnel.Core/Client/FastTunnelClient.cs
+++ b/FastTunnel.Core/Client/FastTunnelClient.cs
@@ -12,8 +12,8 @@ using System.Threading.Tasks;
using FastTunnel.Core.Config;
using FastTunnel.Core.Extensions;
using FastTunnel.Core.Handlers.Client;
-using FastTunnel.Core.Models;
using FastTunnel.Core.Models.Massage;
+using FastTunnel.Core.Protocol;
using FastTunnel.Core.Utilitys;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
@@ -80,8 +80,8 @@ public class FastTunnelClient : IFastTunnelClient
// 连接到的目标IP
socket = new ClientWebSocket();
socket.Options.RemoteCertificateValidationCallback = delegate { return true; };
- socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_VERSION, AssemblyUtility.GetVersion().ToString());
- socket.Options.SetRequestHeader(FastTunnelConst.FASTTUNNEL_TOKEN, ClientConfig.Token);
+ socket.Options.SetRequestHeader(ProtocolConst.FASTTUNNEL_VERSION, AssemblyUtility.GetVersion().ToString());
+ socket.Options.SetRequestHeader(ProtocolConst.FASTTUNNEL_TOKEN, ClientConfig.Token);
_logger.LogInformation($"正在连接服务端 {Server.ServerAddr}:{Server.ServerPort}");
await socket.ConnectAsync(
@@ -110,7 +110,7 @@ public class FastTunnelClient : IFastTunnelClient
private async Task ReceiveServerAsync(CancellationToken cancellationToken)
{
- var buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH];
+ var buffer = new byte[ProtocolConst.MAX_CMD_LENGTH];
while (!cancellationToken.IsCancellationRequested)
{
var res = await socket.ReceiveAsync(buffer, cancellationToken);
@@ -144,7 +144,7 @@ public class FastTunnelClient : IFastTunnelClient
}
catch (Exception ex)
{
- _logger.LogError(ex);
+ _logger.LogError(ex, "HandleServerRequest Error");
}
}
diff --git a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs b/FastTunnel.Core/Client/ServiceFastTunnelClient.cs
similarity index 96%
rename from FastTunnel.Core/Services/ServiceFastTunnelClient.cs
rename to FastTunnel.Core/Client/ServiceFastTunnelClient.cs
index e782358..ddbe0a9 100644
--- a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs
+++ b/FastTunnel.Core/Client/ServiceFastTunnelClient.cs
@@ -7,11 +7,10 @@
using System;
using System.Threading;
using System.Threading.Tasks;
-using FastTunnel.Core.Client;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
-namespace FastTunnel.Core.Services;
+namespace FastTunnel.Core.Client;
public class ServiceFastTunnelClient : IHostedService
{
diff --git a/FastTunnel.Core/Exceptions/APIErrorException.cs b/FastTunnel.Core/Exceptions/APIErrorException.cs
deleted file mode 100644
index 52c9816..0000000
--- a/FastTunnel.Core/Exceptions/APIErrorException.cs
+++ /dev/null
@@ -1,17 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License").
-// You may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
-// Copyright (c) 2019 Gui.H
-
-using System;
-
-namespace FastTunnel.Core.Exceptions;
-
-public class APIErrorException : Exception
-{
- public APIErrorException(string message)
- : base(message)
- {
- }
-}
diff --git a/FastTunnel.Core/Extensions/ListenOptionsSwapExtensions.cs b/FastTunnel.Core/Extensions/ListenOptionsSwapExtensions.cs
index 06bf832..fe0354c 100644
--- a/FastTunnel.Core/Extensions/ListenOptionsSwapExtensions.cs
+++ b/FastTunnel.Core/Extensions/ListenOptionsSwapExtensions.cs
@@ -4,8 +4,8 @@
// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
// Copyright (c) 2019 Gui.H
-using FastTunnel.Core.Client;
-using FastTunnel.Core.Forwarder.Kestrel;
+using FastTunnel.Core.Forwarder.Kestrel.MiddleWare;
+using FastTunnel.Core.Server;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@@ -14,15 +14,19 @@ namespace FastTunnel.Core.Extensions;
public static class ListenOptionsSwapExtensions
{
+ ///
+ /// 使用FastTunnel中间件
+ ///
+ ///
+ ///
public static ListenOptions UseConnectionFastTunnel(this ListenOptions listenOptions)
{
+ var fastTunnelServer = listenOptions.KestrelServerOptions.ApplicationServices.GetRequiredService();
var loggerFactory = listenOptions.KestrelServerOptions.ApplicationServices.GetRequiredService();
var logger = loggerFactory.CreateLogger();
- var loggerClient = loggerFactory.CreateLogger();
- var loggerHttp = loggerFactory.CreateLogger();
- var fastTunnelServer = listenOptions.KestrelServerOptions.ApplicationServices.GetRequiredService();
+ var loggerHttp = loggerFactory.CreateLogger();
- listenOptions.Use(next => new HandleHttpConnectionMiddleware(next, loggerHttp, fastTunnelServer).OnConnectionAsync);
+ listenOptions.Use(next => new FastTunnelConnectionMiddleware(next, loggerHttp, fastTunnelServer).OnConnectionAsync);
listenOptions.Use(next => new SwapConnectionMiddleware(next, logger, fastTunnelServer).OnConnectionAsync);
// 登录频率低,放在后面
diff --git a/FastTunnel.Core/Extensions/LoggerExtentions.cs b/FastTunnel.Core/Extensions/LoggerExtentions.cs
deleted file mode 100644
index 32578f5..0000000
--- a/FastTunnel.Core/Extensions/LoggerExtentions.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License").
-// You may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
-// Copyright (c) 2019 Gui.H
-
-using System;
-using Microsoft.Extensions.Logging;
-
-namespace FastTunnel.Core.Extensions;
-
-public static class LoggerExtentions
-{
- public static void LogError(this ILogger logger, Exception ex)
- {
- logger.LogError(ex, string.Empty);
- }
-}
diff --git a/FastTunnel.Core/Extensions/ServicesExtensions.cs b/FastTunnel.Core/Extensions/ServicesExtensions.cs
index 03acc11..5d1e0a5 100644
--- a/FastTunnel.Core/Extensions/ServicesExtensions.cs
+++ b/FastTunnel.Core/Extensions/ServicesExtensions.cs
@@ -9,7 +9,7 @@ using FastTunnel.Core.Config;
using FastTunnel.Core.Forwarder.MiddleWare;
using FastTunnel.Core.Handlers.Client;
using FastTunnel.Core.Handlers.Server;
-using FastTunnel.Core.Services;
+using FastTunnel.Core.Server;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -46,7 +46,6 @@ public static class ServicesExtensions
services.Configure(configurationSection)
.AddTransient()
.AddSingleton()
- .AddSingleton()
.AddSingleton();
}
diff --git a/FastTunnel.Core/Extensions/SocketExtensions.cs b/FastTunnel.Core/Extensions/SocketExtensions.cs
deleted file mode 100644
index 8d2d484..0000000
--- a/FastTunnel.Core/Extensions/SocketExtensions.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License").
-// You may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
-// Copyright (c) 2019 Gui.H
-
-using System.Net.Sockets;
-using System.Text;
-using FastTunnel.Core.Models;
-using FastTunnel.Core.Models.Massage;
-
-namespace FastTunnel.Core.Extensions;
-
-public static class SocketExtensions
-{
- public static void SendCmd(this Socket socket, Message message)
- where T : TunnelMassage
- {
- socket.Send(Encoding.UTF8.GetBytes(message.ToJson() + "\n"));
- }
-}
diff --git a/FastTunnel.Core/Extensions/WebSocketExtensions.cs b/FastTunnel.Core/Extensions/WebSocketExtensions.cs
index b51c469..7b1caf8 100644
--- a/FastTunnel.Core/Extensions/WebSocketExtensions.cs
+++ b/FastTunnel.Core/Extensions/WebSocketExtensions.cs
@@ -10,7 +10,8 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FastTunnel.Core.Exceptions;
-using FastTunnel.Core.Models;
+using FastTunnel.Core.Models.Massage;
+using FastTunnel.Core.Protocol;
namespace FastTunnel.Core.Extensions;
@@ -24,7 +25,7 @@ public static class WebSocketExtensions
}
var buffer = Encoding.UTF8.GetBytes($"{(char)type}{content}\n");
- if (type != MessageType.LogIn && buffer.Length > FastTunnelConst.MAX_CMD_LENGTH)
+ if (type != MessageType.LogIn && buffer.Length > ProtocolConst.MAX_CMD_LENGTH)
throw new ArgumentOutOfRangeException(nameof(content));
await socket.SendAsync(buffer, WebSocketMessageType.Binary, false, cancellationToken);
diff --git a/FastTunnel.Core/Filters/FastTunnelExceptionFilter.cs b/FastTunnel.Core/Filters/FastTunnelExceptionFilter.cs
deleted file mode 100644
index 07f80ca..0000000
--- a/FastTunnel.Core/Filters/FastTunnelExceptionFilter.cs
+++ /dev/null
@@ -1,37 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License").
-// You may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
-// Copyright (c) 2019 Gui.H
-
-using FastTunnel.Core.Extensions;
-using Microsoft.AspNetCore.Hosting;
-using Microsoft.AspNetCore.Mvc.Filters;
-using Microsoft.Extensions.Hosting;
-using Microsoft.Extensions.Logging;
-
-namespace FastTunnel.Core.Filters;
-
-public class FastTunnelExceptionFilter : IExceptionFilter
-{
- private readonly IWebHostEnvironment _hostingEnvironment;
- private readonly ILogger logger;
-
- public FastTunnelExceptionFilter(
- ILogger logger,
- IWebHostEnvironment hostingEnvironment)
- {
- this.logger = logger;
- _hostingEnvironment = hostingEnvironment;
- }
-
- public void OnException(ExceptionContext context)
- {
- if (!_hostingEnvironment.IsDevelopment())
- {
- return;
- }
-
- logger.LogError(context.Exception, "[全局异常]");
- }
-}
diff --git a/FastTunnel.Core/Forwarder/Kestrel/ClientConnectionMiddleware.cs b/FastTunnel.Core/Forwarder/Kestrel/ClientConnectionMiddleware.cs
deleted file mode 100644
index be9e0af..0000000
--- a/FastTunnel.Core/Forwarder/Kestrel/ClientConnectionMiddleware.cs
+++ /dev/null
@@ -1,100 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License").
-// You may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-// https://github.com/FastTunnel/FastTunnel/edit/v2/LICENSE
-// Copyright (c) 2019 Gui.H
-
-using System;
-using System.Buffers;
-using System.Text;
-using System.Threading.Tasks;
-using FastTunnel.Core.Client;
-using Microsoft.AspNetCore.Connections;
-using Microsoft.Extensions.Logging;
-
-namespace FastTunnel.Core.Forwarder.Kestrel;
-internal class ClientConnectionMiddleware
-{
- private readonly ConnectionDelegate next;
- private readonly ILogger logger;
- private readonly FastTunnelServer fastTunnelServer;
-
- public ClientConnectionMiddleware(ConnectionDelegate next, ILogger logger, FastTunnelServer fastTunnelServer)
- {
- this.next = next;
- this.logger = logger;
- this.fastTunnelServer = fastTunnelServer;
- }
-
- internal async Task OnConnectionAsync(ConnectionContext context)
- {
- if (!await ReadPipeAsync(context))
- {
- await next(context);
- }
- }
-
- ///
- ///
- ///
- ///
- /// is for FastTunnel
- private async Task ReadPipeAsync(ConnectionContext context)
- {
- var reader = context.Transport.Input;
-
- var isProxy = false;
- while (true)
- {
- var result = await reader.ReadAsync();
- var buffer = result.Buffer;
- SequencePosition? position = null;
-
- do
- {
- position = buffer.PositionOf((byte)'\n');
-
- if (position != null)
- {
- isProxy = ProcessProxyLine(buffer.Slice(0, position.Value));
- if (isProxy)
- {
- await Login(buffer, position.Value, context);
- return true;
- }
- else
- {
- context.Transport.Input.AdvanceTo(buffer.Start, buffer.Start);
- return false;
- }
- }
- }
- while (position != null);
-
- if (result.IsCompleted)
- {
- break;
- }
- }
-
- return false;
- }
-
- private async Task Login(ReadOnlySequence buffer, SequencePosition position, ConnectionContext context)
- {
-
-
- }
-
- ///
- ///
- ///
- ///
- private bool ProcessProxyLine(ReadOnlySequence readOnlySequence)
- {
- var str = Encoding.UTF8.GetString(readOnlySequence);
-
- return str.StartsWith("LOGIN");
- }
-}
-
diff --git a/FastTunnel.Core/Forwarder/Kestrel/FastTunnelConnectionContext.cs b/FastTunnel.Core/Forwarder/Kestrel/FastTunnelConnectionContext.cs
index 6beae9d..5ac55f9 100644
--- a/FastTunnel.Core/Forwarder/Kestrel/FastTunnelConnectionContext.cs
+++ b/FastTunnel.Core/Forwarder/Kestrel/FastTunnelConnectionContext.cs
@@ -10,8 +10,8 @@ using System.Collections.Generic;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
-using FastTunnel.Core.Client;
using FastTunnel.Core.Models;
+using FastTunnel.Core.Server;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Logging;
@@ -38,7 +38,7 @@ internal class FastTunnelConnectionContext : ConnectionContext
public override IDictionary