public override bool Excute() { LogUtils.Trace($"开始处理消息:0x0101"); bool ret = true; string clientName = BinaryUtils.ReadString(m_data); string authCode = BinaryUtils.ReadString(m_data); if (appCenter.Config.ClientAuthList.Count == 0 || appCenter.Config.ClientAuthList.Any(t => t.Match(clientName, authCode))) { bool isSuccess = true; P2PTcpItem item = new P2PTcpItem(); item.TcpClient = m_tcpClient; if (clientCenter.TcpMap.ContainsKey(clientName)) { if (clientCenter.TcpMap[clientName].TcpClient.IsDisConnected) { clientCenter.TcpMap[clientName].TcpClient?.SafeClose(); clientCenter.TcpMap[clientName] = item; } else { isSuccess = false; Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, false, $"ClientName:{clientName} 已被使用", clientName); LogUtils.Info($"客户端【{clientName}】接入被拒绝,名称被使用,ip:{m_tcpClient.RemoteEndPoint}"); EasyOp.Do(() => m_tcpClient.BeginSend(sendPacket.PackData())); ret = false; } } else { clientCenter.TcpMap.Add(clientName, item); } if (isSuccess) { m_tcpClient.IsAuth = true; m_tcpClient.ClientName = clientName; Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, true, $"客户端{clientName}认证通过", clientName); m_tcpClient.BeginSend(sendPacket.PackData()); LogUtils.Info($"客户端【{clientName}】成功接入,ip:{m_tcpClient.RemoteEndPoint}"); } } else { Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, false, $"客户端{clientName}认证失败", clientName); LogUtils.Info($"客户端【{clientName}】接入被拒绝,授权码错误或客户端名称不在AllowClient内,ip:{m_tcpClient.RemoteEndPoint}"); m_tcpClient.BeginSend(sendPacket.PackData()); ret = false; } return(ret); }
public override bool Excute() { LogUtils.Trace($"开始处理消息:0x0104"); bool ret = true; string macAddress = BinaryUtils.ReadString(m_data); string clientName = clientCenter.GetClientName(macAddress); bool isSuccess = true; P2PTcpItem item = new P2PTcpItem(); item.TcpClient = m_tcpClient; if (appCenter.Config.ClientAuthList.Count == 0 || appCenter.Config.ClientAuthList.Any(t => t.Match(clientName, "auto"))) { if (clientCenter.TcpMap.ContainsKey(clientName)) { if (clientCenter.TcpMap[clientName].TcpClient.IsDisConnected) { clientCenter.TcpMap[clientName].TcpClient?.SafeClose(); clientCenter.TcpMap[clientName] = item; } else { isSuccess = false; Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, false, $"ClientName:{clientName} 已被使用", clientName); m_tcpClient.BeginSend(sendPacket.PackData()); ret = false; } } else { clientCenter.TcpMap.Add(clientName, item); } if (isSuccess) { m_tcpClient.ClientName = clientName; Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, true, $"客户端{clientName}认证通过", clientName); m_tcpClient.BeginSend(sendPacket.PackData()); } } else { Send_0x0101 sendPacket = new Send_0x0101(m_tcpClient, false, $"客户端{clientName}认证失败", clientName); m_tcpClient.BeginSend(sendPacket.PackData()); ret = false; } return(ret); }
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()); }); }
public void P2PBind_ServerTransfer(string token) { P2PTcpClient client = clientCenter.WaiteConnetctTcp[token]; clientCenter.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(4); EasyOp.Do(() => client.BeginSend(sendPacket.PackData())); EasyOp.Do(() => m_tcpClient.BeginSend(sendPacket.PackData())); LogUtils.Debug($"成功建立中转模式隧道 token:{token}"); }
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 void P2PBind_DirectConnect(string token) { P2PTcpClient clientA = clientCenter.WaiteConnetctTcp[token]; P2PTcpClient clientB = m_tcpClient; Send_0x0201_Success sendPacketA = new Send_0x0201_Success(14); sendPacketA.WriteDirectData(clientB.Client.RemoteEndPoint.ToString(), token); Send_0x0201_Success sendPacketB = new Send_0x0201_Success(14); sendPacketB.WriteDirectData(clientA.Client.RemoteEndPoint.ToString(), token); EasyOp.Do(() => clientA.BeginSend(sendPacketA.PackData())); EasyOp.Do(() => clientB.BeginSend(sendPacketB.PackData())); EasyOp.Do(() => clientA?.SafeClose()); EasyOp.Do(() => clientB?.SafeClose()); }
public void CreateTcpFromSource_DirectConnect(string token) { Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token); Utils.LogUtils.Debug($"命令:0x0201 正尝试建立P2P模式隧道 token:{token}"); if (tcpCenter.WaiteConnetctTcp.ContainsKey(token)) { P2PTcpClient serverClient = new P2PTcpClient(); serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); EasyOp.Do(() => { serverClient.Connect(appCenter.ServerAddress, appCenter.ServerPort); }, () => { serverClient.IsAuth = true; serverClient.UpdateEndPoint(); EasyOp.Do(() => { serverClient.BeginSend(sendPacket.PackData()); }, () => { EasyOp.Do(() => { Global_Func.ListenTcp <ReceivePacket>(serverClient); LogUtils.Debug($"命令:0x0201 P2P模式隧道,已连接到服务器,等待下一步操作 token:{token}"); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,服务器连接被强制断开 token:{token}:{Environment.NewLine}{ex}"); tcpCenter.WaiteConnetctTcp.Remove(token); EasyOp.Do(serverClient.SafeClose); }); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,隧道打洞失败 token:{token}:{Environment.NewLine} 隧道被服务器强制断开"); tcpCenter.WaiteConnetctTcp.Remove(token); }); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,无法连接服务器 token:{token}:{Environment.NewLine}{ex}"); tcpCenter.WaiteConnetctTcp.Remove(token); }); } else { LogUtils.Debug($"命令:0x0201 接收到建立P2P模式隧道命令,但已超时. token:{token}"); } }
public void CreateTcpFromDest_DirectConnect(string token) { int port = BinaryUtils.ReadInt(data); Utils.LogUtils.Debug($"命令:0x0201 正尝试建立P2P模式隧道 token:{token}"); P2PTcpClient serverClient = new P2PTcpClient(); serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); EasyOp.Do(() => { serverClient.Connect(appCenter.ServerAddress, appCenter.ServerPort); }, () => { serverClient.IsAuth = true; serverClient.P2PLocalPort = port; serverClient.UpdateEndPoint(); 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); LogUtils.Debug($"命令:0x0201 P2P模式隧道,已连接到服务器,等待下一步操作 token:{token}"); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,服务器连接被强制断开 token:{token}:{Environment.NewLine}{ex}"); EasyOp.Do(serverClient.Close); }); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,隧道打洞失败 token:{token}:{Environment.NewLine} 隧道被服务器强制断开"); }); }, ex => { LogUtils.Debug($"命令:0x0201 P2P模式隧道,无法连接服务器 token:{token}:{Environment.NewLine}{ex}"); }); }