private void ReceivedMessage(IAsyncResult result) { try { if (connectionState == ConnectionState.Disconnected) { // Connection has been closed. Ignore the message. LoggingService.LogWarning("LocalNodeConnection: Ignored message received after connection was closed."); return; } var messageData = transport.EndReceiveMessage(result); if (messageData == null) { return; } // Get the next one! ReceiveMessage(); Message message = null; string messageFrom = null; try { message = Message.Parse(transport.Network, messageData, out messageFrom); } catch (InvalidSignatureException ex) { if (string.IsNullOrEmpty(messageFrom) || messageFrom == remoteNodeInfo.NodeId) { throw ex; } LoggingService.LogWarning("Ignored message with invalid signature from {0}", messageFrom); return; } var info = new ReceivedMessageInfo(); info.Connection = this; info.Message = message; transport.Network.Core.RaiseMessageReceived(info); if (remoteNodeInfo == null) { var keyInfo = (KeyInfo)message.Content; var publicKey = new PublicKey(keyInfo.Info, keyInfo.Key); var nodeId = publicKey.Fingerprint; if (nodeId.ToLower() == transport.Network.LocalNode.NodeID.ToLower()) { throw new Exception("You cannot connect to yourself!"); } if (!transport.Network.TrustedNodes.ContainsKey(nodeId)) { var acceptKey = transport.Network.RaiseReceivedKey(this, keyInfo); if (acceptKey) { var trustedNode = new TrustedNodeInfo($"[{nodeId}]", nodeId, keyInfo.Key); transport.Network.AddTrustedNode(trustedNode); transport.Network.Core.Settings.SyncNetworkInfoAndSave(transport.Network.Core); } else { throw new ConnectNotTrustedException(); } } if (transport.Network.TrustedNodes[nodeId].AllowConnect == false) { throw new ConnectNotAllowedException(nodeId); } foreach (var connection in transport.Network.LocalConnections) { if (connection != this && connection.NodeRemote != null && connection.NodeRemote.NodeID == nodeId) { throw new Exception("Already connected!"); } } remoteNodeInfo = transport.Network.TrustedNodes[nodeId]; if (!transport.Network.Nodes.ContainsKey(RemoteNodeInfo.NodeId)) { var node = new Node(transport.Network, RemoteNodeInfo.NodeId); node.NickName = RemoteNodeInfo.Identifier; node.Verified = true; transport.Network.AddNode(node); } NodeRemote = transport.Network.Nodes[RemoteNodeInfo.NodeId]; if (transport.Incoming) { SendMessage(transport.Network.MessageBuilder.CreateMyKeyMessage(null)); } else { ConnectionState = ConnectionState.Authenticating; RaiseConnectionInfoChanged(); var m = transport.Network.MessageBuilder.CreateAuthMessage(this, RemoteNodeInfo); SendMessage(m); } } else { ThreadPool.QueueUserWorkItem(transport.Network.ProcessMessage, info); } } catch (Exception ex) { Disconnect(ex); } }
private void ReceivedMessage(IAsyncResult result) { try { if (connectionState == ConnectionState.Disconnected) { // Connection has been closed. Ignore the message. LoggingService.LogWarning("LocalNodeConnection: Ignored message received after connection was closed."); return; } byte[] messageData = transport.EndReceiveMessage(result); if (messageData == null) return; // Get the next one! ReceiveMessage(); Message message = null; string messageFrom = null; try { message = Message.Parse(transport.Network, messageData, out messageFrom); } catch (InvalidSignatureException ex) { if (String.IsNullOrEmpty(messageFrom) || messageFrom == remoteNodeInfo.NodeID) { throw ex; } else { LoggingService.LogWarning("Ignored message with invalid signature from {0}", messageFrom); return; } } ReceivedMessageInfo info = new ReceivedMessageInfo (); info.Connection = this; info.Message = message; Core.RaiseMessageReceived(info); if (remoteNodeInfo == null) { KeyInfo key = (KeyInfo) message.Content; RSACryptoServiceProvider provider = new RSACryptoServiceProvider (); provider.FromXmlString (key.Key); string nodeID = FileFind.Common.SHA512Str(provider.ToXmlString (false)); if (nodeID.ToLower () == transport.Network.LocalNode.NodeID.ToLower ()) { throw new Exception ("You cannot connect to yourself!"); } if (!transport.Network.TrustedNodes.ContainsKey(nodeID)) { bool acceptKey = transport.Network.RaiseReceivedKey (this, key); if (acceptKey) { TrustedNodeInfo trustedNode = new TrustedNodeInfo(); trustedNode.Identifier = String.Format("[{0}]", nodeID); trustedNode.PublicKey = new PublicKey(key.Key); transport.Network.AddTrustedNode(trustedNode); Core.Settings.SyncNetworkInfoAndSave(); } else { throw new ConnectNotTrustedException (); } } if (transport.Network.TrustedNodes[nodeID].AllowConnect == false) throw new ConnectNotAllowedException(nodeID); foreach (LocalNodeConnection connection in transport.Network.LocalConnections) { if (connection != this && connection.NodeRemote != null && connection.NodeRemote.NodeID == nodeID) throw new Exception ("Already connected!"); } remoteNodeInfo = transport.Network.TrustedNodes[nodeID]; if (!transport.Network.Nodes.ContainsKey(RemoteNodeInfo.NodeID)) { Node node = new Node (transport.Network, RemoteNodeInfo.NodeID); node.NickName = RemoteNodeInfo.Identifier; node.Verified = true; transport.Network.AddNode(node); } this.NodeRemote = transport.Network.Nodes[RemoteNodeInfo.NodeID]; if (transport.Incoming == true) { this.SendMessage(transport.Network.MessageBuilder.CreateMyKeyMessage (null)); } else { ConnectionState = ConnectionState.Authenticating; RaiseConnectionInfoChanged (); Message m = transport.Network.MessageBuilder.CreateAuthMessage (this, RemoteNodeInfo); SendMessage(m); } } else { ThreadPool.QueueUserWorkItem(new WaitCallback(transport.Network.ProcessMessage), info); } } catch (Exception ex) { Disconnect(ex); } }
private void ReceivedMessage(IAsyncResult result) { try { if (connectionState == ConnectionState.Disconnected) { // Connection has been closed. Ignore the message. Core.LoggingService.LogWarning("LocalNodeConnection: Ignored message received after connection was closed."); return; } byte[] messageData = transport.EndReceiveMessage(result); if (messageData == null) { return; } // Get the next one! ReceiveMessage(); Message message = null; string messageFrom = null; try { message = Message.Parse(transport.Network, messageData, out messageFrom); } catch (InvalidSignatureException ex) { if (String.IsNullOrEmpty(messageFrom) || messageFrom == remoteNodeInfo.NodeID) { throw ex; } else { Core.LoggingService.LogWarning("Ignored message with invalid signature from {0}", messageFrom); return; } } ReceivedMessageInfo info = new ReceivedMessageInfo(); info.Connection = this; info.Message = message; Core.RaiseMessageReceived(info); if (remoteNodeInfo == null) { KeyInfo key = (KeyInfo)message.Content; RSACryptoServiceProvider provider = new RSACryptoServiceProvider(); provider.FromXmlString(key.Key); string nodeID = FileFind.Common.SHA512Str(provider.ToXmlString(false)); if (nodeID.ToLower() == transport.Network.LocalNode.NodeID.ToLower()) { throw new Exception("You cannot connect to yourself!"); } if (!transport.Network.TrustedNodes.ContainsKey(nodeID)) { bool acceptKey = transport.Network.RaiseReceivedKey(this, key); if (acceptKey) { TrustedNodeInfo trustedNode = new TrustedNodeInfo(); trustedNode.Identifier = String.Format("[{0}]", nodeID); trustedNode.PublicKey = new PublicKey(key.Key); transport.Network.AddTrustedNode(trustedNode); Core.Settings.SyncNetworkInfoAndSave(); } else { throw new ConnectNotTrustedException(); } } if (transport.Network.TrustedNodes[nodeID].AllowConnect == false) { throw new ConnectNotAllowedException(nodeID); } foreach (LocalNodeConnection connection in transport.Network.LocalConnections) { if (connection != this && connection.NodeRemote != null && connection.NodeRemote.NodeID == nodeID) { throw new Exception("Already connected!"); } } remoteNodeInfo = transport.Network.TrustedNodes[nodeID]; if (!transport.Network.Nodes.ContainsKey(RemoteNodeInfo.NodeID)) { Node node = new Node(transport.Network, RemoteNodeInfo.NodeID); node.NickName = RemoteNodeInfo.Identifier; node.Verified = true; transport.Network.AddNode(node); } this.NodeRemote = transport.Network.Nodes[RemoteNodeInfo.NodeID]; if (transport.Incoming == true) { this.SendMessage(transport.Network.MessageBuilder.CreateMyKeyMessage(null)); } else { ConnectionState = ConnectionState.Authenticating; RaiseConnectionInfoChanged(); Message m = transport.Network.MessageBuilder.CreateAuthMessage(this, RemoteNodeInfo); SendMessage(m); } } else { ThreadPool.QueueUserWorkItem(new WaitCallback(transport.Network.ProcessMessage), info); } } catch (Exception ex) { Disconnect(ex); } }
internal protected abstract bool OnEndReceive(bool isSuccess, ReceivedMessageInfo <TMessage> messageInfo, MessagingServiceBase <TMessage> service);