From d8ce0e58828f9da2a436688e79f4bb71e846294d Mon Sep 17 00:00:00 2001 From: springhgui <740360381@qq.com> Date: Tue, 16 Nov 2021 23:06:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MiddleWare/FastTunnelClientHandler.cs | 1 + .../Handlers/Server/ILoginHandler.cs | 3 +- .../Handlers/Server/LoginHandler.cs | 52 ++++++++++--------- FastTunnel.Core/Models/TunnelClient.cs | 28 ++++++++-- .../Properties/launchSettings.json | 25 +++++---- 5 files changed, 69 insertions(+), 40 deletions(-) diff --git a/FastTunnel.Core/Forwarder/MiddleWare/FastTunnelClientHandler.cs b/FastTunnel.Core/Forwarder/MiddleWare/FastTunnelClientHandler.cs index a41d4dc..4445bcf 100644 --- a/FastTunnel.Core/Forwarder/MiddleWare/FastTunnelClientHandler.cs +++ b/FastTunnel.Core/Forwarder/MiddleWare/FastTunnelClientHandler.cs @@ -73,6 +73,7 @@ namespace FastTunnel.Core.MiddleWares } var client = new TunnelClient(webSocket, fastTunnelServer, loginHandler, context.Connection.RemoteIpAddress); + client.ConnectionPort = context.Connection.LocalPort; try { diff --git a/FastTunnel.Core/Handlers/Server/ILoginHandler.cs b/FastTunnel.Core/Handlers/Server/ILoginHandler.cs index 29dbddb..f9f310a 100644 --- a/FastTunnel.Core/Handlers/Server/ILoginHandler.cs +++ b/FastTunnel.Core/Handlers/Server/ILoginHandler.cs @@ -1,4 +1,5 @@ using FastTunnel.Core.Client; +using FastTunnel.Core.Models; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +11,6 @@ namespace FastTunnel.Core.Handlers.Server { public interface ILoginHandler { - Task HandlerMsg(FastTunnelServer fastTunnelServer, WebSocket webSocket, string lineCmd); + Task HandlerMsg(FastTunnelServer fastTunnelServer, TunnelClient tunnelClient, string lineCmd); } } diff --git a/FastTunnel.Core/Handlers/Server/LoginHandler.cs b/FastTunnel.Core/Handlers/Server/LoginHandler.cs index 9b64814..21e49fe 100644 --- a/FastTunnel.Core/Handlers/Server/LoginHandler.cs +++ b/FastTunnel.Core/Handlers/Server/LoginHandler.cs @@ -20,21 +20,21 @@ namespace FastTunnel.Core.Handlers.Server { public class LoginHandler : ILoginHandler { - ILogger _logger; + ILogger logger; IProxyConfigProvider proxyConfig; public const bool NeedRecive = true; public LoginHandler(ILogger logger, IProxyConfigProvider proxyConfig) { this.proxyConfig = proxyConfig; - this._logger = logger; + this.logger = logger; } - protected async Task HandleLoginAsync(FastTunnelServer server, WebSocket client, LogInMassage requet) + protected async Task HandleLoginAsync(FastTunnelServer server, TunnelClient client, LogInMassage requet) { bool hasTunnel = false; - await client.SendCmdAsync(MessageType.Log, $"穿透协议 | 映射关系(公网=>内网)", CancellationToken.None); + await client.webSocket.SendCmdAsync(MessageType.Log, $"穿透协议 | 映射关系(公网=>内网)", CancellationToken.None); Thread.Sleep(300); if (requet.Webs != null && requet.Webs.Count() > 0) @@ -43,26 +43,27 @@ namespace FastTunnel.Core.Handlers.Server foreach (var item in requet.Webs) { var hostName = $"{item.SubDomain}.{server.ServerOption.CurrentValue.WebDomain}".Trim(); - var info = new WebInfo { Socket = client, WebConfig = item }; + var info = new WebInfo { Socket = client.webSocket, WebConfig = item }; - _logger.LogDebug($"new domain '{hostName}'"); + logger.LogDebug($"new domain '{hostName}'"); server.WebList.AddOrUpdate(hostName, info, (key, oldInfo) => { return info; }); (proxyConfig as InMemoryConfigProvider).AddWeb(hostName); - await client.SendCmdAsync(MessageType.Log, $" HTTP | http://{hostName} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); + await client.webSocket.SendCmdAsync(MessageType.Log, $" HTTP | http://{hostName}:{client.ConnectionPort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); + client.AddWeb(info); if (item.WWW != null) { foreach (var www in item.WWW) { // TODO:validateDomain - _logger.LogInformation($"WWW {www}"); + logger.LogInformation($"WWW {www}"); server.WebList.AddOrUpdate(www, info, (key, oldInfo) => { return info; }); (proxyConfig as InMemoryConfigProvider).AddWeb(www); - await client.SendCmdAsync(MessageType.Log, $" HTTP | http://{www} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); - + await client.webSocket.SendCmdAsync(MessageType.Log, $" HTTP | http://{www}:{client.ConnectionPort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); + client.AddWeb(info); } } } @@ -81,44 +82,47 @@ namespace FastTunnel.Core.Handlers.Server ForwardInfo old; if (server.ForwardList.TryGetValue(item.RemotePort, out old)) { - _logger.LogDebug($"Remove Listener {old.Listener.ListenIp}:{old.Listener.ListenPort}"); + logger.LogDebug($"Remove Listener {old.Listener.ListenIp}:{old.Listener.ListenPort}"); old.Listener.Stop(); server.ForwardList.TryRemove(item.RemotePort, out ForwardInfo _); } // TODO: 客户端离线时销毁 - var ls = new PortProxyListener("0.0.0.0", item.RemotePort, _logger, client); - ls.Start(new ForwardDispatcher(_logger, server, item)); + var ls = new PortProxyListener("0.0.0.0", item.RemotePort, logger, client.webSocket); + ls.Start(new ForwardDispatcher(logger, server, item)); + + var forwardInfo = new ForwardInfo { Listener = ls, Socket = client.webSocket, SSHConfig = item }; // TODO: 客户端离线时销毁 - server.ForwardList.TryAdd(item.RemotePort, new ForwardInfo { Listener = ls, Socket = client, SSHConfig = item }); - _logger.LogDebug($"SSH proxy success: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}"); + server.ForwardList.TryAdd(item.RemotePort, forwardInfo); + logger.LogDebug($"SSH proxy success: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}"); - await client.SendCmdAsync(MessageType.Log, $" TCP | {server.ServerOption.CurrentValue.WebDomain}:{item.RemotePort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); + client.AddForward(forwardInfo); + await client.webSocket.SendCmdAsync(MessageType.Log, $" TCP | {server.ServerOption.CurrentValue.WebDomain}:{item.RemotePort} => {item.LocalIp}:{item.LocalPort}", CancellationToken.None); } catch (Exception ex) { - _logger.LogError($"SSH proxy error: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}"); - _logger.LogError(ex.Message); - await client.SendCmdAsync(MessageType.Log, ex.Message, CancellationToken.None); + logger.LogError($"SSH proxy error: {item.RemotePort} => {item.LocalIp}:{item.LocalPort}"); + logger.LogError(ex.Message); + await client.webSocket.SendCmdAsync(MessageType.Log, ex.Message, CancellationToken.None); continue; } } } else { - await client.SendCmdAsync(MessageType.Log, TunnelResource.ForwardDisabled, CancellationToken.None); + await client.webSocket.SendCmdAsync(MessageType.Log, TunnelResource.ForwardDisabled, CancellationToken.None); } } if (!hasTunnel) - await client.SendCmdAsync(MessageType.Log, TunnelResource.NoTunnel, CancellationToken.None); + await client.webSocket.SendCmdAsync(MessageType.Log, TunnelResource.NoTunnel, CancellationToken.None); } - public virtual async Task HandlerMsg(FastTunnelServer server, WebSocket client, string content) + public async Task HandlerMsg(FastTunnelServer fastTunnelServer, TunnelClient tunnelClient, string lineCmd) { - var msg = JsonSerializer.Deserialize(content); - await HandleLoginAsync(server, client, msg); + var msg = JsonSerializer.Deserialize(lineCmd); + await HandleLoginAsync(fastTunnelServer, tunnelClient, msg); return NeedRecive; } } diff --git a/FastTunnel.Core/Models/TunnelClient.cs b/FastTunnel.Core/Models/TunnelClient.cs index 3cdcedd..705dfcc 100644 --- a/FastTunnel.Core/Models/TunnelClient.cs +++ b/FastTunnel.Core/Models/TunnelClient.cs @@ -14,11 +14,19 @@ namespace FastTunnel.Core.Models { public class TunnelClient { - readonly WebSocket webSocket; + public WebSocket webSocket { get; private set; } + + /// + /// 服务端端口号 + /// + public int ConnectionPort { get; set; } + readonly FastTunnelServer fastTunnelServer; readonly ILoginHandler loginHandler; public IPAddress RemoteIpAddress { get; private set; } + IList webInfos = new List(); + IList> forwardInfos = new List>(); public TunnelClient(WebSocket webSocket, FastTunnelServer fastTunnelServer, ILoginHandler loginHandler, IPAddress remoteIpAddress) { @@ -28,6 +36,16 @@ namespace FastTunnel.Core.Models this.RemoteIpAddress = remoteIpAddress; } + internal void AddWeb(WebInfo info) + { + webInfos.Add(info); + } + + internal void AddForward(ForwardInfo forwardInfo) + { + forwardInfos.Add(forwardInfo); + } + public async Task ReviceAsync(CancellationToken cancellationToken) { var buffer = new byte[FastTunnelConst.MAX_CMD_LENGTH]; @@ -41,19 +59,18 @@ namespace FastTunnel.Core.Models foreach (var item in cmds) { - if (!await HandleCmdAsync(webSocket, item)) + if (!await HandleCmdAsync(this, item)) { return; }; } } } - - private async Task HandleCmdAsync(WebSocket webSocket, string lineCmd) + private async Task HandleCmdAsync(TunnelClient tunnelClient, string lineCmd) { try { - return await loginHandler.HandlerMsg(fastTunnelServer, webSocket, lineCmd.Substring(1)); + return await loginHandler.HandlerMsg(fastTunnelServer, tunnelClient, lineCmd.Substring(1)); } catch (Exception ex) { @@ -64,6 +81,7 @@ namespace FastTunnel.Core.Models internal void Logout() { + // TODO: } } } diff --git a/FastTunnel.Server/Properties/launchSettings.json b/FastTunnel.Server/Properties/launchSettings.json index ea64ade..64fd206 100644 --- a/FastTunnel.Server/Properties/launchSettings.json +++ b/FastTunnel.Server/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -13,18 +13,23 @@ "commandName": "Project", "launchBrowser": true, "launchUrl": "http://localhost:1270/swagger", - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" + }, + "IIS Express": { + "commandName": "IISExpress", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, - "IIS Express": { - "commandName": "IISExpress", - //"launchBrowser": true, - "launchUrl": "", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "publishAllPorts": true, + "useSSL": true } } -} +} \ No newline at end of file