Example #1
0
        public void SendMessage(byte[] messageData)
        {
            using (var client = new TcpClient())
            {
                client.Connect(EndPoint);
                using (var stream = client.GetStream())
                {
                    var wrt = new BinaryWriter(stream);
                    wrt.Write((byte)PeerCommand.BinaryBlob);

                    GlobalHelpers.ProveIdentity(stream, Client.PublicKey, Client.PrivateKey);
                    GlobalHelpers.OneTimePad(messageData, Client.SymetricKey);

                    wrt.Write(messageData.Length);
                    wrt.Write(messageData);
                }
            }
        }
Example #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();
                    }
                }
        }