【新特性】添加自定义过滤器,可进行自定义登录认证逻辑

This commit is contained in:
SpringHgui 2020-09-26 23:58:29 +08:00
parent bc0071095c
commit 926b16fa36
10 changed files with 125 additions and 4 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ publish
/SuiDao.Client/*.user
/SuiDao.Client/Properties/PublishProfiles/FolderProfile.pubxml
/FastTunnel.Core/*.user
/build

View File

@ -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);
}
}

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace FastTunnel.Core.Filters
{
public interface IFastTunntlfilter
{
}
}

View File

@ -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<IFastTunntlfilter> m_filters = new List<IFastTunntlfilter>();
public static void AddFilter(IFastTunntlfilter filter)
{
m_filters.Add(filter);
}
public static IEnumerable<IFastTunntlfilter> GetFilters(Type type)
{
return m_filters.Where(x => { return x.GetType().GetInterfaces().Contains(type); });
}
}
}

View File

@ -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<LogMassage>
{
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)
{

View File

@ -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; }
}
}

View File

@ -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;
}
}
}

View File

@ -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<ServiceFastTunnelServer>();
// DI
services.AddTransient<FastTunnelServer>();
services.AddTransient<TestAuthenticationFilter>();
//services.AddSingleton<IServerConfig>();
});
}

View File

@ -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<ServiceFastTunnelServer> _logger;
FastTunnelServer _fastTunnelServer;
public ServiceFastTunnelServer(ILogger<ServiceFastTunnelServer> logger, IConfiguration configuration)
TestAuthenticationFilter _testAuthenticationFilter;
IConfiguration _configuration;
public ServiceFastTunnelServer(
ILogger<ServiceFastTunnelServer> logger,
IConfiguration configuration,
TestAuthenticationFilter testAuthenticationFilter)
{
_configuration = configuration;
_testAuthenticationFilter = testAuthenticationFilter;
_logger = logger;
_fastTunnelServer = new FastTunnelServer(_logger, configuration.Get<Appsettings>().ServerSettings);
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("===== FastTunnel Server Starting =====");
_fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get<Appsettings>().ServerSettings);
FastTunnelGlobal.AddFilter(_testAuthenticationFilter);
try
{
_fastTunnelServer.Run();

12
build.cmd Normal file
View File

@ -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