public TargetProtocolPacket(byte[] data)
        {
            BinaryReader reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8);

            // Remove command code
            reader.ReadByte();
            UserName = reader.ReadString();            
            Packet = ProtocolPacket.FromData(NetworkUtils.ReadBytes(reader));
        }
        private ProtocolPacket DoConnect(TcpClient client, string hostname, bool ssl, string username, bool supportsUpgrade)
        {
            Stream stm;

            _client = client;

            if (ssl)
            {
                SslStream sslStream = new SslStream(_client.GetStream(), false, ValidateRemoteConnection);

                int lastTimeout = sslStream.ReadTimeout;
                sslStream.ReadTimeout = 3000;

                sslStream.AuthenticateAsClient(hostname);

                sslStream.ReadTimeout = lastTimeout;

                stm = sslStream;
            }
            else
            {
                stm = _client.GetStream();
            }

            _baseStream = new XorStream(stm);
            _writer     = new BinaryWriter(_baseStream);
            _reader     = new BinaryReader(_baseStream);

            WritePacket(new HelloProtocolPacket(username, Environment.MachineName, supportsUpgrade, 0));

            ProtocolPacket packet = ReadPacket(3000);

            if (packet is GoodbyeProtocolPacket)
            {
                throw new EndOfStreamException(((GoodbyeProtocolPacket)packet).Message);
            }
            else
            {
                HelloProtocolPacket p = packet as HelloProtocolPacket;
                if (p != null)
                {
                    if (p.SupportsSecurityUpgrade)
                    {
                        UpgradeSecurity(p.XorKey);
                    }
                }

                return(packet);
            }
        }
        public ProtocolPacket ReadPacket(int timeout)
        {
            int lastTimeout = _baseStream.ReadTimeout;

            try
            {
                _baseStream.ReadTimeout = timeout;
                return(ProtocolPacket.FromData(DataPacket.ReadFrom(_reader).Data));
            }
            finally
            {
                _baseStream.ReadTimeout = lastTimeout;
            }
        }
 public ProtocolPacket ReadPacket()
 {
     return(ProtocolPacket.FromData(DataPacket.ReadFrom(_reader).Data));
 }
        public void WritePacket(ProtocolPacket packet)
        {
            DataPacket p = new DataPacket(packet.GetData());

            p.WriteTo(_writer);
        }
Example #6
0
 public DataPacket(ProtocolPacket packet) : this(packet.GetData())
 {
 }
 public TargetProtocolPacket(string userName, ProtocolPacket packet)
 {
     UserName = userName;
     Packet = packet;
 }