From 320efdaf500e605449cab8e98b3adeeca51a45b2 Mon Sep 17 00:00:00 2001 From: SpringHgui <740360381@qq.com> Date: Sat, 12 Jun 2021 15:29:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastTunnel.Client/FastTunnel.Client.csproj | 18 ++--- FastTunnel.Client/Program.cs | 2 +- FastTunnel.Core/Client/SuiDaoClient.cs | 4 +- FastTunnel.Core/Client/SuiDaoServer.cs | 13 +++- .../Extensions/ServicesExtensions.cs | 9 ++- FastTunnel.Core/FastTunnel.Core.csproj | 24 +++---- .../Services/ServiceFastTunnelClient.cs | 70 +++++++++---------- .../Services/ServiceFastTunnelServer.cs | 46 ++++++------ FastTunnel.Server/FastTunnel.Server.csproj | 8 +-- FastTunnel.Server/Program.cs | 1 - FastTunnel.sln | 5 -- addhost.bat | 9 +++ 12 files changed, 115 insertions(+), 94 deletions(-) create mode 100644 addhost.bat diff --git a/FastTunnel.Client/FastTunnel.Client.csproj b/FastTunnel.Client/FastTunnel.Client.csproj index 316ae51..908cb94 100644 --- a/FastTunnel.Client/FastTunnel.Client.csproj +++ b/FastTunnel.Client/FastTunnel.Client.csproj @@ -6,15 +6,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/FastTunnel.Client/Program.cs b/FastTunnel.Client/Program.cs index 1cb7e21..1f79601 100644 --- a/FastTunnel.Client/Program.cs +++ b/FastTunnel.Client/Program.cs @@ -17,7 +17,7 @@ namespace FastTunnel.Client { public static void Main(string[] args) { - var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); try { CreateHostBuilder(args).Build().Run(); diff --git a/FastTunnel.Core/Client/SuiDaoClient.cs b/FastTunnel.Core/Client/SuiDaoClient.cs index f1da057..3982a3b 100644 --- a/FastTunnel.Core/Client/SuiDaoClient.cs +++ b/FastTunnel.Core/Client/SuiDaoClient.cs @@ -127,8 +127,10 @@ namespace FastTunnel.Core.Client } } - public void Login(Func login, SuiDaoServer serverConfig) + public void Start(Func login, SuiDaoServer serverConfig) { + _logger.LogInformation("===== FastTunnel Client Start ====="); + _serverConfig = serverConfig; lastLogin = login; diff --git a/FastTunnel.Core/Client/SuiDaoServer.cs b/FastTunnel.Core/Client/SuiDaoServer.cs index 6f80955..c214438 100644 --- a/FastTunnel.Core/Client/SuiDaoServer.cs +++ b/FastTunnel.Core/Client/SuiDaoServer.cs @@ -6,6 +6,8 @@ using System.Collections.Concurrent; using System; using FastTunnel.Core.Listener; using FastTunnel.Core.Dispatchers; +using System.Threading.Tasks; +using System.Threading; namespace FastTunnel.Core.Client { @@ -26,9 +28,9 @@ namespace FastTunnel.Core.Client ServerSettings = settings; } - public void Run() + public void Run(CancellationToken cancellationToken) { - _logger.LogDebug("FastTunnel Server Start"); + _logger.LogInformation("===== FastTunnel Server Starting ====="); CheckSettins(); @@ -68,5 +70,12 @@ namespace FastTunnel.Core.Client _logger.LogInformation($"监听HTTP请求 -> {ServerSettings.BindAddr}:{ServerSettings.WebProxyPort}"); } + + public void Stop(CancellationToken cancellationToken) + { + _logger.LogInformation("===== FastTunnel Server Stoping ====="); + + // TODO:释放资源和线程 + } } } diff --git a/FastTunnel.Core/Extensions/ServicesExtensions.cs b/FastTunnel.Core/Extensions/ServicesExtensions.cs index 96982e6..701c798 100644 --- a/FastTunnel.Core/Extensions/ServicesExtensions.cs +++ b/FastTunnel.Core/Extensions/ServicesExtensions.cs @@ -11,13 +11,20 @@ namespace FastTunnel.Core.Extensions { public static class ServicesExtensions { + /// + /// 添加服务端后台进程 + /// + /// public static void AddFastTunnelServer(this IServiceCollection services) { services.AddTransient(); - services.AddHostedService(); } + /// + /// 添加客户端后台进程 + /// + /// public static void AddFastTunnelClient(this IServiceCollection services) { services.AddSingleton() .AddSingleton() diff --git a/FastTunnel.Core/FastTunnel.Core.csproj b/FastTunnel.Core/FastTunnel.Core.csproj index 2e06ac4..d808529 100644 --- a/FastTunnel.Core/FastTunnel.Core.csproj +++ b/FastTunnel.Core/FastTunnel.Core.csproj @@ -2,7 +2,7 @@ Library - netcoreapp3.1 + net5.0 https://github.com/SpringHgui/FastTunnel @@ -32,17 +32,17 @@ - - - - - - - - - - - + + + + + + + + + + + diff --git a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs b/FastTunnel.Core/Services/ServiceFastTunnelClient.cs index 92d07d6..82913af 100644 --- a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs +++ b/FastTunnel.Core/Services/ServiceFastTunnelClient.cs @@ -12,10 +12,9 @@ namespace FastTunnel.Core.Services { public class ServiceFastTunnelClient : IHostedService { - ILogger _logger; - IConfiguration _configuration; - FastTunnelClient _fastTunnelClient; - ClientConfig config; + readonly ILogger _logger; + readonly FastTunnelClient _fastTunnelClient; + readonly ClientConfig _config; public ServiceFastTunnelClient( ILogger logger, @@ -23,48 +22,47 @@ namespace FastTunnel.Core.Services FastTunnelClient fastTunnelClient) { _logger = logger; - _configuration = configuration; _fastTunnelClient = fastTunnelClient; + _config = configuration.Get().ClientSettings; } public Task StartAsync(CancellationToken cancellationToken) { - _logger.LogInformation("===== FastTunnel Client Start ====="); - - config = _configuration.Get().ClientSettings; - - _fastTunnelClient.Login(() => + return Task.Run(() => { - Connecter _client; + _fastTunnelClient.Start(() => + { + Connecter _client; - try - { - // 连接到的目标IP - _client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort); - _client.Connect(); - } - catch (Exception) - { - Thread.Sleep(5000); - throw; - } - - // 登录 - _client.Send(new Message - { - MessageType = MessageType.C_LogIn, - Content = new LogInMassage + try { - Webs = config.Webs, - SSH = config.SSH, - AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa" - }, - }); + // 连接到的目标IP + _client = new Connecter(_config.Common.ServerAddr, _config.Common.ServerPort); + _client.Connect(); + } + catch (Exception) + { + Thread.Sleep(5000); + throw; + } - return _client; - }, config.Common); + // 登录 + _client.Send(new Message + { + MessageType = MessageType.C_LogIn, + Content = new LogInMassage + { + Webs = _config.Webs, + SSH = _config.SSH, + AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa" + }, + }); - return Task.CompletedTask; + return _client; + }, _config.Common); + + + }, cancellationToken); } public Task StopAsync(CancellationToken cancellationToken) diff --git a/FastTunnel.Core/Services/ServiceFastTunnelServer.cs b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs index 443d750..fbae18c 100644 --- a/FastTunnel.Core/Services/ServiceFastTunnelServer.cs +++ b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs @@ -13,24 +13,26 @@ using System.Linq; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; +using FastTunnel.Core.Models; namespace FastTunnel.Core.Services { public class ServiceFastTunnelServer : IHostedService { - ILogger _logger; + readonly ILogger _logger; + readonly IFastTunnelAuthenticationFilter _authenticationFilter; + readonly AppSettings _appSettings; + FastTunnelServer _fastTunnelServer; - IFastTunnelAuthenticationFilter _authenticationFilter; - IConfiguration _configuration; public ServiceFastTunnelServer( ILogger logger, IConfiguration configuration, IFastTunnelAuthenticationFilter authenticationFilter) { - _configuration = configuration; _authenticationFilter = authenticationFilter; _logger = logger; + _appSettings = configuration.Get(); AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; @@ -67,31 +69,31 @@ namespace FastTunnel.Core.Services public Task StartAsync(CancellationToken cancellationToken) { - _logger.LogInformation("===== FastTunnel Server Starting ====="); - - _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get().ServerSettings); - FastTunnelGlobal.AddFilter(_authenticationFilter); - - try + return Task.Run(() => { - _fastTunnelServer.Run(); + _fastTunnelServer = new FastTunnelServer(_logger, _appSettings.ServerSettings); + FastTunnelGlobal.AddFilter(_authenticationFilter); - _logger.LogDebug("Server Run Success"); - } - catch (Exception ex) - { - // NLog: catch any exception and log it. - _logger.LogError(ex, "Server Error"); - Console.WriteLine(ex); - } + try + { + _fastTunnelServer.Run(cancellationToken); + } + catch (Exception ex) + { + // NLog: catch any exception and log it. + _logger.LogError(ex, "Server Error"); + Console.WriteLine(ex); + } - return Task.CompletedTask; + }, cancellationToken); } public Task StopAsync(CancellationToken cancellationToken) { - _logger.LogInformation("===== FastTunnel Server Stoping ====="); - return Task.CompletedTask; + return Task.Run(() => + { + _fastTunnelServer.Stop(cancellationToken); + }, cancellationToken); } } } diff --git a/FastTunnel.Server/FastTunnel.Server.csproj b/FastTunnel.Server/FastTunnel.Server.csproj index 70e0426..e1b7044 100644 --- a/FastTunnel.Server/FastTunnel.Server.csproj +++ b/FastTunnel.Server/FastTunnel.Server.csproj @@ -17,10 +17,10 @@ - - - - + + + + diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index 310553a..f236317 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -43,7 +43,6 @@ namespace FastTunnel.Server .AddJsonFile($"config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); }); - }) .ConfigureServices((hostContext, services) => { diff --git a/FastTunnel.sln b/FastTunnel.sln index 15ad552..1bba29d 100644 --- a/FastTunnel.sln +++ b/FastTunnel.sln @@ -9,11 +9,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastTunnel.Core", "FastTunn EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastTunnel.Server", "FastTunnel.Server\FastTunnel.Server.csproj", "{DEF2E322-9075-4C3F-9967-7EAF0EE28CEB}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "杂项", "杂项", "{6B874530-F9F3-4BEE-A018-566458AA2E4F}" - ProjectSection(SolutionItems) = preProject - Dockerfile = Dockerfile - EndProjectSection -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C}" EndProject Global diff --git a/addhost.bat b/addhost.bat new file mode 100644 index 0000000..e296c31 --- /dev/null +++ b/addhost.bat @@ -0,0 +1,9 @@ +@echo off +echo 127.0.0.1 test.cc >>C:\Windows\System32\drivers\etc\hosts +echo 127.0.0.1 test.test.cc >>C:\Windows\System32\drivers\etc\hosts +echo 127.0.0.1 test1.test.cc >>C:\Windows\System32\drivers\etc\hosts +echo 127.0.0.1 test111.test.cc >>C:\Windows\System32\drivers\etc\hosts +echo 127.0.0.1 test111.test.cc >>C:\Windows\System32\drivers\etc\hosts +echo 127.0.0.1 www.abc.com >>C:\Windows\System32\drivers\etc\hosts + +pause \ No newline at end of file