public override bool Excute()
        {
            try
            {
                string token          = BinaryUtils.ReadString(m_data);
                int    mapPort        = BinaryUtils.ReadInt(m_data);
                string remoteEndPoint = BinaryUtils.ReadString(m_data);;
                if (Global.AllowPortList.Any(t => t.Match(mapPort, m_tcpClient.ClientName)))
                {
                    P2PTcpClient portClient   = new P2PTcpClient("127.0.0.1", mapPort);
                    P2PTcpClient serverClient = new P2PTcpClient(Global.ServerAddress, Global.ServerPort);
                    portClient.IsAuth     = serverClient.IsAuth = true;
                    portClient.ToClient   = serverClient;
                    serverClient.ToClient = portClient;


                    Models.Send.Send_0x0211 sendPacket = new Models.Send.Send_0x0211(token);
                    int length = serverClient.Client.Send(sendPacket.PackData());
                    LogUtils.Info($"命令:0x0211  内网穿透Tcp绑定:{portClient.RemoteEndPoint}->{serverClient.RemoteEndPoint}->{remoteEndPoint}");
                    Global.TaskFactory.StartNew(() => { Global_Func.ListenTcp <Models.Receive.Packet_0x0212>(portClient); });
                    Global.TaskFactory.StartNew(() => { Global_Func.ListenTcp <Models.Receive.Packet_ToPort>(serverClient); });
                }
                else
                {
                    LogUtils.Warning($"命令:0x0211 无权限,端口:{mapPort}");
                    m_tcpClient.Close();
                }
            }
            catch (Exception ex)
            {
                LogUtils.Warning($"命令:0x0211 错误:{Environment.NewLine} {ex}");
            }
            return(true);
        }
예제 #2
0
 protected virtual void SendError(string token, string msg)
 {
     EasyOp.Do(() =>
     {
         Models.Send.Send_0x0211 sendPacket = new Models.Send.Send_0x0211(token, false, msg);
         m_tcpClient.BeginSend(sendPacket.PackData());
     });
 }
예제 #3
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);
        }