Exemplo n.º 1
0
        public override bool Excute()
        {
            int    tokenLength = m_data.ReadInt32();
            string token       = m_data.ReadBytes(tokenLength).ToStringUnicode();

            if (ClientCenter.Instance.WaiteConnetctTcp.ContainsKey(token))
            {
                P2PTcpClient client = ClientCenter.Instance.WaiteConnetctTcp[token];
                ClientCenter.Instance.WaiteConnetctTcp.Remove(token);
                client.IsAuth        = m_tcpClient.IsAuth = true;
                client.ToClient      = m_tcpClient;
                m_tcpClient.ToClient = client;
                LogUtils.Debug($"命令:0x0211 已绑定内网穿透(2端)通道 {client.RemoteEndPoint}->{m_tcpClient.RemoteEndPoint}");
                //监听client
                AppCenter.Instance.StartNewTask(() => { Global_Func.ListenTcp <Packet_0x0212>(client); });
            }
            else
            {
                m_tcpClient.SafeClose();
                throw new Exception("绑定内网穿透(2端)通道失败,目标Tcp连接已断开");
            }
            return(true);
        }
        public override bool Excute()
        {
            int    tokenLength = m_data.ReadInt32();
            string token       = m_data.ReadBytes(tokenLength).ToStringUnicode();

            if (Global.WaiteConnetctTcp.ContainsKey(token))
            {
                P2PTcpClient client = Global.WaiteConnetctTcp[token];
                Global.WaiteConnetctTcp.Remove(token);
                client.IsAuth        = m_tcpClient.IsAuth = true;
                client.ToClient      = m_tcpClient;
                m_tcpClient.ToClient = client;
                Debug.WriteLine($"[服务器]转发{client.RemoteEndPoint}->{m_tcpClient.RemoteEndPoint}");
                //监听client
                Global.TaskFactory.StartNew(() => { Global_Func.ListenTcp <Port2PPacket>(client); });
            }
            else
            {
                m_tcpClient.Close();
                throw new Exception("连接已关闭");
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     从目标端创建与服务器的tcp连接
        /// </summary>
        /// <param name="token"></param>
        public void CreateTcpFromDest(string token)
        {
            Utils.LogUtils.Debug($"命令:0x0201  正在连接中转模式隧道通道 token:{token}");
            int         port    = BinaryUtils.ReadInt(data);
            PortMapItem destMap = appCenter.PortMapList.FirstOrDefault(t => t.LocalPort == port && string.IsNullOrEmpty(t.LocalAddress));


            P2PTcpClient portClient = null;

            EasyOp.Do(() =>
            {
                if (destMap != null)
                {
                    if (destMap.MapType == PortMapType.ip)
                    {
                        portClient = new P2PTcpClient(destMap.RemoteAddress, destMap.RemotePort);
                    }
                    else
                    {
                        portClient = new P2PTcpClient("127.0.0.1", port);
                    }
                }
                else
                {
                    portClient = new P2PTcpClient("127.0.0.1", port);
                }
            }, () =>
            {
                P2PTcpClient serverClient = null;
                EasyOp.Do(() =>
                {
                    serverClient = new P2PTcpClient(appCenter.ServerAddress, appCenter.ServerPort);
                }, () =>
                {
                    portClient.IsAuth     = serverClient.IsAuth = true;
                    portClient.ToClient   = serverClient;
                    serverClient.ToClient = portClient;
                    Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token);

                    EasyOp.Do(() =>
                    {
                        serverClient.BeginSend(sendPacket.PackData());
                    }, () =>
                    {
                        EasyOp.Do(() =>
                        {
                            Global_Func.ListenTcp <ReceivePacket>(serverClient);
                            Utils.LogUtils.Debug($"命令:0x0201  中转模式隧道,连接成功 token:{token}");
                        }, ex =>
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                            EasyOp.Do(portClient.SafeClose);
                            EasyOp.Do(portClient.SafeClose);
                        });
                    }, ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                        EasyOp.Do(portClient.SafeClose);
                    });
                }, ex =>
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                    EasyOp.Do(portClient.SafeClose);
                });
            }, ex =>
            {
                LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接目标端口失败 token{token}:{Environment.NewLine}{ex}");
            });
        }
