mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
socket使用异步方式
This commit is contained in:
parent
6908dd2469
commit
2314a553c5
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Default": "Debug",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ using System.Threading.Tasks;
|
|||
|
||||
namespace FastTunnel.Core
|
||||
{
|
||||
public class AsyncListener<T> : IListener<T>
|
||||
public class AsyncListener : IListener
|
||||
{
|
||||
ILogger _logerr;
|
||||
|
||||
|
@ -17,60 +17,101 @@ namespace FastTunnel.Core
|
|||
|
||||
public int Port { get; set; }
|
||||
|
||||
Action<Socket, T> receiveClient;
|
||||
Socket listener;
|
||||
T _data;
|
||||
Action<Socket> receiveClient;
|
||||
Socket listenSocket;
|
||||
|
||||
bool Shutdown { get; set; }
|
||||
|
||||
// Thread signal.
|
||||
ManualResetEvent allDone = new ManualResetEvent(false);
|
||||
|
||||
public AsyncListener(string ip, int port, ILogger logerr, T data)
|
||||
public AsyncListener(string ip, int port, ILogger logerr)
|
||||
{
|
||||
_logerr = logerr;
|
||||
_data = data;
|
||||
this.IP = ip;
|
||||
this.Port = port;
|
||||
|
||||
IPAddress ipa = IPAddress.Parse(IP);
|
||||
IPEndPoint localEndPoint = new IPEndPoint(ipa, Port);
|
||||
|
||||
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
listener.Bind(localEndPoint);
|
||||
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
listenSocket.Bind(localEndPoint);
|
||||
}
|
||||
|
||||
public void Listen(Action<Socket, T> receiveClient)
|
||||
public void Listen(Action<Socket> receiveClient)
|
||||
{
|
||||
// example https://docs.microsoft.com/en-us/dotnet/framework/network-programming/asynchronous-server-socket-example
|
||||
// Bind the socket to the local endpoint and listen for incoming connections.
|
||||
this.receiveClient = receiveClient;
|
||||
|
||||
Task.Run(() =>
|
||||
listenSocket.Listen(100);
|
||||
|
||||
// post accepts on the listening socket
|
||||
StartAccept(null);
|
||||
|
||||
//Task.Run(() =>
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// listenSocket.Listen(100);
|
||||
|
||||
// while (true)
|
||||
// {
|
||||
// // Set the event to nonsignaled state.
|
||||
// allDone.Reset();
|
||||
|
||||
// // Start an asynchronous socket to listen for connections.
|
||||
// _logerr.LogDebug($"Waiting for a connection {listenSocket.Handle}");
|
||||
// listenSocket.BeginAccept(new AsyncCallback(AcceptCallback), listenSocket);
|
||||
|
||||
// // Wait until a connection is made before continuing.
|
||||
// allDone.WaitOne();
|
||||
// }
|
||||
// }
|
||||
// catch (Exception e)
|
||||
// {
|
||||
// Console.WriteLine(e.ToString());
|
||||
// }
|
||||
//});
|
||||
}
|
||||
|
||||
public void StartAccept(SocketAsyncEventArgs acceptEventArg)
|
||||
{
|
||||
if (acceptEventArg == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
listener.Listen(100);
|
||||
acceptEventArg = new SocketAsyncEventArgs();
|
||||
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
|
||||
}
|
||||
else
|
||||
{
|
||||
// socket must be cleared since the context object is being reused
|
||||
acceptEventArg.AcceptSocket = null;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
// Set the event to nonsignaled state.
|
||||
allDone.Reset();
|
||||
bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
|
||||
if (!willRaiseEvent)
|
||||
{
|
||||
ProcessAccept(acceptEventArg);
|
||||
}
|
||||
}
|
||||
|
||||
// Start an asynchronous socket to listen for connections.
|
||||
_logerr.LogDebug($"Waiting for a connection {listener.Handle}");
|
||||
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
|
||||
int m_numConnectedSockets;
|
||||
|
||||
// Wait until a connection is made before continuing.
|
||||
allDone.WaitOne();
|
||||
}
|
||||
private void ProcessAccept(SocketAsyncEventArgs e)
|
||||
{
|
||||
Interlocked.Increment(ref m_numConnectedSockets);
|
||||
Console.WriteLine("Client connection accepted. There are {0} clients connected to the server",
|
||||
m_numConnectedSockets);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
});
|
||||
var accept = e.AcceptSocket;
|
||||
|
||||
// Accept the next connection request
|
||||
StartAccept(e);
|
||||
|
||||
receiveClient.Invoke(accept);
|
||||
}
|
||||
|
||||
private void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
|
||||
{
|
||||
ProcessAccept(e);
|
||||
}
|
||||
|
||||
void AcceptCallback(IAsyncResult ar)
|
||||
|
@ -85,78 +126,7 @@ namespace FastTunnel.Core
|
|||
Socket listener = (Socket)ar.AsyncState;
|
||||
Socket handler = listener.EndAccept(ar);
|
||||
|
||||
// Create the state object.
|
||||
StateObject state = new StateObject();
|
||||
state.workSocket = handler;
|
||||
|
||||
receiveClient.Invoke(handler, _data);
|
||||
|
||||
//handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
|
||||
}
|
||||
|
||||
void ReadCallback(IAsyncResult ar)
|
||||
{
|
||||
String content = String.Empty;
|
||||
|
||||
// Retrieve the state object and the handler socket
|
||||
// from the asynchronous state object.
|
||||
StateObject state = (StateObject)ar.AsyncState;
|
||||
Socket handler = state.workSocket;
|
||||
|
||||
// Read data from the client socket.
|
||||
int bytesRead = handler.EndReceive(ar);
|
||||
|
||||
if (bytesRead > 0)
|
||||
{
|
||||
// There might be more data, so store the data received so far.
|
||||
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
|
||||
|
||||
// Check for end-of-file tag. If it is not there, read
|
||||
// more data.
|
||||
content = state.sb.ToString();
|
||||
if (content.IndexOf("<EOF>") > -1)
|
||||
{
|
||||
// All the data has been read from the
|
||||
// client. Display it on the console.
|
||||
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
|
||||
content.Length, content);
|
||||
// Echo the data back to the client.
|
||||
|
||||
Send(handler, content);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not all data received. Get more.
|
||||
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Send(Socket handler, String data)
|
||||
{
|
||||
// Convert the string data to byte data using ASCII encoding.
|
||||
byte[] byteData = Encoding.ASCII.GetBytes(data);
|
||||
|
||||
// Begin sending the data to the remote device.
|
||||
handler.BeginSend(byteData, 0, byteData.Length, 0,
|
||||
new AsyncCallback(SendCallback), handler);
|
||||
}
|
||||
|
||||
void SendCallback(IAsyncResult ar)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Retrieve the socket from the state object.
|
||||
Socket handler = (Socket)ar.AsyncState;
|
||||
|
||||
// Complete sending the data to the remote device.
|
||||
int bytesSent = handler.EndSend(ar);
|
||||
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
receiveClient.Invoke(handler);
|
||||
}
|
||||
|
||||
public void ShutdownAndClose()
|
||||
|
@ -164,27 +134,15 @@ namespace FastTunnel.Core
|
|||
Shutdown = true;
|
||||
try
|
||||
{
|
||||
listener.Shutdown(SocketShutdown.Both);
|
||||
listenSocket.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
finally
|
||||
{
|
||||
listener.Close();
|
||||
listenSocket.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class StateObject
|
||||
{
|
||||
// Client socket.
|
||||
public Socket workSocket = null;
|
||||
// Size of receive buffer.
|
||||
public const int BufferSize = 1024;
|
||||
// Receive buffer.
|
||||
public byte[] buffer = new byte[BufferSize];
|
||||
// Received data string.
|
||||
public StringBuilder sb = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
|
12
FastTunnel.Core/AsyncUserToken.cs
Normal file
12
FastTunnel.Core/AsyncUserToken.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
|
||||
namespace FastTunnel.Core
|
||||
{
|
||||
public class AsyncUserToken
|
||||
{
|
||||
public Socket Socket { get; set; }
|
||||
}
|
||||
}
|
|
@ -18,6 +18,6 @@ namespace FastTunnel.Core.Config
|
|||
|
||||
public bool WebHasNginxProxy { get; set; } = false;
|
||||
|
||||
public bool SSHEnabled { get; set; } = true;
|
||||
public bool SSHEnabled { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -231,6 +231,8 @@ namespace FastTunnel.Core.Core
|
|||
|
||||
var msgs = words.Split("\n");
|
||||
|
||||
_logger.LogDebug("recive from server:" + words);
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var item in msgs)
|
||||
|
|
|
@ -52,20 +52,20 @@ namespace FastTunnel.Core.Core
|
|||
|
||||
private void ListenFastTunnelClient()
|
||||
{
|
||||
IListener<object> listener = new AsyncListener<object>(_serverSettings.BindAddr, _serverSettings.BindPort, _logger, null);
|
||||
IListener listener = new AsyncListener(_serverSettings.BindAddr, _serverSettings.BindPort, _logger);
|
||||
listener.Listen(ReceiveClient);
|
||||
_logger.LogDebug($"监听客户端 -> {_serverSettings.BindAddr}:{_serverSettings.BindPort}");
|
||||
}
|
||||
|
||||
private void ListenCustomer()
|
||||
{
|
||||
var listener = new AsyncListener<object>(_serverSettings.BindAddr, _serverSettings.WebProxyPort, _logger, null);
|
||||
var listener = new AsyncListener(_serverSettings.BindAddr, _serverSettings.WebProxyPort, _logger);
|
||||
listener.Listen(ReceiveCustomer);
|
||||
|
||||
_logger.LogDebug($"监听HTTP -> {_serverSettings.BindAddr}:{_serverSettings.WebProxyPort}");
|
||||
}
|
||||
|
||||
void ReceiveCustomer(Socket client, object _)
|
||||
void ReceiveCustomer(Socket client)
|
||||
{
|
||||
|
||||
try
|
||||
|
@ -234,7 +234,7 @@ namespace FastTunnel.Core.Core
|
|||
byte[] buffer = new byte[1024 * 1024];
|
||||
string temp = string.Empty;
|
||||
|
||||
public void ReceiveClient(Socket client, object _)
|
||||
public void ReceiveClient(Socket client)
|
||||
{
|
||||
//定义byte数组存放从客户端接收过来的数据
|
||||
int length;
|
||||
|
@ -273,6 +273,8 @@ namespace FastTunnel.Core.Core
|
|||
words += temp;
|
||||
temp = string.Empty;
|
||||
|
||||
_logger.LogDebug($"revice from client: {words}");
|
||||
|
||||
try
|
||||
{
|
||||
int index = 0;
|
||||
|
@ -284,7 +286,7 @@ namespace FastTunnel.Core.Core
|
|||
if (firstIndex < 0)
|
||||
{
|
||||
temp += words;
|
||||
ReceiveClient(client, _);
|
||||
ReceiveClient(client);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -301,7 +303,7 @@ namespace FastTunnel.Core.Core
|
|||
|
||||
if (needRecive)
|
||||
{
|
||||
ReceiveClient(client, _);
|
||||
ReceiveClient(client);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<ApplicationIcon />
|
||||
<StartupObject />
|
||||
<PackageProjectUrl>https://github.com/SpringHgui/FastTunnel</PackageProjectUrl>
|
||||
|
|
|
@ -92,18 +92,15 @@ namespace FastTunnel.Core.Handlers
|
|||
server.SSHList.Remove(item.RemotePort);
|
||||
}
|
||||
|
||||
var ls = new AsyncListener<SSHHandlerArg>("0.0.0.0", item.RemotePort, _logger,
|
||||
var ls = new AsyncListener("0.0.0.0", item.RemotePort, _logger);
|
||||
|
||||
new SSHHandlerArg { LocalClient = client, SSHConfig = item });
|
||||
|
||||
ls.Listen((client, local) =>
|
||||
ls.Listen((_socket) =>
|
||||
{
|
||||
var msgid = Guid.NewGuid().ToString();
|
||||
local.LocalClient.Send(new Message<NewSSHRequest> { MessageType = MessageType.S_NewSSH, Content = new NewSSHRequest { MsgId = msgid, SSHConfig = local.SSHConfig } });
|
||||
|
||||
client.Send(new Message<NewSSHRequest> { MessageType = MessageType.S_NewSSH, Content = new NewSSHRequest { MsgId = msgid, SSHConfig = item } });
|
||||
server.newRequest.Add(msgid, new NewRequest
|
||||
{
|
||||
CustomerClient = client,
|
||||
CustomerClient = _socket,
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -5,13 +5,13 @@ using System.Text;
|
|||
|
||||
namespace FastTunnel.Core
|
||||
{
|
||||
public interface IListener<T>
|
||||
public interface IListener
|
||||
{
|
||||
string IP { get; set; }
|
||||
string IP { get; }
|
||||
|
||||
int Port { get; set; }
|
||||
int Port { get; }
|
||||
|
||||
void Listen(Action<Socket, T> receiveClient);
|
||||
void Listen(Action<Socket> receiveClient);
|
||||
|
||||
void ShutdownAndClose();
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ namespace FastTunnel.Core.Models
|
|||
public class SSHHandlerArg
|
||||
{
|
||||
public SSHConfig SSHConfig { get; internal set; }
|
||||
|
||||
public Socket LocalClient { get; internal set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,6 @@ namespace FastTunnel.Core.Models
|
|||
|
||||
public SSHConfig SSHConfig { get; set; }
|
||||
|
||||
public IListener<T> Listener { get; set; }
|
||||
public IListener Listener { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -1,21 +1,31 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Default": "Debug", // Information
|
||||
"Microsoft": "Error",
|
||||
"Microsoft.Hosting.Lifetime": "Error"
|
||||
}
|
||||
},
|
||||
"ServerSettings": {
|
||||
"BindAddr": "0.0.0.0", // 可选
|
||||
"BindPort": 1271, // 可选
|
||||
// 必选 默认值
|
||||
"BindAddr": "0.0.0.0",
|
||||
// 必选 默认值
|
||||
"BindPort": 1271,
|
||||
|
||||
"WebDomain": "test.cc", // 自定义域名web穿透必须
|
||||
// 自定义域名web穿透必须
|
||||
"WebDomain": "test.cc",
|
||||
|
||||
// 服务监听的端口号, 访问自定义域名站点时url为 http://{SubDomain}.{Domain}:{ProxyPort_HTTP}/
|
||||
"WebProxyPort": 1270, // web穿透必须
|
||||
"WebHasNginxProxy": false, // 可选,ngixn反向代理后可省略域名后的端口号进行访问
|
||||
"WebAllowAccessIps": [ "192.168.0.99" ],
|
||||
"SSHEnabled": false,
|
||||
// web穿透必须
|
||||
"WebProxyPort": 1270,
|
||||
|
||||
// 可选,ngixn反向代理后可省略域名后的端口号进行访问
|
||||
"WebHasNginxProxy": false,
|
||||
|
||||
// 可选,访问白名单,不在白名单的ip拒绝
|
||||
"WebAllowAccessIps": [ "192.168.0.99", "127.0.0.1" ],
|
||||
|
||||
// 可选,是否开启SSH,禁用后不处理SSH类型端口转发.默认false。
|
||||
"SSHEnabled": true
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<p align="center"><img src="images/logo.png" width="150" align=center /></p>
|
||||
|
||||
## FastTunnel -内网穿透利器
|
||||
## FastTunnel - 内网穿透
|
||||
[README](README.md) | [中文文档](README_zh.md)
|
||||
|
||||
***
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<p align="center"><img src="images/logo.png" width="150" align=center /></p>
|
||||
|
||||
## FastTunnel -内网穿透利器
|
||||
## FastTunnel - 跨平台内网穿透工具
|
||||
[README](README.md) | [中文文档](README_zh.md)
|
||||
|
||||
***
|
||||
|
|
Loading…
Reference in New Issue
Block a user