优化代码

This commit is contained in:
SpringHgui 2021-07-04 22:36:12 +08:00
parent 1683862fee
commit 460682dde5
14 changed files with 92 additions and 147 deletions

View File

@ -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
// Aip
"WWW": [ "www.abc.com", "test111.test.cc" ]
// Aip
// "WWW": [ "www.abc.com", "test111.test.cc" ]
}
],
/**
* ssh穿ssh访
* 访 #ssh -oPort=12701 {root}@{ServerAddr}
* ServerAddr iproot
* ssh穿ssh访/mysql/erp/
* linux#ssh -oPort=12701 {root}@{ServerAddr} ServerAddr iproot
* 访访
*/
"SSH": [
{
"LocalIp": "127.0.0.1",
"LocalPort": 88,
"LocalIp": "test.cc",
"LocalPort": 8090,
"RemotePort": 9999
},
{
"LocalIp": "192.168.0.91",
"LocalPort": 3389, // windows3389
"RemotePort": 1274
"RemotePort": 1274 // 访 ip:1274 window
}
]
}

View File

@ -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<AppSettings>().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()

View File

@ -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);
}
}
}
}

View File

@ -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<JObject> Msg)
{
var request = Msg.Content.ToObject<NewCustomerMassage>();
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<SwapMassage> { 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();
}
}
}

View File

@ -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<JObject> Msg)
{
var request_ssh = Msg.Content.ToObject<NewSSHRequest>();
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<SwapMassage> { 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();
}
}
}

View File

@ -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
{

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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
/// <summary>
/// 异步数据交换
/// 存在大文件下载时,导致栈溢出的问题
/// </summary>
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)
{

View File

@ -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;

View File

@ -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
{
/// <summary>
/// 前置操作
/// </summary>
/// <param name="fun"></param>
/// <returns></returns>
ISocketSwap BeforeSwap(Action fun);
void StartSwap();
}
}

View File

@ -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)
{

View File

@ -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"
}
}
}
}

View File

@ -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": [],
// SSHSSH.false