mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
调整client的启动方式
This commit is contained in:
parent
abacc6b0a0
commit
f64ab45332
|
@ -1,13 +0,0 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Client
|
||||
{
|
||||
public class Appsettings
|
||||
{
|
||||
public ClientConfig ClientSettings { get; set; }
|
||||
}
|
||||
}
|
|
@ -12,9 +12,10 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.0" />
|
||||
<PackageReference Include="NLog" Version="4.6.8" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
|
||||
<PackageReference Include="NLog" Version="4.6.8" />
|
||||
<PackageReference Include="NLog" Version="4.7.5" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -25,6 +26,9 @@
|
|||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="nlog.config">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ProjectExtensions>
|
||||
|
|
|
@ -1,116 +1,58 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using FastTunnel.Core;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using FastTunnel.Core.Host;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Services;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Models;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Handlers.Client;
|
||||
using FastTunnel.Core.Logger;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Web;
|
||||
|
||||
namespace FastTunnel.Client
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
LogManager.Configuration = NlogConfig.getNewConfig();
|
||||
var logger = LogManager.GetCurrentClassLogger();
|
||||
logger.Debug("===== FastTunnel Client Start =====");
|
||||
|
||||
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
|
||||
try
|
||||
{
|
||||
var servicesProvider = new Host().Config(Config).Build();
|
||||
|
||||
Run(servicesProvider);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(10000 * 60);
|
||||
}
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception exception)
|
||||
{
|
||||
// NLog: catch any exception and log it.
|
||||
logger.Error(ex, "Stopped program because of 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)
|
||||
LogManager.Shutdown();
|
||||
NLog.LogManager.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private static void Run(IServiceProvider servicesProvider)
|
||||
{
|
||||
var client = servicesProvider.GetRequiredService<FastTunnelClient>();
|
||||
var config = servicesProvider.GetRequiredService<ClientConfig>();
|
||||
|
||||
client.Login(() =>
|
||||
{
|
||||
Connecter _client;
|
||||
|
||||
try
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
// 连接到的目标IP
|
||||
_client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort);
|
||||
_client.Connect();
|
||||
}
|
||||
catch (Exception)
|
||||
// -------------------FastTunnel START------------------
|
||||
services.AddSingleton<FastTunnelClient>()
|
||||
.AddSingleton<ClientHeartHandler>()
|
||||
.AddSingleton<LogHandler>()
|
||||
.AddSingleton<HttpRequestHandler>()
|
||||
.AddSingleton<NewSSHHandler>();
|
||||
|
||||
services.AddHostedService<ServiceFastTunnelClient>();
|
||||
// -------------------FastTunnel EDN--------------------
|
||||
})
|
||||
.ConfigureLogging((HostBuilderContext context, ILoggingBuilder logging) =>
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
throw;
|
||||
}
|
||||
|
||||
// 登录
|
||||
_client.Send(new Message<LogInMassage>
|
||||
{
|
||||
MessageType = MessageType.C_LogIn,
|
||||
Content = new LogInMassage
|
||||
{
|
||||
Webs = config.Webs,
|
||||
SSH = config.SSH,
|
||||
AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa"
|
||||
},
|
||||
});
|
||||
|
||||
return _client;
|
||||
}, config.Common);
|
||||
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(10000 * 60);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Config(ServiceCollection service)
|
||||
{
|
||||
service.AddSingleton<FastTunnelClient>()
|
||||
.AddSingleton<ClientHeartHandler>()
|
||||
.AddSingleton<LogHandler>()
|
||||
.AddSingleton<HttpRequestHandler>()
|
||||
.AddSingleton<NewSSHHandler>()
|
||||
.AddSingleton<ClientConfig>(implementationFactory);
|
||||
}
|
||||
|
||||
private static ClientConfig implementationFactory(IServiceProvider arg)
|
||||
{
|
||||
var conf = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", true, true)
|
||||
.Build();
|
||||
|
||||
var settings = conf.Get<Appsettings>();
|
||||
return settings.ClientSettings;
|
||||
}
|
||||
logging.ClearProviders();
|
||||
logging.SetMinimumLevel(LogLevel.Trace);
|
||||
})
|
||||
.UseNLog(); // NLog: Setup NLog for Dependency injection
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
// Trace Debug Information Warning Error
|
||||
"Default": "Trace",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
|
|
26
FastTunnel.Client/nlog.config
Normal file
26
FastTunnel.Client/nlog.config
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
autoReload="true"
|
||||
internalLogLevel="Info">
|
||||
|
||||
<!-- enable asp.net core layout renderers -->
|
||||
<extensions>
|
||||
<add assembly="NLog.Web.AspNetCore"/>
|
||||
</extensions>
|
||||
<targets>
|
||||
<target xsi:type="File" name="file"
|
||||
layout="${longdate} ${logger} ${message}${exception:format=ToString}"
|
||||
fileName="${basedir}/Logs/${shortdate}.${level}.log" />
|
||||
|
||||
<target xsi:type="Console" name="console"
|
||||
layout="${date}|${level:uppercase=true}|${message} ${exception}" />
|
||||
<!--layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />-->
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
<logger name="*" minlevel="Debug" writeTo="file,console" />
|
||||
<!--Skip non-critical Microsoft logs and so log only own logs-->
|
||||
<logger name="Microsoft.*" maxlevel="Info" final="true" />
|
||||
</rules>
|
||||
</nlog>
|
|
@ -9,5 +9,7 @@ namespace FastTunnel.Core.Config
|
|||
public class AppSettings
|
||||
{
|
||||
public DefaultServerConfig ServerSettings { get; set; }
|
||||
|
||||
public ClientConfig ClientSettings { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
76
FastTunnel.Core/Services/ServiceFastTunnelClient.cs
Normal file
76
FastTunnel.Core/Services/ServiceFastTunnelClient.cs
Normal file
|
@ -0,0 +1,76 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Core;
|
||||
using FastTunnel.Core.Models;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Services
|
||||
{
|
||||
public class ServiceFastTunnelClient : IHostedService
|
||||
{
|
||||
ILogger<ServiceFastTunnelClient> _logger;
|
||||
IConfiguration _configuration;
|
||||
FastTunnelClient _fastTunnelClient;
|
||||
ClientConfig config;
|
||||
|
||||
public ServiceFastTunnelClient(
|
||||
ILogger<ServiceFastTunnelClient> logger,
|
||||
IConfiguration configuration,
|
||||
FastTunnelClient fastTunnelClient)
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
_fastTunnelClient = fastTunnelClient;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Client Start =====");
|
||||
|
||||
config = _configuration.Get<AppSettings>().ClientSettings;
|
||||
|
||||
_fastTunnelClient.Login(() =>
|
||||
{
|
||||
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<LogInMassage>
|
||||
{
|
||||
MessageType = MessageType.C_LogIn,
|
||||
Content = new LogInMassage
|
||||
{
|
||||
Webs = config.Webs,
|
||||
SSH = config.SSH,
|
||||
AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa"
|
||||
},
|
||||
});
|
||||
|
||||
return _client;
|
||||
}, config.Common);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Client Stoping =====");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -44,7 +44,6 @@ namespace FastTunnel.Server
|
|||
{
|
||||
webBuilder.UseStartup<Startup>();
|
||||
})
|
||||
// NLog
|
||||
.ConfigureLogging((HostBuilderContext context, ILoggingBuilder logging) =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
|
|
Loading…
Reference in New Issue
Block a user