diff --git a/FastTunnel.Client/appsettings.json b/FastTunnel.Client/appsettings.json index 4e1e3c0..5ef2270 100644 --- a/FastTunnel.Client/appsettings.json +++ b/FastTunnel.Client/appsettings.json @@ -9,10 +9,10 @@ }, "ClientSettings": { "Server": { - // 服务端公网ip, 对应服务端配置文件的 BindAddr,支持域名 - // "ServerAddr": "my.com", - "ServerAddr": "127.0.0.1", - // 服务端通信端口,对应服务端配置文件的 BindPort + // 服务端ip/域名 + "ServerAddr": "test.cc", + //"ServerAddr": "127.0.0.1", + // 服务端监听的通信端口 "ServerPort": 1271 }, "Webs": [ @@ -23,29 +23,29 @@ // 站点监听的端口号 "LocalPort": 9529, - // 子域名, 访问本站点时的url为 http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/ - "SubDomain": "test", // test.test.cc + // 子域名, 访问本站点时的url为 http://{SubDomain}.{WebDomain}:{WebProxyPort}/ + "SubDomain": "test" // test.test.cc - // 个人域名(需要解析域名A记录至服务的ip地址) - "WWW": [ "www.abc.com", "test111.test.cc" ] + // 附加域名,需要解析域名A记录至服务的ip地址) + // "WWW": [ "www.abc.com", "test111.test.cc" ] } ], /** - * ssh穿透,ssh访问内网主机 - * 访问方式 #ssh -oPort=12701 {root}@{ServerAddr} - * ServerAddr 填入服务端ip,root对应内网用户名 + * ssh穿透,ssh访问内网主机/mysql/erp/等任何服务 + * 远程linux示例:#ssh -oPort=12701 {root}@{ServerAddr} ServerAddr 填入服务端ip,root对应内网用户名 + * 通过服务端返回的访问方式进行访问即可 */ "SSH": [ { - "LocalIp": "127.0.0.1", - "LocalPort": 88, + "LocalIp": "test.cc", + "LocalPort": 8090, "RemotePort": 9999 }, { "LocalIp": "192.168.0.91", "LocalPort": 3389, // windows远程桌面端口为3389 - "RemotePort": 1274 + "RemotePort": 1274 // 访问 服务端ip:1274 即可实现远程window桌面 } ] } diff --git a/FastTunnel.Core/Client/FastTunnelClient.cs b/FastTunnel.Core/Client/FastTunnelClient.cs index 52eeb22..f2056b4 100644 --- a/FastTunnel.Core/Client/FastTunnelClient.cs +++ b/FastTunnel.Core/Client/FastTunnelClient.cs @@ -3,8 +3,6 @@ using Newtonsoft.Json.Linq; using FastTunnel.Core.Config; using FastTunnel.Core.Models; using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; @@ -15,6 +13,7 @@ using Microsoft.Extensions.Logging; using FastTunnel.Core.Handlers.Client; using Microsoft.Extensions.Configuration; using FastTunnel.Core.Server; +using FastTunnel.Core.Sockets; namespace FastTunnel.Core.Client { @@ -63,10 +62,13 @@ namespace FastTunnel.Core.Client private void reConn() { Close(); - while (true) + + do { try { + Thread.Sleep(reTrySpan); + _logger.LogInformation("登录重试..."); _client = lastLogin.Invoke(); @@ -75,9 +77,8 @@ namespace FastTunnel.Core.Client catch (Exception ex) { _logger.LogError(ex.Message); - Thread.Sleep(reTrySpan); } - } + } while (true); connSuccessAsync(); } @@ -120,7 +121,6 @@ namespace FastTunnel.Core.Client { _logger.LogError(ex.Message); - Thread.Sleep(reTrySpan); reConn(); return; } @@ -133,20 +133,23 @@ namespace FastTunnel.Core.Client var ClientConfig = _configuration.Get().ClientSettings; Server = ClientConfig.Server; - Connecter _client; + DnsSocket _client = null; _logger.LogInformation($"正在连接服务端 {Server.ServerAddr}:{Server.ServerPort}"); try { // 连接到的目标IP - _client = new Connecter(Server.ServerAddr, Server.ServerPort); + if (_client == null) + { + _client = new DnsSocket(Server.ServerAddr, Server.ServerPort); + } + _client.Connect(); _logger.LogInformation("连接成功"); } catch (Exception) { - Thread.Sleep(5000); throw; } @@ -172,13 +175,13 @@ namespace FastTunnel.Core.Client try { - _client.Shutdown(SocketShutdown.Both); + _client?.Shutdown(SocketShutdown.Both); } catch (Exception) { } - _client.Close(); + _client?.Close(); } private async Task connSuccessAsync() diff --git a/FastTunnel.Core/DataReciver.cs b/FastTunnel.Core/DataReciver.cs deleted file mode 100644 index d5a1bee..0000000 --- a/FastTunnel.Core/DataReciver.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Sockets; -using System.Text; - -namespace FastTunnel.Core -{ - public delegate void OnCompleteHandler(DataReciver send, byte[] buffer, int index, int count); - public delegate void OnError(DataReciver send, SocketAsyncEventArgs e); - public delegate void OnConnectionReset(DataReciver send, Socket socket, SocketAsyncEventArgs e); - - public class DataReciver - { - private Socket m_client; - - public event OnCompleteHandler OnComplete; - public event OnError OnError; - public event OnConnectionReset OnReset; - - byte[] buffer = new byte[1024 * 1024]; - SocketAsyncEventArgs rcv_event; - - public Socket Socket => m_client; - - public DataReciver(Socket client) - { - this.m_client = client; - - rcv_event = new SocketAsyncEventArgs(); - rcv_event.Completed += Rcv_event_Completed; - rcv_event.SetBuffer(buffer); - } - - public void ReciveOneAsync() - { - var willRaise = m_client.ReceiveAsync(rcv_event); - if (!willRaise) - { - Process(rcv_event); - } - } - - private void Rcv_event_Completed(object sender, SocketAsyncEventArgs e) - { - Process(e); - } - - private void Process(SocketAsyncEventArgs e) - { - if (e.SocketError == SocketError.Success) - { - if (e.BytesTransferred == 0) - { - - } - else - { - OnComplete?.Invoke(this, buffer, e.Offset, e.BytesTransferred); - } - } - else if (e.SocketError == SocketError.ConnectionReset) - { - // 断线 - OnReset?.Invoke(this, m_client, e); - } - else - { - OnError?.Invoke(this, e); - } - } - } -} diff --git a/FastTunnel.Core/Handlers/Client/HttpRequestHandler.cs b/FastTunnel.Core/Handlers/Client/HttpRequestHandler.cs index 80d544d..9350ba2 100644 --- a/FastTunnel.Core/Handlers/Client/HttpRequestHandler.cs +++ b/FastTunnel.Core/Handlers/Client/HttpRequestHandler.cs @@ -9,6 +9,7 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using FastTunnel.Core.Sockets; namespace FastTunnel.Core.Handlers.Client { @@ -17,12 +18,12 @@ namespace FastTunnel.Core.Handlers.Client public void HandlerMsg(FastTunnelClient cleint, Message Msg) { var request = Msg.Content.ToObject(); - var connecter = new Connecter(cleint.Server.ServerAddr, cleint.Server.ServerPort); + var connecter = new DnsSocket(cleint.Server.ServerAddr, cleint.Server.ServerPort); connecter.Connect(); connecter.Send(new Message { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request.MsgId) }); - var localConnecter = new Connecter(request.WebConfig.LocalIp, request.WebConfig.LocalPort); + var localConnecter = new DnsSocket(request.WebConfig.LocalIp, request.WebConfig.LocalPort); try { @@ -59,7 +60,7 @@ namespace FastTunnel.Core.Handlers.Client throw; } - new SocketSwap(connecter.Socket, localConnecter.Socket).StartSwapAsync(); + new SocketSwap(connecter.Socket, localConnecter.Socket).StartSwap(); } } } diff --git a/FastTunnel.Core/Handlers/Client/NewSSHHandler.cs b/FastTunnel.Core/Handlers/Client/NewSSHHandler.cs index e086684..b2dbd17 100644 --- a/FastTunnel.Core/Handlers/Client/NewSSHHandler.cs +++ b/FastTunnel.Core/Handlers/Client/NewSSHHandler.cs @@ -5,6 +5,7 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Text; +using FastTunnel.Core.Sockets; namespace FastTunnel.Core.Handlers.Client { @@ -13,14 +14,14 @@ namespace FastTunnel.Core.Handlers.Client public void HandlerMsg(FastTunnelClient cleint, Message Msg) { var request_ssh = Msg.Content.ToObject(); - var connecter_ssh = new Connecter(cleint.Server.ServerAddr, cleint.Server.ServerPort); + var connecter_ssh = new DnsSocket(cleint.Server.ServerAddr, cleint.Server.ServerPort); connecter_ssh.Connect(); connecter_ssh.Send(new Message { MessageType = MessageType.C_SwapMsg, Content = new SwapMassage(request_ssh.MsgId) }); - var localConnecter_ssh = new Connecter(request_ssh.SSHConfig.LocalIp, request_ssh.SSHConfig.LocalPort); + var localConnecter_ssh = new DnsSocket(request_ssh.SSHConfig.LocalIp, request_ssh.SSHConfig.LocalPort); localConnecter_ssh.Connect(); - new SocketSwap(connecter_ssh.Socket, localConnecter_ssh.Socket).StartSwapAsync(); + new SocketSwap(connecter_ssh.Socket, localConnecter_ssh.Socket).StartSwap(); } } } diff --git a/FastTunnel.Core/Handlers/Server/SwapMessageHandler.cs b/FastTunnel.Core/Handlers/Server/SwapMessageHandler.cs index 04e2a53..61790b3 100644 --- a/FastTunnel.Core/Handlers/Server/SwapMessageHandler.cs +++ b/FastTunnel.Core/Handlers/Server/SwapMessageHandler.cs @@ -1,6 +1,7 @@ using FastTunnel.Core.Client; using FastTunnel.Core.Extensions; using FastTunnel.Core.Models; +using FastTunnel.Core.Sockets; using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; using System; @@ -35,7 +36,7 @@ namespace FastTunnel.Core.Handlers.Server { if (request.Buffer != null) client.Send(request.Buffer); }) - .StartSwapAsync(); + .StartSwap(); } else { diff --git a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs b/FastTunnel.Core/Services/ServiceFastTunnelClient.cs index ce162ec..36995e2 100644 --- a/FastTunnel.Core/Services/ServiceFastTunnelClient.cs +++ b/FastTunnel.Core/Services/ServiceFastTunnelClient.cs @@ -23,7 +23,7 @@ namespace FastTunnel.Core.Services _logger = logger; _fastTunnelClient = fastTunnelClient; - AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; + //AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } diff --git a/FastTunnel.Core/Services/ServiceFastTunnelServer.cs b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs index fbd9260..fb1d228 100644 --- a/FastTunnel.Core/Services/ServiceFastTunnelServer.cs +++ b/FastTunnel.Core/Services/ServiceFastTunnelServer.cs @@ -31,7 +31,7 @@ namespace FastTunnel.Core.Services _logger = logger; _authenticationFilter = authenticationFilter; _server = fastTunnelServer; - AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; + //AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; } diff --git a/FastTunnel.Core/AsyncSocketSwap.cs b/FastTunnel.Core/Sockets/AsyncSocketSwap.cs similarity index 87% rename from FastTunnel.Core/AsyncSocketSwap.cs rename to FastTunnel.Core/Sockets/AsyncSocketSwap.cs index 6b38947..0d5d10b 100644 --- a/FastTunnel.Core/AsyncSocketSwap.cs +++ b/FastTunnel.Core/Sockets/AsyncSocketSwap.cs @@ -1,4 +1,5 @@ -using System; +using FastTunnel.Core.Dispatchers; +using System; using System.Collections.Generic; using System.Net.Sockets; using System.Runtime.ExceptionServices; @@ -6,9 +7,13 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -namespace FastTunnel.Core +namespace FastTunnel.Core.Sockets { - public class AsyncSocketSwap + /// + /// 异步数据交换 + /// 存在大文件下载时,导致栈溢出的问题 + /// + public class AsyncSocketSwap : ISocketSwap { private Socket m_sockt1; private Socket m_sockt2; @@ -35,7 +40,7 @@ namespace FastTunnel.Core e2.SetBuffer(m_buffer, 512, 512); } - public AsyncSocketSwap BeforeSwap(Action fun) + public ISocketSwap BeforeSwap(Action fun) { if (m_swaping) throw new Exception("BeforeSwap must be invoked before StartSwap!"); @@ -44,7 +49,7 @@ namespace FastTunnel.Core return this; } - public void StartSwapAsync() + public void StartSwap() { try { @@ -133,20 +138,6 @@ namespace FastTunnel.Core } catch (Exception) { } socket.Close(); - - //try - //{ - // m_sockt1.Shutdown(SocketShutdown.Both); - //} - //catch (Exception) { } - //m_sockt1.Close(); - - //try - //{ - // m_sockt2.Shutdown(SocketShutdown.Both); - //} - //catch (Exception) { } - //m_sockt2.Close(); } catch (Exception ex) { diff --git a/FastTunnel.Core/Connecter.cs b/FastTunnel.Core/Sockets/DnsSocket.cs similarity index 89% rename from FastTunnel.Core/Connecter.cs rename to FastTunnel.Core/Sockets/DnsSocket.cs index 3474489..9567248 100644 --- a/FastTunnel.Core/Connecter.cs +++ b/FastTunnel.Core/Sockets/DnsSocket.cs @@ -6,16 +6,16 @@ using System.Net; using System.Net.Sockets; using System.Text; -namespace FastTunnel.Core +namespace FastTunnel.Core.Sockets { - public class Connecter + public class DnsSocket { private string _host; private int _port; public Socket Socket { get; set; } - public Connecter(string v1, int v2) + public DnsSocket(string v1, int v2) { this._host = v1; this._port = v2; diff --git a/FastTunnel.Core/Sockets/ISocketSwap.cs b/FastTunnel.Core/Sockets/ISocketSwap.cs new file mode 100644 index 0000000..6247f33 --- /dev/null +++ b/FastTunnel.Core/Sockets/ISocketSwap.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FastTunnel.Core.Sockets +{ + public interface ISocketSwap + { + /// + /// 前置操作 + /// + /// + /// + ISocketSwap BeforeSwap(Action fun); + + void StartSwap(); + } +} diff --git a/FastTunnel.Core/SocketSwap.cs b/FastTunnel.Core/Sockets/SocketSwap.cs similarity index 93% rename from FastTunnel.Core/SocketSwap.cs rename to FastTunnel.Core/Sockets/SocketSwap.cs index 85d87df..ef22524 100644 --- a/FastTunnel.Core/SocketSwap.cs +++ b/FastTunnel.Core/Sockets/SocketSwap.cs @@ -1,12 +1,13 @@ -using System; +using FastTunnel.Core.Dispatchers; +using System; using System.Collections.Generic; using System.Net.Sockets; using System.Text; using System.Threading; -namespace FastTunnel.Core +namespace FastTunnel.Core.Sockets { - public class SocketSwap + public class SocketSwap : ISocketSwap { private Socket _sockt1; private Socket _sockt2; @@ -25,7 +26,7 @@ namespace FastTunnel.Core _sockt2 = sockt2; } - public void StartSwapAsync() + public void StartSwap() { Swaped = true; ThreadPool.QueueUserWorkItem(swapCallback, new Channel @@ -111,7 +112,7 @@ namespace FastTunnel.Core } } - internal SocketSwap BeforeSwap(Action fun) + public ISocketSwap BeforeSwap(Action fun) { if (Swaped) { diff --git a/FastTunnel.Server/Properties/launchSettings.json b/FastTunnel.Server/Properties/launchSettings.json index 73772f4..cb0f412 100644 --- a/FastTunnel.Server/Properties/launchSettings.json +++ b/FastTunnel.Server/Properties/launchSettings.json @@ -9,14 +9,6 @@ } }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - //"launchBrowser": true, - "launchUrl": "", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, "FastTunnel.Server": { "commandName": "Project", //"launchBrowser": true, @@ -25,6 +17,14 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "IIS Express": { + "commandName": "IISExpress", + //"launchBrowser": true, + "launchUrl": "", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } } } } diff --git a/FastTunnel.Server/config/appsettings.json b/FastTunnel.Server/config/appsettings.json index 7aa775a..bdfe510 100644 --- a/FastTunnel.Server/config/appsettings.json +++ b/FastTunnel.Server/config/appsettings.json @@ -9,22 +9,21 @@ }, "AllowedHosts": "*", "ServerSettings": { - // ѡ Ĭֵ + // ַ "BindAddr": "0.0.0.0", - // ѡ Ĭֵ + // ˿ "BindPort": 1271, - // Զweb͸ + // "WebDomain": "test.cc", - // Ķ˿ں, ԶվʱurlΪ http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/ - // web͸ + // Http˿, ԶվʱurlΪ http://{SubDomain}.{WebDomain}:{WebProxyPort}/ "WebProxyPort": 1270, // ѡngixnʡĶ˿ںŽз "WebHasNginxProxy": false, - // ѡʰڰipܾ + // ѡʰڰipܾΪʱȨ޷ "WebAllowAccessIps": [], // ѡǷSSHú󲻴SSHͶ˿ת.Ĭfalse