diff --git a/FastTunnel.Server/Appsettings.cs b/FastTunnel.Core/Config/AppSettings.cs similarity index 77% rename from FastTunnel.Server/Appsettings.cs rename to FastTunnel.Core/Config/AppSettings.cs index de522a6..b3b3d03 100644 --- a/FastTunnel.Server/Appsettings.cs +++ b/FastTunnel.Core/Config/AppSettings.cs @@ -4,9 +4,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace FastTunnel.Server +namespace FastTunnel.Core.Config { - public class Appsettings + public class AppSettings { public DefaultServerConfig ServerSettings { get; set; } } diff --git a/FastTunnel.Core/Extensions/LogExtentions.cs b/FastTunnel.Core/Extensions/LogExtentions.cs index 473e004..00d9abf 100644 --- a/FastTunnel.Core/Extensions/LogExtentions.cs +++ b/FastTunnel.Core/Extensions/LogExtentions.cs @@ -9,7 +9,7 @@ namespace FastTunnel.Core.Extensions { public static void LogError(this ILogger logger, Exception ex) { - logger.LogError(ex.ToString()); + logger.LogError(ex, string.Empty); } } } diff --git a/FastTunnel.Core/Extensions/ServicesExtensions.cs b/FastTunnel.Core/Extensions/ServicesExtensions.cs new file mode 100644 index 0000000..10b46be --- /dev/null +++ b/FastTunnel.Core/Extensions/ServicesExtensions.cs @@ -0,0 +1,16 @@ +using FastTunnel.Core.Filters; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FastTunnel.Core.Extensions +{ + public static class ServicesExtensions + { + public static void AddFastTunnel(this IServiceCollection service) + { + service.AddTransient(); + } + } +} diff --git a/FastTunnel.Core/Filters/DefaultAuthenticationFilter.cs b/FastTunnel.Core/Filters/DefaultAuthenticationFilter.cs new file mode 100644 index 0000000..aec3723 --- /dev/null +++ b/FastTunnel.Core/Filters/DefaultAuthenticationFilter.cs @@ -0,0 +1,16 @@ +using FastTunnel.Core.Core; +using FastTunnel.Core.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace FastTunnel.Core.Filters +{ + public class DefaultAuthenticationFilter : IAuthenticationFilter + { + public virtual bool Authentication(FastTunnelServer server, LogInMassage requet) + { + return true; + } + } +} diff --git a/FastTunnel.Server/Service/ServiceFastTunnelServer.cs b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs similarity index 83% rename from FastTunnel.Server/Service/ServiceFastTunnelServer.cs rename to FastTunnel.Core/Services/ServiceFastTunnelServer.cs index 17a7328..189b075 100644 --- a/FastTunnel.Server/Service/ServiceFastTunnelServer.cs +++ b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs @@ -1,33 +1,35 @@ using FastTunnel.Core.Config; using FastTunnel.Core.Core; +using FastTunnel.Core.Filters; using FastTunnel.Core.Global; -using FastTunnel.Server.Filters; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; -namespace FastTunnel.Server.Service +namespace FastTunnel.Core.Services { public class ServiceFastTunnelServer : IHostedService { ILogger _logger; FastTunnelServer _fastTunnelServer; - TestAuthenticationFilter _testAuthenticationFilter; + IAuthenticationFilter _authenticationFilter; IConfiguration _configuration; + public ServiceFastTunnelServer( ILogger logger, IConfiguration configuration, - TestAuthenticationFilter testAuthenticationFilter) + IAuthenticationFilter authenticationFilter) { _configuration = configuration; - _testAuthenticationFilter = testAuthenticationFilter; + _authenticationFilter = authenticationFilter; _logger = logger; AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; @@ -50,10 +52,13 @@ namespace FastTunnel.Server.Service private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e) { - if (e.Exception is System.IO.DirectoryNotFoundException) + if (e.Exception is DirectoryNotFoundException) { // nlog第一次找不到文件的错误,跳过 } + else if (e.Exception is IOException && e.Exception.Source == "System.Net.Security") + { + } else { _logger.LogError(e.Exception, "【FirstChanceException】"); @@ -64,8 +69,8 @@ namespace FastTunnel.Server.Service { _logger.LogInformation("===== FastTunnel Server Starting ====="); - _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get().ServerSettings); - FastTunnelGlobal.AddFilter(_testAuthenticationFilter); + _fastTunnelServer = new FastTunnelServer(_logger, _configuration.Get().ServerSettings); + FastTunnelGlobal.AddFilter(_authenticationFilter); try { diff --git a/FastTunnel.Core/Views/Home/index.cshtml b/FastTunnel.Core/Views/Home/index.cshtml new file mode 100644 index 0000000..435c794 --- /dev/null +++ b/FastTunnel.Core/Views/Home/index.cshtml @@ -0,0 +1,11 @@ + + + + + + + + +
Coming soon!
+ + \ No newline at end of file diff --git a/FastTunnel.Core/WebApi/Controllers/HomeController.cs b/FastTunnel.Server/Controllers/HomeController.cs similarity index 67% rename from FastTunnel.Core/WebApi/Controllers/HomeController.cs rename to FastTunnel.Server/Controllers/HomeController.cs index 33c3e4e..2f24f45 100644 --- a/FastTunnel.Core/WebApi/Controllers/HomeController.cs +++ b/FastTunnel.Server/Controllers/HomeController.cs @@ -4,13 +4,13 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -namespace FastTunnel.Core.WebApi.Controllers +namespace FastTunnel.Server.Controllers { public class HomeController : Controller { - public string Index() + public ViewResult Index() { - return "ok"; + return View(); } } } diff --git a/FastTunnel.Server/Controllers/OperateContoller.cs b/FastTunnel.Server/Controllers/OperateContoller.cs new file mode 100644 index 0000000..41f8998 --- /dev/null +++ b/FastTunnel.Server/Controllers/OperateContoller.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace FastTunnel.Server.Controllers +{ + public class OperateContoller : Controller + { + [Route("restart")] + public string Restart() + { + // TODO:Restart FastTunnel + return "FastTunnel Will Restart"; + } + } +} diff --git a/FastTunnel.Server/FastTunnel.Server.csproj b/FastTunnel.Server/FastTunnel.Server.csproj index a33a853..9eb5344 100644 --- a/FastTunnel.Server/FastTunnel.Server.csproj +++ b/FastTunnel.Server/FastTunnel.Server.csproj @@ -1,30 +1,29 @@ - + + - Exe netcoreapp3.1 - Linux - 17e7d83b-9640-4fba-8e72-2b6b022a01b0 - - - - - - - - + + + + + + PreserveNewest + + + + + + + + - - - Always - - diff --git a/FastTunnel.Server/Program.cs b/FastTunnel.Server/Program.cs index 7090eb4..0c8e193 100644 --- a/FastTunnel.Server/Program.cs +++ b/FastTunnel.Server/Program.cs @@ -1,41 +1,55 @@ -using Microsoft.Extensions.DependencyInjection; -using NLog.Extensions.Logging; -using Microsoft.Extensions.Hosting; +using FastTunnel.Core.Extensions; +using FastTunnel.Core.Services; using Microsoft.AspNetCore.Hosting; -using FastTunnel.Server.Service; -using FastTunnel.Core.Logger; -using FastTunnel.Server.Filters; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NLog.Web; +using System; namespace FastTunnel.Server { - class Program + public class Program { public static void Main(string[] args) { - CreateHostBuilder(args).Build().Run(); + var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + try + { + CreateHostBuilder(args).Build().Run(); + } + catch (Exception exception) + { + //NLog: catch setup errors + logger.Error(exception, "Stopped program because of exception"); + throw; + } + finally + { + // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) + NLog.LogManager.Shutdown(); + } } - /// - /// 使用托管服务实现后台任务:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&tabs=visual-studio - /// - /// - /// public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) - .ConfigureLogging((context) => - { - context.AddNLog(NlogConfig.getNewConfig()); - }) .ConfigureServices((hostContext, services) => { + // -------------------FastTunnel START------------------ + services.AddFastTunnel(); services.AddHostedService(); - - // DI - services.AddTransient(); - //services.AddSingleton(); - }).ConfigureWebHostDefaults(webBuilder => + // -------------------FastTunnel END-------------------- + }) + .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); - }); + }) + // NLog + .ConfigureLogging((HostBuilderContext context, ILoggingBuilder logging) => + { + logging.ClearProviders(); + logging.SetMinimumLevel(LogLevel.Trace); + }) + .UseNLog(); // NLog: Setup NLog for Dependency injection } } diff --git a/FastTunnel.Server/Properties/launchSettings.json b/FastTunnel.Server/Properties/launchSettings.json index 5a6f375..499d5a5 100644 --- a/FastTunnel.Server/Properties/launchSettings.json +++ b/FastTunnel.Server/Properties/launchSettings.json @@ -1,16 +1,18 @@ -{ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:5249", - "sslPort": 0 + "applicationUrl": "http://localhost:60256", + "sslPort": 44335 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, + "launchUrl": "", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -18,13 +20,11 @@ "FastTunnel.Server": { "commandName": "Project", "launchBrowser": true, + "launchUrl": "", + "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - }, - "applicationUrl": "https://localhost:5001;http://localhost:5000" - }, - "Docker": { - "commandName": "Docker" + } } } -} \ No newline at end of file +} diff --git a/FastTunnel.Server/Startup.cs b/FastTunnel.Server/Startup.cs index 70f079c..9232e88 100644 --- a/FastTunnel.Server/Startup.cs +++ b/FastTunnel.Server/Startup.cs @@ -1,46 +1,52 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using FastTunnel.Core.Filters; +using FastTunnel.Server.Filters; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpsPolicy; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace FastTunnel.Server { public class Startup { - public IConfiguration Configuration { get; } - public Startup(IConfiguration configuration) { Configuration = configuration; } + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); + + // ------------------------Custom Business------------------------------ + services.AddTransient(); } - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } - else - { - app.UseExceptionHandler("/Home/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseHsts(); - } + app.UseHttpsRedirection(); app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( @@ -49,4 +55,4 @@ namespace FastTunnel.Server }); } } -} \ No newline at end of file +} diff --git a/FastTunnel.Server/Views/Home/Index.cshtml b/FastTunnel.Server/Views/Home/Index.cshtml new file mode 100644 index 0000000..87e83e1 --- /dev/null +++ b/FastTunnel.Server/Views/Home/Index.cshtml @@ -0,0 +1,6 @@ +@* + For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +*@ +@{ +} +
Soming soon!
\ No newline at end of file diff --git a/FastTunnel.Server/appsettings.Development.json b/FastTunnel.Server/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/FastTunnel.Server/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/FastTunnel.Server/appsettings.json b/FastTunnel.Server/appsettings.json index 6c5486f..7aa775a 100644 --- a/FastTunnel.Server/appsettings.json +++ b/FastTunnel.Server/appsettings.json @@ -1,31 +1,33 @@ -{ +{ "Logging": { "LogLevel": { - "Default": "Information", // Information - "Microsoft": "Error", - "Microsoft.Hosting.Lifetime": "Error" + // Trace Debug Information Warning Error + "Default": "Trace", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } }, + "AllowedHosts": "*", "ServerSettings": { - // 必选 默认值 + // ѡ Ĭֵ "BindAddr": "0.0.0.0", - // 必选 默认值 + // ѡ Ĭֵ "BindPort": 1271, - // 自定义域名web穿透必须 + // Զweb͸ "WebDomain": "test.cc", - // 服务监听的端口号, 访问自定义域名站点时url为 http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/ - // web穿透必须 + // Ķ˿ں, ԶվʱurlΪ http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/ + // web͸ "WebProxyPort": 1270, - // 可选,ngixn反向代理后可省略域名后的端口号进行访问 + // ѡngixnʡĶ˿ںŽз "WebHasNginxProxy": false, - // 可选,访问白名单,不在白名单的ip拒绝 + // ѡʰڰipܾ "WebAllowAccessIps": [], - // 可选,是否开启SSH,禁用后不处理SSH类型端口转发.默认false。 + // ѡǷSSHú󲻴SSHͶ˿ת.Ĭfalse "SSHEnabled": true } -} \ No newline at end of file +} diff --git a/FastTunnel.Server/nlog.config b/FastTunnel.Server/nlog.config new file mode 100644 index 0000000..53b543a --- /dev/null +++ b/FastTunnel.Server/nlog.config @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FastTunnel.sln b/FastTunnel.sln index 41490e6..15ad552 100644 --- a/FastTunnel.sln +++ b/FastTunnel.sln @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "杂项", "杂项", "{6B8745 Dockerfile = Dockerfile EndProjectSection EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,6 +38,9 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {C8ADFEB1-59DB-4CE3-8D04-5B547107BCCB} = {0E2A9DA2-26AE-4657-B4C5-3A913E2F5A3C} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3D9C6B44-6706-4EE8-9043-802BBE474A2E} EndGlobalSection diff --git a/FastTunnel.Server/fasttunnel.service b/fasttunnel.service similarity index 100% rename from FastTunnel.Server/fasttunnel.service rename to fasttunnel.service