From 926b16fa36ed74364f82896316275dc1e5186e66 Mon Sep 17 00:00:00 2001 From: SpringHgui <740360381@qq.com> Date: Sat, 26 Sep 2020 23:58:29 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E7=89=B9=E6=80=A7=E3=80=91?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E5=8F=AF=E8=BF=9B=E8=A1=8C=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=99=BB=E5=BD=95=E8=AE=A4=E8=AF=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../Filters/IAuthenticationFilter.cs | 13 +++++++++++ FastTunnel.Core/Filters/IFastTunntlfilter.cs | 10 ++++++++ FastTunnel.Core/Global/FastTunnelGlobal.cs | 23 +++++++++++++++++++ .../Handlers/Server/LoginMessageHandler.cs | 20 ++++++++++++++++ FastTunnel.Core/Models/ClientContext.cs | 13 +++++++++++ .../Filters/TestAuthenticationFilter.cs | 18 +++++++++++++++ FastTunnel.Server/Program.cs | 3 ++- .../Service/ServiceFastTunnelServer.cs | 16 ++++++++++--- build.cmd | 12 ++++++++++ 10 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 FastTunnel.Core/Filters/IAuthenticationFilter.cs create mode 100644 FastTunnel.Core/Filters/IFastTunntlfilter.cs create mode 100644 FastTunnel.Core/Global/FastTunnelGlobal.cs create mode 100644 FastTunnel.Core/Models/ClientContext.cs create mode 100644 FastTunnel.Server/Filters/TestAuthenticationFilter.cs create mode 100644 build.cmd diff --git a/.gitignore b/.gitignore index 21facb3..82e1eb2 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ publish /SuiDao.Client/*.user /SuiDao.Client/Properties/PublishProfiles/FolderProfile.pubxml /FastTunnel.Core/*.user +/build diff --git a/FastTunnel.Core/Filters/IAuthenticationFilter.cs b/FastTunnel.Core/Filters/IAuthenticationFilter.cs new file mode 100644 index 0000000..893d519 --- /dev/null +++ b/FastTunnel.Core/Filters/IAuthenticationFilter.cs @@ -0,0 +1,13 @@ +using FastTunnel.Core.Core; +using FastTunnel.Core.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FastTunnel.Core.Filters +{ + public interface IAuthenticationFilter : IFastTunntlfilter + { + bool Authentication(FastTunnelServer server, LogInMassage requet); + } +} diff --git a/FastTunnel.Core/Filters/IFastTunntlfilter.cs b/FastTunnel.Core/Filters/IFastTunntlfilter.cs new file mode 100644 index 0000000..8c15020 --- /dev/null +++ b/FastTunnel.Core/Filters/IFastTunntlfilter.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace FastTunnel.Core.Filters +{ + public interface IFastTunntlfilter + { + } +} diff --git a/FastTunnel.Core/Global/FastTunnelGlobal.cs b/FastTunnel.Core/Global/FastTunnelGlobal.cs new file mode 100644 index 0000000..178e79d --- /dev/null +++ b/FastTunnel.Core/Global/FastTunnelGlobal.cs @@ -0,0 +1,23 @@ +using FastTunnel.Core.Filters; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace FastTunnel.Core.Global +{ + public static class FastTunnelGlobal + { + static IList m_filters = new List(); + + public static void AddFilter(IFastTunntlfilter filter) + { + m_filters.Add(filter); + } + + public static IEnumerable GetFilters(Type type) + { + return m_filters.Where(x => { return x.GetType().GetInterfaces().Contains(type); }); + } + } +} diff --git a/FastTunnel.Core/Handlers/Server/LoginMessageHandler.cs b/FastTunnel.Core/Handlers/Server/LoginMessageHandler.cs index 80bd820..64ed783 100644 --- a/FastTunnel.Core/Handlers/Server/LoginMessageHandler.cs +++ b/FastTunnel.Core/Handlers/Server/LoginMessageHandler.cs @@ -1,5 +1,6 @@ using FastTunnel.Core.Core; using FastTunnel.Core.Extensions; +using FastTunnel.Core.Filters; using FastTunnel.Core.Handlers.Server; using FastTunnel.Core.Models; using Microsoft.Extensions.Logging; @@ -39,6 +40,25 @@ namespace FastTunnel.Core.Handlers { bool hasTunnel = false; + var filters = Global.FastTunnelGlobal.GetFilters(typeof(IAuthenticationFilter)); + if (filters.Count() > 0) + { + foreach (IAuthenticationFilter item in filters) + { + var result = item.Authentication(server, requet); + if (!result) + { + client.Send(new Message + { + MessageType = MessageType.Log, + Content = new LogMassage(LogMsgType.Error, "认证失败") + }); + + return; + } + } + } + var sb = new StringBuilder($"{Environment.NewLine}=====隧道已建立成功,可通过以下方式访问内网服务====={Environment.NewLine}"); if (requet.Webs != null && requet.Webs.Count() > 0) { diff --git a/FastTunnel.Core/Models/ClientContext.cs b/FastTunnel.Core/Models/ClientContext.cs new file mode 100644 index 0000000..dfd762a --- /dev/null +++ b/FastTunnel.Core/Models/ClientContext.cs @@ -0,0 +1,13 @@ +using FastTunnel.Core.Core; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FastTunnel.Core.Models +{ + public class ClientContext + { + public FastTunnelServer CurrentServer { get; internal set; } + public LogInMassage LogInMassage { get; internal set; } + } +} diff --git a/FastTunnel.Server/Filters/TestAuthenticationFilter.cs b/FastTunnel.Server/Filters/TestAuthenticationFilter.cs new file mode 100644 index 0000000..b97b682 --- /dev/null +++ b/FastTunnel.Server/Filters/TestAuthenticationFilter.cs @@ -0,0 +1,18 @@ +using FastTunnel.Core.Core; +using FastTunnel.Core.Filters; +using FastTunnel.Core.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace FastTunnel.Server.Filters +{ + public class TestAuthenticationFilter : IAuthenticationFilter + { + public bool Authentication(FastTunnelServer server, LogInMassage requet) + { + return true; + } + } +} diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index 9d4d404..015acad 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting; using FastTunnel.Server.Service; using FastTunnel.Core.Logger; using FastTunnel.Core.Config; +using FastTunnel.Server.Filters; namespace FastTunnel.Server { @@ -34,7 +35,7 @@ namespace FastTunnel.Server services.AddHostedService(); // DI - services.AddTransient(); + services.AddTransient(); //services.AddSingleton(); }); } diff --git a/FastTunnel.Server/Service/ServiceFastTunnelServer.cs b/FastTunnel.Server/Service/ServiceFastTunnelServer.cs index 9d7a09e..c26fef5 100644 --- a/FastTunnel.Server/Service/ServiceFastTunnelServer.cs +++ b/FastTunnel.Server/Service/ServiceFastTunnelServer.cs @@ -1,5 +1,7 @@ using FastTunnel.Core.Config; using FastTunnel.Core.Core; +using FastTunnel.Core.Global; +using FastTunnel.Server.Filters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -15,17 +17,25 @@ namespace FastTunnel.Server.Service { ILogger _logger; FastTunnelServer _fastTunnelServer; - - public ServiceFastTunnelServer(ILogger logger, IConfiguration configuration) + TestAuthenticationFilter _testAuthenticationFilter; + IConfiguration _configuration; + public ServiceFastTunnelServer( + ILogger logger, + IConfiguration configuration, + TestAuthenticationFilter testAuthenticationFilter) { + _configuration = configuration; + _testAuthenticationFilter = testAuthenticationFilter; _logger = logger; - _fastTunnelServer = new FastTunnelServer(_logger, configuration.Get().ServerSettings); } public Task StartAsync(CancellationToken cancellationToken) { _logger.LogInformation("===== FastTunnel Server Starting ====="); + _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get().ServerSettings); + FastTunnelGlobal.AddFilter(_testAuthenticationFilter); + try { _fastTunnelServer.Run(); diff --git a/build.cmd b/build.cmd new file mode 100644 index 0000000..4f4590e --- /dev/null +++ b/build.cmd @@ -0,0 +1,12 @@ +@echo off + +for /d %%p in (FastTunnel.Client,FastTunnel.Server,SuiDao.Client,SuiDao.Server) do ( + CD ./%%p + + for %%I in (win-x64,osx-x64,linux-x64) do ( + dotnet publish -o=../build/%%p.%%I -c=release -r=%%I & echo f |xcopy %~dp0%%p\appsettings.json %~dp0publish\%%p.%%I\appsettings.json + ) + cd ../ +) + +pause \ No newline at end of file