Exemplo n.º 4
0
        public void P2PBind_DirectConnect(P2PTcpClient p2pClient, string token)
        {
            if (m_tcpClient.P2PLocalPort > 0)
            {
                //B端
                int         port    = m_tcpClient.P2PLocalPort;
                PortMapItem destMap = appCenter.PortMapList.FirstOrDefault(t => t.LocalPort == port && string.IsNullOrEmpty(t.LocalAddress));

                P2PTcpClient portClient = null;

                EasyOp.Do(() =>
                {
                    if (destMap != null)
                    {
                        if (destMap.MapType == PortMapType.ip)
                        {
                            portClient = new P2PTcpClient(destMap.RemoteAddress, destMap.RemotePort);
                        }
                        else
                        {
                            portClient = new P2PTcpClient("127.0.0.1", port);
                        }
                    }
                    else
                    {
                        portClient = new P2PTcpClient("127.0.0.1", port);
                    }
                },
                          () =>
                {
                    portClient.IsAuth   = p2pClient.IsAuth = true;
                    portClient.ToClient = p2pClient;
                    p2pClient.ToClient  = portClient;
                    EasyOp.Do(() =>
                    {
                        if (Global_Func.BindTcp(p2pClient, portClient))
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接成功 token:{token}");
                        }
                        else
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}");
                        }
                    },
                              ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                    });
                },
                          ex =>
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接目标端口失败 token:{token}:{Environment.NewLine}{ex}");
                    EasyOp.Do(p2pClient.SafeClose);
                });
            }
            else
            {
                //A端,发起端
                if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                {
                    P2PTcpClient portClient = tcpCenter.WaiteConnetctTcp[token];
                    tcpCenter.WaiteConnetctTcp.Remove(token);
                    portClient.IsAuth   = p2pClient.IsAuth = true;
                    portClient.ToClient = p2pClient;
                    p2pClient.ToClient  = portClient;
                    EasyOp.Do(() =>
                    {
                        if (Global_Func.BindTcp(p2pClient, portClient))
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接成功 token:{token}");
                        }
                        else
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}");
                        }
                    },
                              ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                    });
                }
                else
                {
                    LogUtils.Debug($"命令:0x0201 接收到建立隧道命令,但已超时. token:{token}");
                    EasyOp.Do(p2pClient.SafeClose);
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 ///     监听连接外部程序的端口
 /// </summary>
 public void ListenPort()
 {
     //  监听端口
     Global.TaskFactory.StartNew(() => { Global_Func.ListenTcp <Packet_0x0202>(m_tcpClient.ToClient); });
 }
Exemplo n.º 6
0
 /// <summary>
 ///     监听连接外部程序的端口
 /// </summary>
 public void ListenPort()
 {
     //  监听端口
     AppCenter.Instance.StartNewTask(() => { Global_Func.ListenTcp <Packet_0x0202>(m_tcpClient.ToClient); });
 }
Exemplo n.º 7
0
        public override bool Excute()
        {
            LogUtils.Trace($"开始处理消息:0x0211");
            string token          = BinaryUtils.ReadString(data);
            int    mapPort        = BinaryUtils.ReadInt(data);
            string remoteEndPoint = BinaryUtils.ReadString(data);
            bool   isError        = true;

            if (appCenter.AllowPortList.Any(t => t.Match(mapPort, m_tcpClient.ClientName)))
            {
                P2PTcpClient portClient = null;
                EasyOp.Do(() => { portClient = new P2PTcpClient("127.0.0.1", mapPort); }, () =>
                {
                    P2PTcpClient serverClient = null;
                    EasyOp.Do(() =>
                    {
                        serverClient = new P2PTcpClient(appCenter.ServerAddress, appCenter.ServerPort);
                    }, () =>
                    {
                        portClient.IsAuth     = serverClient.IsAuth = true;
                        portClient.ToClient   = serverClient;
                        serverClient.ToClient = portClient;
                        Models.Send.Send_0x0211 sendPacket = new Models.Send.Send_0x0211(token, true, "");
                        LogUtils.Debug($"命令:0x0211 正在绑定内网穿透(2端)通道 {portClient.RemoteEndPoint}->服务器->{remoteEndPoint}{Environment.NewLine}token:{token} ");
                        EasyOp.Do(() =>
                        {
                            serverClient.BeginSend(sendPacket.PackData());
                        }, () =>
                        {
                            EasyOp.Do(() =>
                            {
                                Global_Func.ListenTcp <Models.Receive.Packet_0x0212>(portClient);
                                Global_Func.ListenTcp <Models.Receive.Packet_ToPort>(serverClient);
                                isError = false;
                                LogUtils.Debug($"命令:0x0211 成功绑定内网穿透(2端)通道 {portClient.RemoteEndPoint}->服务器->{remoteEndPoint}{Environment.NewLine}token:{token} ");
                            }, ex =>
                            {
                                LogUtils.Debug($"命令:0x0211 接收数据发生错误:{Environment.NewLine}{ex}");
                                EasyOp.Do(() => { portClient?.SafeClose(); });
                                EasyOp.Do(() => { serverClient?.SafeClose(); });
                                SendError(token, $"客户端发生异常,{ex.Message}");
                            });
                        }, ex =>
                        {
                            LogUtils.Debug($"命令:0x0211 无法连接服务器:{Environment.NewLine}{ex}");
                            EasyOp.Do(() => { portClient?.SafeClose(); });
                            EasyOp.Do(() => { serverClient?.SafeClose(); });
                            SendError(token, $"向服务端发送数据失败");
                        });
                    }, ex =>
                    {
                        LogUtils.Debug($"命令:0x0211 无法连接服务器:{Environment.NewLine}{ex}");
                        EasyOp.Do(() => { portClient?.SafeClose(); });
                        SendError(token, $"无法建立到服务端的tcp连接");
                    });
                }, ex =>
                {
                    LogUtils.Debug($"命令:0x0211 建立tcp连接[127.0.0.1:{mapPort}]失败:{Environment.NewLine}{ex}");
                    SendError(token, $"目标端口{mapPort}连接失败!");
                });
            }
            else
            {
                LogUtils.Debug($"命令:0x0211 已拒绝服务端连接本地端口[{mapPort}],不在AllowPort配置项的允许范围内");
                SendError(token, $"无权限访问端口{mapPort},请配置AllowPort");
            }
            return(true);
        }