private void TCP_Connect(object state) { TcpClient Tcp_Client = state as TcpClient; DataHandle.WriteLog($"接收来自{Tcp_Client.Client.RemoteEndPoint},当前线程数:{ThreadPool.ThreadCount}"); NetworkStream TCP_Stream = Tcp_Client.GetStream(); var State_VT = (Tcp_Client, TCP_Stream); TCP_Stream.BeginRead(Receive_Data, 0, Data_Size, new AsyncCallback(TCP_Receive), State_VT); }
/// <summary> /// 关闭UDP对象及TCP依赖 /// </summary> public void Close() { try { DataHandle.WriteLog($"已关闭{Client_Point}的UDP代理隧道"); UDP_Client.Close(); TCP_Client.GetStream().Close(); TCP_Client.Close(); } catch (Exception) { } }
public void Close() { try { DataHandle.WriteLog(string.Format("已关闭{0}的UDP代理隧道", Client_Point)); UDP_Client.Close(); TCP_Client.GetStream().Close(); TCP_Client.Close(); } catch (Exception) { } }
/// <summary> /// 关闭客户端连接 /// </summary> /// <param name="Tcp_Client">客户端TCPClient</param> private void Close(TcpClient Tcp_Client) { try { if (Tcp_Client.Connected) { DataHandle.WriteLog($"已关闭客户端{Tcp_Client.Client.RemoteEndPoint}的连接"); Tcp_Client.GetStream().Close(); Tcp_Client.Close(); } } catch (SocketException) { } }
public TCP_Listen(IPAddress IP, int Port, bool Udp_Support = true) { Receive_Data = new byte[Data_Size]; TcpListener Socks_Server = new TcpListener(IP, Port); DataHandle.WriteLog(string.Format("Socks服务已启动,监听{0}端口中", Port)); Socks_Server.Start(); Socks_Server.BeginAcceptTcpClient(AcceptTcpClient, Socks_Server); if (Udp_Support) { UDP_Listener = new UDP_Listen(Port); } else { UDP_Support = Udp_Support; } }
public TCP_Server(TcpClient Tcp_Client, TcpClient Tcp_Proxy) { try { TCP_Proxy = Tcp_Proxy; TCP_Client = Tcp_Client; Client_Stream = TCP_Client.GetStream(); Proxy_Stream = TCP_Proxy.GetStream(); Client_Stream.BeginRead(Client_Data, 0, Data_Size, TCP_Client_Receive, null); Proxy_Stream.BeginRead(Proxy_Data, 0, Data_Size, TCP_Proxy_Receive, null); DataHandle.WriteLog($"开启对{TCP_Client.Client.RemoteEndPoint}的TCP代理隧道"); } catch (SocketException) { close(); } }
public TCP_Server(TcpClient Tcp_Client, TcpClient Tcp_Proxy) { try { Client_Data = new byte[Data_Size]; Proxy_Data = new byte[Data_Size]; TCP_Proxy = Tcp_Proxy; TCP_Client = Tcp_Client; Client_Stream = TCP_Client.GetStream(); Proxy_Stream = TCP_Proxy.GetStream(); C_Read_Delegate = new AsyncCallback(TCP_Client_Receive); P_Read_Delegate = new AsyncCallback(TCP_Proxy_Receive); Client_Stream.BeginRead(Client_Data, 0, Data_Size, C_Read_Delegate, null); Proxy_Stream.BeginRead(Proxy_Data, 0, Data_Size, P_Read_Delegate, null); DataHandle.WriteLog(string.Format("开启对{0}的TCP代理隧道", TCP_Client.Client.RemoteEndPoint)); } catch (SocketException) { close(); } }
/// <summary> /// TCP清理 /// </summary> private void close() { try { if (TCP_Client != null) { DataHandle.WriteLog(string.Format("已断开客户端{0}的连接", TCP_Client.Client.RemoteEndPoint)); Client_Stream.Close(); TCP_Client.Close(); TCP_Client = null; } if (TCP_Proxy != null) { DataHandle.WriteLog(string.Format("已断开代理端{0}的连接", TCP_Proxy.Client.RemoteEndPoint)); Proxy_Stream.Close(); TCP_Proxy.Close(); TCP_Proxy = null; } } catch (Exception) { } }
public TCP_Listen(IPAddress IP, int Port, string Pass, bool Udp_Support = true) { DataHandle.Key = DataHandle.Get_Pass(Pass); Receive_Data = new byte[Data_Size]; try { TcpListener Socks_Server = new TcpListener(IP, Port); Socks_Server.Start(); Socks_Server.BeginAcceptTcpClient(AcceptTcpClient, Socks_Server); if (Udp_Support) { UDP_Listener = new UDP_Listen(Port); } else { UDP_Support = Udp_Support; } DataHandle.WriteLog($"Socks服务已启动,监听{Port}端口中,UDP支持:{UDP_Support}"); } catch (SocketException) { DataHandle.WriteLog($"端口{Port}被占用,监听服务开启失败"); } }