Example #1
0
        public override void Write(byte[] buffer, int offset, int size)
        {
            if (size == 0)
            {
                return;
            }

            _WriteLock.WaitOne();
            _ReconnLock.AcquireReaderLock(-1);
            try {
                if (_EnableCrypt)
                {
                    _WriteCipher.XORKeyStream(buffer, offset, buffer, offset, size);
                }
                _Rewriter.Push(buffer, offset, size);
                _WriterCount += (ulong)size;

                try {
                    _BaseStream.Write(buffer, offset, size);
                } catch {
                    if (!tryReconn())
                    {
                        throw;
                    }
                }
            } finally {
                _ReconnLock.ReleaseReaderLock();
                _WriteLock.ReleaseMutex();
            }
        }
Example #2
0
        private void handshake()
        {
            byte[] request  = new byte[24 + 16];
            byte[] response = request;

            ulong privateKey;
            ulong publicKey;
            DH64  dh64 = new DH64();

            dh64.KeyPair(out privateKey, out publicKey);

            using (MemoryStream ms = new MemoryStream(request, 8, 8)) {
                using (BinaryWriter w = new BinaryWriter(ms)) {
                    w.Write(publicKey);
                }
            }

            TcpClient client = new TcpClient();
            var       ar     = client.BeginConnect(_Host, _Port, null, null);

            ar.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, ConnectTimeout));
            if (!ar.IsCompleted)
            {
                throw new TimeoutException();
            }
            client.EndConnect(ar);

            setBaseStream(client.GetStream());
            _BaseStream.Write(request, 0, request.Length);

            for (int n = 16; n > 0;)
            {
                int x = _BaseStream.Read(response, 16 - n, n);
                if (x == 0)
                {
                    throw new EndOfStreamException();
                }
                n -= x;
            }

            using (MemoryStream ms = new MemoryStream(response, 0, 16)) {
                using (BinaryReader r = new BinaryReader(ms)) {
                    ulong serverPublicKey = r.ReadUInt64();
                    ulong secret          = dh64.Secret(privateKey, serverPublicKey);

                    using (MemoryStream ms2 = new MemoryStream(_Key)) {
                        using (BinaryWriter w = new BinaryWriter(ms2)) {
                            w.Write(secret);
                        }
                    }

                    _ReadCipher  = new RC4Cipher(_Key);
                    _WriteCipher = new RC4Cipher(_Key);
                    _ReadCipher.XORKeyStream(response, 8, response, 8, 8);

                    _ID = r.ReadUInt64();
                }
            }
        }
Example #3
0
        private void handshake()
        {
            byte[] request  = new byte[24 + 16];
            byte[] response = request;

            ulong privateKey;
            ulong publicKey;
            DH64  dh64 = new DH64();

            dh64.KeyPair(out privateKey, out publicKey);

            using (MemoryStream ms = new MemoryStream(request, 8, 8)) {
                using (BinaryWriter w = new BinaryWriter(ms)) {
                    w.Write(publicKey);
                }
            }

            TcpClient client = new TcpClient(_Host, _Port);

            setBaseStream(client.GetStream());
            _BaseStream.Write(request, 0, request.Length);

            for (int n = 16; n > 0;)
            {
                n -= _BaseStream.Read(response, 16 - n, n);
            }

            using (MemoryStream ms = new MemoryStream(response, 0, 16)) {
                using (BinaryReader r = new BinaryReader(ms)) {
                    ulong serverPublicKey = r.ReadUInt64();
                    ulong secret          = dh64.Secret(privateKey, serverPublicKey);

                    using (MemoryStream ms2 = new MemoryStream(_Key)) {
                        using (BinaryWriter w = new BinaryWriter(ms2)) {
                            w.Write(secret);
                        }
                    }

                    _ReadCipher  = new RC4Cipher(_Key);
                    _WriteCipher = new RC4Cipher(_Key);
                    _ReadCipher.XORKeyStream(response, 8, response, 8, 8);

                    _ID = r.ReadUInt64();
                }
            }
        }
Example #4
0
        public override int Read(byte[] buffer, int offset, int size)
        {
            _ReadLock.WaitOne();
            _ReconnLock.AcquireReaderLock(-1);
            int n = 0;

            try {
                for (;;)
                {
                    n = _Rereader.Pull(buffer, offset, size);
                    if (n > 0)
                    {
                        return(n);
                    }

                    try {
                        n = _BaseStream.Read(buffer, offset + n, size);
                        if (n == 0)
                        {
                            if (!tryReconn())
                            {
                                throw new IOException();
                            }
                            continue;
                        }
                    } catch {
                        if (!tryReconn())
                        {
                            throw;
                        }
                        continue;
                    }
                    break;
                }
            } finally {
                if (n > 0 && _EnableCrypt)
                {
                    _ReadCipher.XORKeyStream(buffer, offset, buffer, offset, n);
                }
                _ReadCount += (ulong)n;
                _ReconnLock.ReleaseReaderLock();
                _ReadLock.ReleaseMutex();
            }
            return(n);
        }
