mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 10:59:31 +08:00
优化代码
This commit is contained in:
parent
1683862fee
commit
460682dde5
|
@ -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桌面
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
{
|
|
@ -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;
|
20
FastTunnel.Core/Sockets/ISocketSwap.cs
Normal file
20
FastTunnel.Core/Sockets/ISocketSwap.cs
Normal 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();
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
{
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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。
|
||||
|
|
Loading…
Reference in New Issue
Block a user