mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
优化代码
This commit is contained in:
parent
320efdaf50
commit
b05decd0e8
|
@ -8,10 +8,10 @@
|
|||
}
|
||||
},
|
||||
"ClientSettings": {
|
||||
"Common": {
|
||||
"Server": {
|
||||
// 服务端公网ip, 对应服务端配置文件的 BindAddr,支持域名
|
||||
"ServerAddr": "test.cc",
|
||||
|
||||
// "ServerAddr": "my.com",
|
||||
"ServerAddr": "127.0.0.1",
|
||||
// 服务端通信端口,对应服务端配置文件的 BindPort
|
||||
"ServerPort": 1271
|
||||
},
|
||||
|
@ -27,7 +27,7 @@
|
|||
"SubDomain": "test", // test.test.cc
|
||||
|
||||
// 个人域名(需要解析域名A记录至服务的ip地址)
|
||||
"WWW": [ "www.abc.com","test111.test.cc" ]
|
||||
"WWW": [ "www.abc.com", "test111.test.cc" ]
|
||||
}
|
||||
],
|
||||
|
||||
|
|
|
@ -13,13 +13,12 @@ using System.Timers;
|
|||
using System.Threading;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using FastTunnel.Core.Handlers.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace FastTunnel.Core.Client
|
||||
{
|
||||
public class FastTunnelClient
|
||||
{
|
||||
public SuiDaoServer _serverConfig;
|
||||
|
||||
Connecter _client;
|
||||
|
||||
ILogger<FastTunnelClient> _logger;
|
||||
|
@ -27,21 +26,25 @@ namespace FastTunnel.Core.Client
|
|||
System.Timers.Timer timer_timeout;
|
||||
System.Timers.Timer timer_heart;
|
||||
|
||||
Func<Connecter> lastLogin;
|
||||
double heartInterval = 10 * 1000; // 10 秒心跳
|
||||
public DateTime lastHeart;
|
||||
Thread th;
|
||||
|
||||
int reTrySpan = 30 * 1000; // 登陆失败后重试间隔
|
||||
int reTrySpan = 10 * 1000; // 登陆失败后重试间隔
|
||||
HttpRequestHandler _newCustomerHandler;
|
||||
NewSSHHandler _newSSHHandler;
|
||||
LogHandler _logHandler;
|
||||
ClientHeartHandler _clientHeartHandler;
|
||||
Func<Connecter> lastLogin;
|
||||
Message<LogInMassage> loginMsg;
|
||||
|
||||
public IClientConfig ClientConfig { get; set; }
|
||||
|
||||
public FastTunnelClient(
|
||||
ILogger<FastTunnelClient> logger,
|
||||
HttpRequestHandler newCustomerHandler,
|
||||
NewSSHHandler newSSHHandler, LogHandler logHandler,
|
||||
ILogger<FastTunnelClient> logger,
|
||||
HttpRequestHandler newCustomerHandler,
|
||||
NewSSHHandler newSSHHandler, LogHandler logHandler,
|
||||
IConfiguration configuration,
|
||||
ClientHeartHandler clientHeartHandler)
|
||||
{
|
||||
_logger = logger;
|
||||
|
@ -49,6 +52,7 @@ namespace FastTunnel.Core.Client
|
|||
_newSSHHandler = newSSHHandler;
|
||||
_logHandler = logHandler;
|
||||
_clientHeartHandler = clientHeartHandler;
|
||||
ClientConfig = configuration.Get<AppSettings>().ClientSettings;
|
||||
initailTimer();
|
||||
}
|
||||
|
||||
|
@ -127,11 +131,26 @@ namespace FastTunnel.Core.Client
|
|||
}
|
||||
}
|
||||
|
||||
public void Start(Func<Connecter> login, SuiDaoServer serverConfig)
|
||||
/// <summary>
|
||||
/// 启动客户端
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="customLoginMsg">自定义登录信息,可进行扩展业务</param>
|
||||
public void Start(object customLoginMsg)
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Client Start =====");
|
||||
|
||||
_serverConfig = serverConfig;
|
||||
loginMsg = new Message<LogInMassage>
|
||||
{
|
||||
MessageType = MessageType.C_LogIn,
|
||||
Content = new LogInMassage
|
||||
{
|
||||
Webs = ClientConfig.Webs,
|
||||
SSH = ClientConfig.SSH,
|
||||
CustomInfo = customLoginMsg,
|
||||
},
|
||||
};
|
||||
|
||||
lastLogin = login;
|
||||
|
||||
try
|
||||
|
@ -150,6 +169,31 @@ namespace FastTunnel.Core.Client
|
|||
LogSuccess(_client.Socket);
|
||||
}
|
||||
|
||||
private Connecter login()
|
||||
{
|
||||
Connecter _client;
|
||||
_logger.LogInformation($"正在连接服务端 {ClientConfig.Server.ServerAddr}:{ClientConfig.Server.ServerPort}");
|
||||
|
||||
try
|
||||
{
|
||||
// 连接到的目标IP
|
||||
_client = new Connecter(ClientConfig.Server.ServerAddr, ClientConfig.Server.ServerPort);
|
||||
_client.Connect();
|
||||
|
||||
_logger.LogInformation("连接成功");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
throw;
|
||||
}
|
||||
|
||||
// 登录
|
||||
_client.Send(loginMsg);
|
||||
|
||||
return _client;
|
||||
}
|
||||
|
||||
void Close()
|
||||
{
|
||||
timer_heart.Stop();
|
|
@ -8,6 +8,7 @@ using FastTunnel.Core.Listener;
|
|||
using FastTunnel.Core.Dispatchers;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace FastTunnel.Core.Client
|
||||
{
|
||||
|
@ -22,13 +23,13 @@ namespace FastTunnel.Core.Client
|
|||
ClientListener client_listener;
|
||||
HttpListener http_listener;
|
||||
|
||||
public FastTunnelServer(ILogger logger, IServerConfig settings)
|
||||
public FastTunnelServer(ILogger<FastTunnelServer> logger, IConfiguration configuration)
|
||||
{
|
||||
_logger = logger;
|
||||
ServerSettings = settings;
|
||||
ServerSettings = configuration.Get<AppSettings>().ServerSettings;
|
||||
}
|
||||
|
||||
public void Run(CancellationToken cancellationToken)
|
||||
public void Run()
|
||||
{
|
||||
_logger.LogInformation("===== FastTunnel Server Starting =====");
|
||||
|
|
@ -1,13 +1,16 @@
|
|||
using FastTunnel.Core.Models;
|
||||
using FastTunnel.Core.Config;
|
||||
using FastTunnel.Core.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FastTunnel.Core.Config
|
||||
namespace FastTunnel.Core.Client
|
||||
{
|
||||
public class ClientConfig
|
||||
public interface IClientConfig
|
||||
{
|
||||
public SuiDaoServer Common { get; set; }
|
||||
public SuiDaoServer Server { get; set; }
|
||||
|
||||
public IEnumerable<WebConfig> Webs { get; set; }
|
||||
|
||||
|
@ -20,4 +23,4 @@ namespace FastTunnel.Core.Config
|
|||
|
||||
public int ServerPort { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,6 +10,6 @@ namespace FastTunnel.Core.Config
|
|||
{
|
||||
public DefaultServerConfig ServerSettings { get; set; }
|
||||
|
||||
public ClientConfig ClientSettings { get; set; }
|
||||
public DefaultClientConfig ClientSettings { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
17
FastTunnel.Core/Config/DefaultClientConfig.cs
Normal file
17
FastTunnel.Core/Config/DefaultClientConfig.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Config
|
||||
{
|
||||
public class DefaultClientConfig : IClientConfig
|
||||
{
|
||||
public SuiDaoServer Server { get; set; }
|
||||
|
||||
public IEnumerable<WebConfig> Webs { get; set; }
|
||||
|
||||
public IEnumerable<SSHConfig> SSH { get; set; }
|
||||
}
|
||||
}
|
|
@ -17,7 +17,9 @@ namespace FastTunnel.Core.Extensions
|
|||
/// <param name="services"></param>
|
||||
public static void AddFastTunnelServer(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IFastTunnelAuthenticationFilter, DefaultAuthenticationFilter>();
|
||||
services.AddSingleton<IFastTunnelAuthenticationFilter, DefaultAuthenticationFilter>();
|
||||
services.AddSingleton<FastTunnelServer, FastTunnelServer>();
|
||||
|
||||
services.AddHostedService<ServiceFastTunnelServer>();
|
||||
}
|
||||
|
||||
|
@ -25,7 +27,8 @@ namespace FastTunnel.Core.Extensions
|
|||
/// 添加客户端后台进程
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
public static void AddFastTunnelClient(this IServiceCollection services) {
|
||||
public static void AddFastTunnelClient(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FastTunnelClient>()
|
||||
.AddSingleton<ClientHeartHandler>()
|
||||
.AddSingleton<LogHandler>()
|
||||
|
|
|
@ -17,7 +17,8 @@ namespace FastTunnel.Core.Handlers.Client
|
|||
public void HandlerMsg(FastTunnelClient cleint, Message<JObject> Msg)
|
||||
{
|
||||
var request = Msg.Content.ToObject<NewCustomerMassage>();
|
||||
var connecter = new Connecter(cleint._serverConfig.ServerAddr, cleint._serverConfig.ServerPort);
|
||||
var connecter = new Connecter(cleint.ClientConfig.Server.ServerAddr, cleint.ClientConfig.Server.ServerPort);
|
||||
|
||||
connecter.Connect();
|
||||
connecter.Send(new Message<SwapMassage> { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request.MsgId) });
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace FastTunnel.Core.Handlers.Client
|
|||
public void HandlerMsg(FastTunnelClient cleint, Message<JObject> Msg)
|
||||
{
|
||||
var request_ssh = Msg.Content.ToObject<NewSSHRequest>();
|
||||
var connecter_ssh = new Connecter(cleint._serverConfig.ServerAddr, cleint._serverConfig.ServerPort);
|
||||
var connecter_ssh = new Connecter(cleint.ClientConfig.Server.ServerAddr, cleint.ClientConfig.Server.ServerPort);
|
||||
connecter_ssh.Connect();
|
||||
connecter_ssh.Send(new Message<SwapMassage> { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request_ssh.MsgId) });
|
||||
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
using FastTunnel.Core.Config;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core.Host
|
||||
{
|
||||
public class Host
|
||||
{
|
||||
private ServiceCollection ServiceCollection;
|
||||
|
||||
public Host()
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory()) //From NuGet Package Microsoft.Extensions.Configuration.Json
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.Build();
|
||||
|
||||
Init(config);
|
||||
}
|
||||
|
||||
public void Init(IConfiguration config)
|
||||
{
|
||||
ServiceCollection = new ServiceCollection();
|
||||
ServiceCollection.AddLogging(loggingBuilder =>
|
||||
{
|
||||
// configure Logging with NLog
|
||||
loggingBuilder.ClearProviders();
|
||||
loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
|
||||
loggingBuilder.AddNLog(config);
|
||||
});
|
||||
}
|
||||
|
||||
public Host Config(Action<ServiceCollection> fun)
|
||||
{
|
||||
fun.Invoke(ServiceCollection);
|
||||
return this;
|
||||
}
|
||||
|
||||
public IServiceProvider Build()
|
||||
{
|
||||
return ServiceCollection.BuildServiceProvider();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,9 +17,6 @@ namespace FastTunnel.Core.Models
|
|||
/// </summary>
|
||||
public IEnumerable<SSHConfig> SSH { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 身份信息,用于服务端认证
|
||||
/// </summary>
|
||||
public string AuthInfo { get; set; }
|
||||
public object CustomInfo { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,11 +6,6 @@ namespace FastTunnel.Core.Models
|
|||
{
|
||||
public class SSHConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// 协议
|
||||
/// </summary>
|
||||
public Protocol Protocol { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 局域网IP地址
|
||||
/// </summary>
|
||||
|
|
|
@ -14,54 +14,18 @@ namespace FastTunnel.Core.Services
|
|||
{
|
||||
readonly ILogger<ServiceFastTunnelClient> _logger;
|
||||
readonly FastTunnelClient _fastTunnelClient;
|
||||
readonly ClientConfig _config;
|
||||
|
||||
public ServiceFastTunnelClient(
|
||||
ILogger<ServiceFastTunnelClient> logger,
|
||||
IConfiguration configuration,
|
||||
FastTunnelClient fastTunnelClient)
|
||||
public ServiceFastTunnelClient(ILogger<ServiceFastTunnelClient> logger, FastTunnelClient fastTunnelClient)
|
||||
{
|
||||
_logger = logger;
|
||||
_fastTunnelClient = fastTunnelClient;
|
||||
_config = configuration.Get<AppSettings>().ClientSettings;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_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<LogInMassage>
|
||||
{
|
||||
MessageType = MessageType.C_LogIn,
|
||||
Content = new LogInMassage
|
||||
{
|
||||
Webs = _config.Webs,
|
||||
SSH = _config.SSH,
|
||||
AuthInfo = "ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa"
|
||||
},
|
||||
});
|
||||
|
||||
return _client;
|
||||
}, _config.Common);
|
||||
|
||||
|
||||
_fastTunnelClient.Start(new { Token = "123" });
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,19 +21,16 @@ namespace FastTunnel.Core.Services
|
|||
{
|
||||
readonly ILogger<ServiceFastTunnelServer> _logger;
|
||||
readonly IFastTunnelAuthenticationFilter _authenticationFilter;
|
||||
readonly AppSettings _appSettings;
|
||||
|
||||
FastTunnelServer _fastTunnelServer;
|
||||
FastTunnelServer _Server;
|
||||
|
||||
public ServiceFastTunnelServer(
|
||||
ILogger<ServiceFastTunnelServer> logger,
|
||||
IConfiguration configuration,
|
||||
FastTunnelServer fastTunnelServer,
|
||||
IFastTunnelAuthenticationFilter authenticationFilter)
|
||||
{
|
||||
_authenticationFilter = authenticationFilter;
|
||||
_logger = logger;
|
||||
_appSettings = configuration.Get<AppSettings>();
|
||||
|
||||
_authenticationFilter = authenticationFilter;
|
||||
_Server = fastTunnelServer;
|
||||
AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
|
||||
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
||||
}
|
||||
|
@ -71,12 +68,11 @@ namespace FastTunnel.Core.Services
|
|||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_fastTunnelServer = new FastTunnelServer(_logger, _appSettings.ServerSettings);
|
||||
FastTunnelGlobal.AddFilter(_authenticationFilter);
|
||||
|
||||
try
|
||||
{
|
||||
_fastTunnelServer.Run(cancellationToken);
|
||||
_Server.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -92,7 +88,7 @@ namespace FastTunnel.Core.Services
|
|||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
_fastTunnelServer.Stop(cancellationToken);
|
||||
_Server.Stop(cancellationToken);
|
||||
}, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using FastTunnel.Core.Client;
|
||||
using FastTunnel.Core.Filters;
|
||||
using FastTunnel.Core.Models;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -12,7 +13,7 @@ namespace FastTunnel.Server.Filters
|
|||
{
|
||||
public bool Authentication(FastTunnelServer server, LogInMassage requet)
|
||||
{
|
||||
return !string.IsNullOrEmpty(requet.AuthInfo) && requet.AuthInfo.Equals("ODadoNDONODHSoDMFMsdpapdoNDSHDoadpwPDNoWAHDoNfa");
|
||||
return (requet.CustomInfo as JObject)["Token"].ToString() == "123";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ namespace FastTunnel.Server
|
|||
services.AddControllersWithViews();
|
||||
|
||||
// ------------------------Custom Business------------------------------
|
||||
services.AddTransient<IFastTunnelAuthenticationFilter, TestAuthenticationFilter>();
|
||||
services.AddTransient<IAuthenticationFilter, TestAuthenticationFilter>();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
|
|
Loading…
Reference in New Issue
Block a user