Esempio n. 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);
                }
            }
        }
Esempio n. 2
0
        public void Start()
        {
            _udpClient.BeginReceive(UdpRecieveLoop, null);
            _tcpListener.Start();
            _tcpListener.BeginAcceptSocket(TcpAcceptLoop, null);

            using (var mem = new MemoryStream())
            {
                var wrt = new BinaryWriter(mem);
                wrt.Write(true); //New client added
                wrt.Write(PublicKey.Length);
                wrt.Write(PublicKey);
                //This is not necessary
                //Length is a fixed 16 bytes
                //wrt.Write(_broadcastGuid.ToByteArray());

                mem.Position = 0;
                GlobalHelpers.BroadcastPacket(mem.ToArray(), NetworkPort);
            }
        }
Esempio n. 3
0
        public void Dispose()
        {
            try
            {
                using (var mem = new MemoryStream())
                {
                    var wrt = new BinaryWriter(mem);
                    wrt.Write(false); //Client is leaving.
                    wrt.Write(PublicKey.Length);
                    wrt.Write(PublicKey);

                    mem.Position = 0;
                    GlobalHelpers.BroadcastPacket(mem.ToArray(), NetworkPort);
                }
            }
            catch
            {
                //Ignore this, we're dying anyway.
            }

            _udpClient?.Close();
            _tcpListener?.Stop();
        }
Esempio n. 4
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();
                    }
                }
        }