FastTunnel/FastTunnel.Server/Program.cs

90 lines
2.6 KiB
C#
Raw Normal View History

2019-12-16 10:29:06 +08:00
using Microsoft.Extensions.Configuration;
using FastTunnel.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
2020-01-02 23:59:31 +08:00
using Microsoft.Extensions.DependencyInjection;
using NLog.Extensions.Logging;
using Microsoft.Extensions.Logging;
using NLog;
using FastTunnel.Core.Config;
using FastTunnel.Core.Host;
2020-04-01 23:20:35 +08:00
using FastTunnel.Core.Core;
2020-04-07 23:56:46 +08:00
using FastTunnel.Core.Handlers;
using FastTunnel.Core.Handlers.Server;
2020-04-09 10:34:59 +08:00
using FastTunnel.Core.Logger;
2019-12-16 10:29:06 +08:00
namespace FastTunnel.Server
{
class Program
{
2020-01-02 23:59:31 +08:00
static Appsettings appsettings;
2019-12-16 10:29:06 +08:00
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 Server 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();
Run(servicesProvider);
}
catch (Exception ex)
{
// NLog: catch any exception and log it.
logger.Error(ex);
Console.WriteLine(ex);
2020-01-02 23:59:31 +08:00
}
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 ServerConfig implementationFactory(IServiceProvider arg)
2019-12-16 10:29:06 +08:00
{
2020-01-02 23:59:31 +08:00
if (appsettings == null)
{
2020-01-02 23:59:31 +08:00
var conf = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.Build();
appsettings = conf.Get<Appsettings>();
}
2020-01-02 23:59:31 +08:00
return appsettings.ServerSettings;
}
private static void Config(ServiceCollection service)
{
service.AddSingleton<FastTunnelServer>()
2020-04-07 23:56:46 +08:00
.AddSingleton<LoginHandler>()
.AddSingleton<HeartHandler>()
.AddSingleton<SwapMsgHandler>()
2020-04-08 19:21:23 +08:00
.AddSingleton<IConfigHandler, ConfigHandler>()
2020-01-02 23:59:31 +08:00
.AddSingleton<ServerConfig>(implementationFactory);
}
private static void Run(IServiceProvider servicesProvider)
{
var server = servicesProvider.GetRequiredService<FastTunnelServer>();
server.Run();
2020-01-03 00:12:19 +08:00
while (true)
{
Thread.Sleep(10000 * 60);
}
2019-12-16 10:29:06 +08:00
}
}
}