예제 #1
0
        private void UdpRecieveLoop(IAsyncResult result)
        {
            try
            {
                _udpClient.BeginReceive(UdpRecieveLoop, null);
            }
            catch
            {
                //On disposed
                return;
            }

            try
            {
                IPEndPoint remote = null;
                var        data   = _udpClient.EndReceive(result, ref remote);

                if (remote == null || data == null || data.Length == 0)
                {
                    return;
                }

                using (var mem = new MemoryStream(data))
                {
                    var bin    = new BinaryReader(mem);
                    var add    = bin.ReadBoolean();
                    var pubKey = bin.ReadBytes(bin.ReadInt32());

                    if (pubKey.Compare(PublicKey) || !TrustVerifier.VerifyTrust(pubKey))
                    {
                        return;
                    }

                    var peer = new PhonemePeer(this, pubKey, new IPEndPoint(remote.Address, NetworkPort));
                    if (add)
                    {
                        if (!_knownPeersSet.Contains(peer))
                        {
                            _knownPeersSet.Add(peer);
                            Application.Current.Dispatcher.Invoke(() => KnownPeers.Add(peer));
                            PeerJoin?.Invoke(peer);
                        }
                        peer.PerformTcpHandshake();
                    }
                    else
                    {
                        if (!_knownPeersSet.Remove(peer))
                        {
                            return;
                        }

                        var realPeer = KnownPeers.First(x => x.Equals(peer));
                        Application.Current.Dispatcher.Invoke(() => KnownPeers.Remove(realPeer));
                        PeerLeave?.Invoke(realPeer);
                    }
                }
            }
            catch (Exception e)
            {
                //Some kind of noise? Ignore this.
                Debug.WriteLine(e);
            }
        }
예제 #2
0
        private void TcpAcceptLoop(IAsyncResult result)
        {
            try
            {
                _tcpListener.BeginAcceptSocket(TcpAcceptLoop, null);
            }
            catch
            {
                //Called when disposed
                return;
            }

            using (var socket = _tcpListener.EndAcceptSocket(result))
                using (var stream = new NetworkStream(socket))
                {
                    if (!(socket.RemoteEndPoint is IPEndPoint))
                    {
                        return;
                    }

                    var rdr = new BinaryReader(stream);
                    var wrt = new BinaryWriter(stream);
                    switch ((PeerCommand)rdr.ReadByte())
                    {
                    case PeerCommand.Identify:
                    {
                        byte[] pub;
                        if (!GlobalHelpers.VerifyIdentity(stream, out pub))
                        {
                            return;
                        }

                        var symGuid = rdr.ReadBytes(rdr.ReadInt32());
                        using (var rsa = OpenSslKey.DecodeRsaPrivateKey(PrivateKey))
                            SymetricKey = new Guid(rsa.Decrypt(symGuid, false));

                        var peer = new PhonemePeer(this, pub, new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint).Address, NetworkPort));
                        if (!_knownPeersSet.Contains(peer))
                        {
                            _knownPeersSet.Add(peer);
                            //TODO: Remove this dependency
                            Application.Current.Dispatcher.Invoke(() => KnownPeers.Add(peer));
                            PeerJoin?.Invoke(peer);
                        }
                        else
                        {
                            peer = _knownPeersSet.First(x => x.Equals(peer));
                        }

                        peer.DisplayName = rdr.ReadBoolean() ? rdr.ReadString() : null;
                        if (rdr.ReadBoolean())
                        {
                            rdr.ReadBytes(rdr.ReadInt32());
                        }

                        wrt.Write(DisplayName != null);
                        if (DisplayName != null)
                        {
                            wrt.Write(DisplayName);
                        }
                        wrt.Write(ProfilePicture != null);
                        if (ProfilePicture != null)
                        {
                            wrt.Write(ProfilePicture.Length);
                            wrt.Write(ProfilePicture);
                        }


                        break;
                    }

                    case PeerCommand.BinaryBlob:
                    {
                        byte[] pub;
                        if (!GlobalHelpers.VerifyIdentity(stream, out pub))
                        {
                            return;
                        }

                        var peer = new PhonemePeer(this, pub, new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint).Address, NetworkPort));
                        if (!_knownPeersSet.Contains(peer))
                        {
                            return;
                        }
                        peer = _knownPeersSet.First(x => x.Equals(peer));

                        var blob = rdr.ReadBytes(rdr.ReadInt32());
                        GlobalHelpers.OneTimePad(blob, SymetricKey);

                        peer.OnMessageRecieved(blob);

                        break;
                    }

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
        }