mirror of
https://github.com/FastTunnel/FastTunnel.git
synced 2025-02-08 02:39:29 +08:00
修改socket的关闭
This commit is contained in:
parent
30491fe24e
commit
958d69b196
|
@ -48,12 +48,12 @@
|
|||
{
|
||||
"LocalIp": "127.0.0.1",
|
||||
"LocalPort": 22,
|
||||
"RemotePort": 12701
|
||||
"RemotePort": 1273
|
||||
},
|
||||
{
|
||||
"LocalIp": "127.0.0.1",
|
||||
"LocalIp": "192.168.0.91",
|
||||
"LocalPort": 3389, // windows远程桌面端口为3389
|
||||
"RemotePort": 12702
|
||||
"RemotePort": 1274
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using FastTunnel.Core.Logger;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
@ -9,42 +10,63 @@ namespace FastTunnel.Core
|
|||
{
|
||||
public class Listener<T>
|
||||
{
|
||||
private string _ip;
|
||||
private int _port;
|
||||
ILogger _logerr;
|
||||
|
||||
public string IP { get; set; }
|
||||
|
||||
public int Port { get; set; }
|
||||
|
||||
Action<Socket, T> handler;
|
||||
Socket socket;
|
||||
Socket ls;
|
||||
T _data;
|
||||
|
||||
public Listener(string ip, int port, Action<Socket, T> acceptCustomerHandler, T data)
|
||||
|
||||
public Listener(string ip, int port, ILogger logerr, Action<Socket, T> acceptCustomerHandler, T data)
|
||||
{
|
||||
_logerr = logerr;
|
||||
_data = data;
|
||||
this._ip = ip;
|
||||
this._port = port;
|
||||
this.IP = ip;
|
||||
this.Port = port;
|
||||
handler = acceptCustomerHandler;
|
||||
|
||||
IPAddress ipa = IPAddress.Parse(_ip);
|
||||
IPEndPoint ipe = new IPEndPoint(ipa, _port);
|
||||
IPAddress ipa = IPAddress.Parse(IP);
|
||||
IPEndPoint ipe = new IPEndPoint(ipa, Port);
|
||||
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
socket.Bind(ipe);
|
||||
ls = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
ls.Bind(ipe);
|
||||
}
|
||||
|
||||
public void Listen()
|
||||
{
|
||||
socket.Listen(100);
|
||||
ls.Listen(100);
|
||||
ThreadPool.QueueUserWorkItem((state) =>
|
||||
{
|
||||
var _socket = state as Socket;
|
||||
|
||||
while (true)
|
||||
{
|
||||
Socket client = _socket.Accept();
|
||||
string point = client.RemoteEndPoint.ToString();
|
||||
Console.WriteLine($"收到请求 {point}");
|
||||
try
|
||||
{
|
||||
var client = _socket.Accept();
|
||||
|
||||
ThreadPool.QueueUserWorkItem(ReceiveCustomer, client);
|
||||
string point = client.RemoteEndPoint.ToString();
|
||||
Console.WriteLine($"收到请求 {point}");
|
||||
|
||||
ThreadPool.QueueUserWorkItem(ReceiveCustomer, client);
|
||||
}
|
||||
catch (SocketException ex)
|
||||
{
|
||||
_logerr.Error(ex.Message);
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logerr.Error(ex);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}, socket);
|
||||
}, ls);
|
||||
}
|
||||
|
||||
private void ReceiveCustomer(object state)
|
||||
|
@ -52,5 +74,18 @@ namespace FastTunnel.Core
|
|||
var client = state as Socket;
|
||||
handler.Invoke(client, _data);
|
||||
}
|
||||
|
||||
public void ShutdownAndClose()
|
||||
{
|
||||
try
|
||||
{
|
||||
ls.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ls.Close();
|
||||
_logerr.Debug("Listener cloed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,14 +38,14 @@ namespace FastTunnel.Core.Server
|
|||
|
||||
private void ListenFastTunnelClient()
|
||||
{
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.BindPort, ReceiveClient, null);
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.BindPort, _logger, ReceiveClient, null);
|
||||
listener.Listen();
|
||||
_logger.Debug($"监听客户端 -> {serverSettings.BindAddr}:{serverSettings.BindPort}");
|
||||
}
|
||||
|
||||
private void ListenCustomer()
|
||||
{
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.ProxyPort_HTTP, ReceiveCustomer, null);
|
||||
var listener = new Listener<object>(serverSettings.BindAddr, serverSettings.ProxyPort_HTTP, _logger, ReceiveCustomer, null);
|
||||
listener.Listen();
|
||||
|
||||
_logger.Debug($"监听HTTP -> {serverSettings.BindAddr}:{serverSettings.ProxyPort_HTTP}");
|
||||
|
@ -190,26 +190,44 @@ namespace FastTunnel.Core.Server
|
|||
{
|
||||
foreach (var item in requet.ClientConfig.SSH)
|
||||
{
|
||||
if (SSHList.ContainsKey(item.RemotePort))
|
||||
SSHList.Remove(item.RemotePort);
|
||||
|
||||
try
|
||||
{
|
||||
var ls = new Listener<SSHHandlerArg>("0.0.0.0", item.RemotePort, SSHHandler, new SSHHandlerArg { LocalClient = client, SSHConfig = item });
|
||||
if (item.RemotePort.Equals(serverSettings.BindPort))
|
||||
{
|
||||
_logger.Error($"RemotePort can not be same with BindPort: {item.RemotePort}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.RemotePort.Equals(serverSettings.ProxyPort_HTTP))
|
||||
{
|
||||
_logger.Error($"RemotePort can not be same with ProxyPort_HTTP: {item.RemotePort}");
|
||||
continue;
|
||||
}
|
||||
|
||||
SSHInfo<SSHHandlerArg> old;
|
||||
if (SSHList.TryGetValue(item.RemotePort, out old))
|
||||
{
|
||||
_logger.Debug($"Remove Listener {old.Listener.IP}:{old.Listener.Port}");
|
||||
old.Listener.ShutdownAndClose();
|
||||
SSHList.Remove(item.RemotePort);
|
||||
}
|
||||
|
||||
var ls = new Listener<SSHHandlerArg>("0.0.0.0", item.RemotePort, _logger, SSHHandler, new SSHHandlerArg { LocalClient = client, SSHConfig = item });
|
||||
ls.Listen();
|
||||
|
||||
// listen success
|
||||
SSHList.Add(item.RemotePort, new SSHInfo<SSHHandlerArg> { Listener = ls, Socket = client, SSHConfig = item });
|
||||
_logger.Debug($"SSH proxy success on {item.RemotePort} -> {item.LocalIp}:{item.LocalPort}");
|
||||
_logger.Debug($"SSH proxy success: {item.RemotePort} -> {item.LocalIp}:{item.LocalPort}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error($"SSH proxy error on {item.RemotePort} -> {item.LocalIp}:{item.LocalPort}");
|
||||
_logger.Error($"SSH proxy error: {item.RemotePort} -> {item.LocalIp}:{item.LocalPort}");
|
||||
_logger.Error(ex);
|
||||
client.Send(new Message<string> { MessageType = MessageType.Error, Content = ex.Message });
|
||||
continue;
|
||||
}
|
||||
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"Tunnel For ProxyPort is OK: {requet.ClientConfig.Common.ServerAddr}:{item.RemotePort}->{item.LocalIp}:{item.LocalPort}" });
|
||||
client.Send(new Message<string> { MessageType = MessageType.Info, Content = $"TunnelForSSH is OK: {requet.ClientConfig.Common.ServerAddr}:{item.RemotePort}->{item.LocalIp}:{item.LocalPort}" });
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -58,7 +58,14 @@ namespace FastTunnel.Core
|
|||
{
|
||||
if (chanel.Receive.Connected)
|
||||
{
|
||||
chanel.Receive.Close();
|
||||
try
|
||||
{
|
||||
chanel.Receive.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
finally
|
||||
{
|
||||
chanel.Receive.Close();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -70,10 +77,30 @@ namespace FastTunnel.Core
|
|||
catch (Exception)
|
||||
{
|
||||
if (chanel.Receive.Connected)
|
||||
chanel.Receive.Close();
|
||||
{
|
||||
try
|
||||
{
|
||||
chanel.Receive.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
finally
|
||||
{
|
||||
chanel.Receive.Close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (chanel.Send.Connected)
|
||||
chanel.Send.Close();
|
||||
{
|
||||
try
|
||||
{
|
||||
chanel.Send.Shutdown(SocketShutdown.Both);
|
||||
}
|
||||
finally
|
||||
{
|
||||
chanel.Send.Close();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user