socket使用异步方式

This commit is contained in:
SpringHgui 2020-09-16 17:51:59 +08:00
parent 6908dd2469
commit b001c7aa02
11 changed files with 52 additions and 120 deletions

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace FastTunnel.Core
{
public class AsyncListener<T> : IListener<T>
public class AsyncListener : IListener
{
ILogger _logerr;
@ -17,19 +17,17 @@ namespace FastTunnel.Core
public int Port { get; set; }
Action<Socket, T> receiveClient;
Action<Socket> receiveClient;
Socket listener;
T _data;
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;
@ -40,10 +38,8 @@ namespace FastTunnel.Core
listener.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(() =>
@ -64,7 +60,6 @@ namespace FastTunnel.Core
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
@ -85,78 +80,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()
@ -175,16 +99,4 @@ namespace FastTunnel.Core
}
}
}
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();
}
}

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

View File

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

View File

@ -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;
@ -284,7 +284,7 @@ namespace FastTunnel.Core.Core
if (firstIndex < 0)
{
temp += words;
ReceiveClient(client, _);
ReceiveClient(client);
break;
}
@ -301,7 +301,7 @@ namespace FastTunnel.Core.Core
if (needRecive)
{
ReceiveClient(client, _);
ReceiveClient(client);
}
}
catch (Exception ex)

View File

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

View File

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

View File

@ -8,6 +8,7 @@ namespace FastTunnel.Core.Models
public class SSHHandlerArg
{
public SSHConfig SSHConfig { get; internal set; }
public Socket LocalClient { get; internal set; }
}
}

View File

@ -11,6 +11,6 @@ namespace FastTunnel.Core.Models
public SSHConfig SSHConfig { get; set; }
public IListener<T> Listener { get; set; }
public IListener Listener { get; set; }
}
}

View File

@ -7,15 +7,25 @@
}
},
"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访
// web穿
"WebProxyPort": 1270,
// ngixn访
"WebHasNginxProxy": false,
// 访ip
"WebAllowAccessIps": [ "192.168.0.99" ],
"SSHEnabled": false,
// SSHSSH.false
"SSHEnabled": true
}
}

View File

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

View File

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