Example #5
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     count = stream.Read(buffer, offset, count);
     cipher.XORKeyStream(buffer, offset, buffer, offset, count);
     return(count);
 }
Example #6
0
        private void handshake()
        {
            byte[] preRequest = new byte[1];
            byte[] request    = new byte[24];
            byte[] response   = request;

            preRequest[0] = TypeNewconn;

            ulong privateKey;
            ulong publicKey;
            DH64  dh64 = new DH64();

            dh64.KeyPair(out privateKey, out publicKey);

            using (MemoryStream ms = new MemoryStream(request, 0, 8)) {
                using (BinaryWriter w = new BinaryWriter(ms)) {
                    w.Write(publicKey);
                }
            }

            TcpClient client = new TcpClient(_Host.AddressFamily);
            var       ar     = client.BeginConnect(_Host, _Port, null, null);

            ar.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, ConnectTimeout));
            if (!ar.IsCompleted)
            {
                throw new TimeoutException();
            }
            client.EndConnect(ar);

            setBaseStream(client.GetStream());
            _BaseStream.Write(preRequest, 0, preRequest.Length);
            _BaseStream.Write(request, 0, 8);

            for (int n = 24; n > 0;)
            {
                int x = _BaseStream.Read(response, 24 - n, n);
                if (x == 0)
                {
                    throw new EndOfStreamException();
                }
                n -= x;
            }

            ulong challengeCode = 0;

            using (MemoryStream ms = new MemoryStream(response, 0, 24))
            {
                using (BinaryReader r = new BinaryReader(ms))
                {
                    ulong serverPublicKey = r.ReadUInt64();
                    ulong secret          = dh64.Secret(privateKey, serverPublicKey);

                    using (MemoryStream ms2 = new MemoryStream(_Key))
                    {
                        using (BinaryWriter w = new BinaryWriter(ms2))
                        {
                            w.Write(secret);
                        }
                    }

                    _ReadCipher  = new RC4Cipher(_Key);
                    _WriteCipher = new RC4Cipher(_Key);
                    _ReadCipher.XORKeyStream(response, 8, response, 8, 8);

                    _ID = r.ReadUInt64();

                    using (MemoryStream ms2 = new MemoryStream(request, 0, 16))
                    {
                        using (BinaryWriter w = new BinaryWriter(ms2))
                        {
                            w.Write(response, 16, 8);
                            w.Write(_Key);
                            MD5    md5  = MD5CryptoServiceProvider.Create();
                            byte[] hash = md5.ComputeHash(request, 0, 16);
                            Buffer.BlockCopy(hash, 0, request, 0, hash.Length);
                            _BaseStream.Write(request, 0, 16);
                        }
                    }
                }
            }
        }
Example #7
0
        private void secondHandshake()
        {
            byte [] preRequest = new byte [1];
            byte [] request    = new byte [24];
            byte [] response   = request;

            preRequest [0] = TypeNewconn;

            ulong privateKey;
            ulong publicKey;
            DH64  dh64 = new DH64();

            dh64.KeyPair(out privateKey, out publicKey);

            using (MemoryStream ms = new MemoryStream(request, 0, 8)) {
                using (BinaryWriter w = new BinaryWriter(ms)) {
                    w.Write(publicKey);
                }
            }
            _BaseStream.Write(preRequest, 0, preRequest.Length);
            _BaseStream.Write(request, 0, 8);

            for (int n = 24; n > 0;)
            {
                int x = _BaseStream.Read(response, 24 - n, n);
                if (x == 0)
                {
                    throw new EndOfStreamException();
                }
                n -= x;
            }

            ulong challengeCode = 0;

            using (MemoryStream ms = new MemoryStream(response, 0, 24)) {
                using (BinaryReader r = new BinaryReader(ms)) {
                    ulong serverPublicKey = r.ReadUInt64();
                    ulong secret          = dh64.Secret(privateKey, serverPublicKey);

                    using (MemoryStream ms2 = new MemoryStream(_Key)) {
                        using (BinaryWriter w = new BinaryWriter(ms2)) {
                            w.Write(secret);
                        }
                    }

                    _ReadCipher  = new RC4Cipher(_Key);
                    _WriteCipher = new RC4Cipher(_Key);
                    _ReadCipher.XORKeyStream(response, 8, response, 8, 8);

                    _ID = r.ReadUInt64();

                    using (MemoryStream ms2 = new MemoryStream(request, 0, 16)) {
                        using (BinaryWriter w = new BinaryWriter(ms2)) {
                            w.Write(response, 16, 8);
                            w.Write(_Key);
                            MD5     md5  = MD5CryptoServiceProvider.Create();
                            byte [] hash = md5.ComputeHash(request, 0, 16);
                            Buffer.BlockCopy(hash, 0, request, 0, hash.Length);
                            _BaseStream.Write(request, 0, 16);
                        }
                    }
                }
            }
        }