public void P2PStart_ServerTransfer(string token, string clientName, int clientPort, int p2pType)
        {
            P2PTcpItem item = null;

            if (ClientCenter.Instance.TcpMap.ContainsKey(clientName))
            {
                item = ClientCenter.Instance.TcpMap[clientName];
            }
            if (item != null && item.TcpClient.Connected)
            {
                if (item.BlackClients.Contains(m_tcpClient.ClientName))
                {
                    Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}已被加入黑名单");
                    LogUtils.Warning($"【P2P】客户端{clientName}已被加入黑名单");
                    m_tcpClient.Client.Send(sendPacket.PackData());
                }
                else if (item.AllowPorts.Any(t => t.Match(clientPort, m_tcpClient.ClientName)))
                {
                    LogUtils.Debug("【P2P】等待Tcp连接,进行绑定");
                    Send_0x0201_Success sendDPacket = new Send_0x0201_Success(token, clientPort, p2pType);
                    Send_0x0201_Success sendSPacket = new Send_0x0201_Success(token, p2pType);
                    ClientCenter.Instance.TcpMap[clientName].TcpClient.Client.Send(sendDPacket.PackData());
                    m_tcpClient.Client.Send(sendSPacket.PackData());
                }
                else
                {
                    Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"没有权限,端口{clientPort}");
                    LogUtils.Debug($"【P2P】没有权限,端口{clientPort}");
                    m_tcpClient.Client.Send(sendPacket.PackData());
                }
            }
            else
            {
                //发送客户端未在线
                LogUtils.Debug($"【P2P】客户端{clientName}不在线.");
                Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}不在线");
                m_tcpClient.Client.Send(sendPacket.PackData());
            }
        }
Exemple #2
0
        public void P2PStart_ServerTransfer(string token, string clientName, int clientPort, int p2pType)
        {
            P2PTcpItem item = null;

            if (clientCenter.TcpMap.ContainsKey(clientName))
            {
                item = clientCenter.TcpMap[clientName];
            }
            if (item != null && item.TcpClient.Connected)
            {
                if (item.BlackClients.Contains(m_tcpClient.ClientName))
                {
                    Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}已被加入黑名单");
                    LogUtils.Warning($"建立隧道失败,客户端{clientName}已被加入黑名单");
                    EasyOp.Do(() => m_tcpClient.BeginSend(sendPacket.PackData()));
                }
                else if (item.AllowPorts.Any(t => t.Match(clientPort, m_tcpClient.ClientName)))
                {
                    LogUtils.Debug($"通知客户端开始建立{(p2pType >= 1 ? "打洞" : "中转")}模式隧道 token{token}");
                    Send_0x0201_Success sendDPacket = new Send_0x0201_Success(token, clientPort, p2pType);
                    Send_0x0201_Success sendSPacket = new Send_0x0201_Success(token, p2pType);
                    clientCenter.TcpMap[clientName].TcpClient.BeginSend(sendDPacket.PackData());
                    EasyOp.Do(() => m_tcpClient.BeginSend(sendSPacket.PackData()));
                }
                else
                {
                    Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"未获得授权,无法建立隧道,端口{clientPort}");
                    LogUtils.Debug($"未获得授权,无法建立隧道,端口{clientPort}");
                    EasyOp.Do(() => m_tcpClient.BeginSend(sendPacket.PackData()));
                }
            }
            else
            {
                //发送客户端未在线
                LogUtils.Debug($"【P2P】客户端{clientName}不在线.");
                Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}不在线");
                EasyOp.Do(() => m_tcpClient.BeginSend(sendPacket.PackData()));
            }
        }
        public override bool Excute()
        {
            int step = BinaryUtils.ReadInt(m_data);

            //是否第一步创建
            if (step == 1)
            {
                //token,servername,port
                string token      = BinaryUtils.ReadString(m_data);
                string clientName = BinaryUtils.ReadString(m_data);
                int    clientPort = BinaryUtils.ReadInt(m_data);

                P2PTcpItem item = null;
                if (Global.TcpMap.ContainsKey(clientName))
                {
                    item = Global.TcpMap[clientName];
                }
                if (item != null && item.TcpClient.Connected)
                {
                    if (item.BlackClients.Contains(m_tcpClient.ClientName))
                    {
                        Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}已被加入黑名单");
                        LogUtils.Warning($"【P2P】客户端{clientName}已被加入黑名单");
                        m_tcpClient.Client.Send(sendPacket.PackData());
                    }
                    else if (item.AllowPorts.Any(t => t.Match(clientPort, m_tcpClient.ClientName)))
                    {
                        LogUtils.Debug("【P2P】等待Tcp连接,进行绑定");
                        Send_0x0201_Success sendDPacket = new Send_0x0201_Success(token, clientPort);
                        Send_0x0201_Success sendSPacket = new Send_0x0201_Success(token);
                        Global.TcpMap[clientName].TcpClient.Client.Send(sendDPacket.PackData());
                        m_tcpClient.Client.Send(sendSPacket.PackData());
                    }
                    else
                    {
                        Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"没有权限,端口{clientPort}");
                        LogUtils.Debug($"【P2P】没有权限,端口{clientPort}");
                        m_tcpClient.Client.Send(sendPacket.PackData());
                    }
                }
                else
                {
                    //发送客户端未在线
                    LogUtils.Debug($"【P2P】客户端{clientName}不在线.");
                    Send_0x0201_Failure sendPacket = new Send_0x0201_Failure($"客户端{clientName}不在线");
                    m_tcpClient.Client.Send(sendPacket.PackData());
                }
            }
            else if (step == 3)
            {
                string clientName = BinaryUtils.ReadString(m_data);
                m_tcpClient.ClientName = clientName;
                string token = BinaryUtils.ReadString(m_data);
                if (Global.WaiteConnetctTcp.ContainsKey(token))
                {
                    LogUtils.Debug($"【P2P】内网穿透成功");
                    P2PTcpClient client = Global.WaiteConnetctTcp[token];
                    Global.WaiteConnetctTcp.Remove(token);
                    client.IsAuth        = m_tcpClient.IsAuth = true;
                    client.ToClient      = m_tcpClient;
                    m_tcpClient.ToClient = client;
                    Send_0x0201_Success sendPacket = new Send_0x0201_Success();
                    client.Client.Send(sendPacket.PackData());
                    m_tcpClient.Client.Send(sendPacket.PackData());
                }
                else
                {
                    Global.WaiteConnetctTcp.Add(token, m_tcpClient);
                    LogUtils.Debug("【P2P】等待目标tcp.");
                    Global.TaskFactory.StartNew(() =>
                    {
                        Thread.Sleep(Global.P2PTimeout);
                        if (Global.WaiteConnetctTcp.ContainsKey(token))
                        {
                            LogUtils.Debug("【P2P】已超时,内网穿透失败.");
                            Global.WaiteConnetctTcp[token].Close();
                            Global.WaiteConnetctTcp.Remove(token);
                        }
                    });
                }
            }
            return(true);
        }