FastTunnel/FastTunnel.Client/Program.cs

116 lines
3.4 KiB
C#
Raw Normal View History

2019-12-16 10:29:06 +08:00
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;
2020-01-02 23:59:31 +08:00
using NLog;
using FastTunnel.Core.Host;
using FastTunnel.Core.Config;
2020-04-01 23:20:35 +08:00
using FastTunnel.Core.Core;
using FastTunnel.Core.Models;
using FastTunnel.Core.Handlers.Client;
2020-04-09 10:34:59 +08:00
using FastTunnel.Core.Logger;
2019-12-16 10:29:06 +08:00
namespace FastTunnel.Client
{
class Program
{
static void Main(string[] args)
{
2020-04-09 10:34:59 +08:00
LogManager.Configuration = NlogConfig.getNewConfig();
2020-01-02 23:59:31 +08:00
var logger = LogManager.GetCurrentClassLogger();
2020-02-28 11:49:25 +08:00
logger.Debug("===== FastTunnel Client Start =====");
2019-12-16 10:29:06 +08:00
2020-01-02 23:59:31 +08:00
try
{
var servicesProvider = new Host().Config(Config).Build();
2020-01-03 17:58:09 +08:00
2020-01-02 23:59:31 +08:00
Run(servicesProvider);
2019-12-16 10:29:06 +08:00
2020-01-02 23:59:31 +08:00
while (true)
{
Thread.Sleep(10000 * 60);
}
}
catch (Exception ex)
{
// NLog: catch any exception and log it.
logger.Error(ex, "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();
}
2019-12-16 10:29:06 +08:00
}
2020-01-02 23:59:31 +08:00
private static void Run(IServiceProvider servicesProvider)
2019-12-16 10:29:06 +08:00
{
2020-01-02 23:59:31 +08:00
var client = servicesProvider.GetRequiredService<FastTunnelClient>();
2020-03-31 16:47:19 +08:00
var config = servicesProvider.GetRequiredService<ClientConfig>();
2020-04-01 23:20:35 +08:00
client.Login(() =>
{
Connecter _client;
try
{
// 连接到的目标IP
_client = new Connecter(config.Common.ServerAddr, config.Common.ServerPort);
_client.Connect();
}
catch (Exception ex)
{
Thread.Sleep(5000);
throw;
}
// 登录
_client.Send(new Message<LogInMassage>
2020-04-01 23:20:35 +08:00
{
MessageType = MessageType.C_LogIn,
Content = new LogInMassage
2020-04-01 23:20:35 +08:00
{
Webs = config.Webs,
SSH = config.SSH
}
});
return _client;
2020-04-02 00:30:04 +08:00
}, config.Common);
while (true)
{
Thread.Sleep(10000 * 60);
}
2019-12-16 10:29:06 +08:00
}
2020-01-02 23:59:31 +08:00
private static void Config(ServiceCollection service)
{
service.AddSingleton<FastTunnelClient>()
.AddSingleton<ClientHeartHandler>()
.AddSingleton<LogHandler>()
.AddSingleton<NewCustomerHandler>()
.AddSingleton<NewSSHHandler>()
2020-01-02 23:59:31 +08:00
.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;
}
2019-12-16 10:29:06 +08:00
}
}