Exemplo n.º 1
0
        public bool Connect()
        {
            if (_client?.Connected == true)
            {
                return(true);
            }

            try
            {
                _client = new TcpClient();
                _client.Connect(_address, _port);

                _drs = new DataReceiveService(_client, this);


                Task.Factory.StartNew(() =>
                {
                    _drs.Disconnected += client =>
                    {
                        Disconnected?.Invoke();
                    };

                    _drs.NewData += (data) =>
                    {
                        NewData?.Invoke(data);

                        try
                        {
                            object packet = null;

                            int packetId = BitConverter.ToInt32(data.Take(4).ToArray(), 0);

                            NewPacket?.Invoke(ref packetId, (t) =>
                            {
                                using (MemoryStream ms = new MemoryStream(data.Skip(4).ToArray()))
                                    using (BsonReader reader = new BsonReader(ms))
                                    {
                                        JsonSerializer serializer = new JsonSerializer();

                                        packet = serializer.Deserialize(reader, t);
                                    }

                                return((Packet)packet);
                            }, this);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message + " " + e.StackTrace);
                        }
                    };

                    _drs.Run();
                });

                var waitPacket = SendPacketAndGetResponse <ConnectionRequest, ConnectionConfig>(new ConnectionRequest());

                if (waitPacket?.CryptoEnabled == true)
                {
                    RsaClientSide rsaClientSide = new RsaClientSide();

                    var publicKey = rsaClientSide.PublicKeyString;

                    var rsaHandshakeResponse =
                        SendPacketAndGetResponse <RsaHandshakeRequest, RsaHandshakeResponse>(
                            new RsaHandshakeRequest()
                    {
                        PublicKey = publicKey
                    });

                    if (rsaHandshakeResponse == null)
                    {
                        throw new Exception();
                    }

                    var decryptedData = rsaClientSide.Step2(rsaHandshakeResponse.EncryptedData);

                    Console.WriteLine($"Receive decrypted AES-256 Key: {Convert.ToBase64String(decryptedData)}");

                    Aes256Key = decryptedData;

                    var pinger = new PingService(this);
                    pinger.StartAsync();
                }
                else
                {
                    var pinger = new PingService(this);
                    pinger.StartAsync();
                }
            }
            catch (Exception e)
            {
                return(false);
            }

            return(true);
        }