Exemplo n.º 1
0
        public void Test_ReceiveDataAfterHandshake()
        {
            var data           = new byte[] { 2, 4, 6 };
            var encryptedData  = new byte[] { 4, 6, 8 };
            var mockAsymmetric = new Mock <IAsymmetricCipher>();

            mockAsymmetric.Setup(m => m.Decrypt(encryptedData)).Returns(data).Verifiable();

            var mockSymmetric = new Mock <ISymmetricCipher>();

            mockSymmetric.Setup(m => m.Decrypt(encryptedData)).Returns(data).Verifiable();

            var mockConnection = new Mock <IConnection>();

            var secureClient = new SecureClientConnection(mockConnection.Object, mockAsymmetric.Object, mockSymmetric.Object);

            mockConnection.Raise(i => i.OnDataReceived += null, new byte[] { });
            mockConnection.Raise(i => i.OnDataReceived += null, new byte[] { });
            byte[] receivedData = null;
            secureClient.OnDataReceived += (data) => receivedData = data;

            //Act
            mockConnection.Raise(i => i.OnDataReceived += null, encryptedData);

            mockSymmetric.Verify(m => m.Decrypt(encryptedData));
            Assert.AreEqual(data, receivedData);
            mockAsymmetric.Verify(m => m.Decrypt(It.IsAny <byte[]>()), Times.Exactly(2));
        }
Exemplo n.º 2
0
        public void Test_StartHandshake()
        {
            var publicKey      = new byte[] { 1, 2, 3 };
            var mockAsymmetric = new Mock <IAsymmetricCipher>();

            mockAsymmetric.Setup(m => m.PublicKey).Returns(publicKey);
            var mockSymmetric  = new Mock <ISymmetricCipher>();
            var mockConnection = new Mock <IConnection>();

            var secureClient = new SecureClientConnection(mockConnection.Object, mockAsymmetric.Object, mockSymmetric.Object);

            secureClient.Start();
            bool dataReceivedHasBeenCalled = false;

            secureClient.OnDataReceived += (data) => { dataReceivedHasBeenCalled = true; };

            mockConnection.Verify(m => m.SendData(publicKey));
            Assert.IsFalse(dataReceivedHasBeenCalled);
        }
Exemplo n.º 3
0
        public void Test_EndHandshake()
        {
            var publicKey      = new byte[] { 1, 2, 3 };
            var iv             = new byte[] { 1, 2 };
            var key            = new byte[] { 1, 2 };
            var mockAsymmetric = new Mock <IAsymmetricCipher>();

            mockAsymmetric.Setup(m => m.PublicKey).Returns(publicKey);
            mockAsymmetric.Setup(m => m.Decrypt(iv)).Returns(iv);
            mockAsymmetric.Setup(m => m.Decrypt(key)).Returns(key);

            var mockSymmetric = new Mock <ISymmetricCipher>();

            mockSymmetric.SetupSet(m => m.IV  = It.IsAny <byte[]>()).Verifiable();
            mockSymmetric.SetupSet(m => m.Key = It.IsAny <byte[]>()).Verifiable();

            var mockConnection = new Mock <IConnection>();

            var secureClient = new SecureClientConnection(mockConnection.Object, mockAsymmetric.Object, mockSymmetric.Object);

            secureClient.Start();

            bool OnConnectedHasBeenCalled = false;

            secureClient.OnConnected += () => { OnConnectedHasBeenCalled = true; };
            // Act
            mockConnection.Raise(i => i.OnDataReceived += null, iv);
            mockConnection.Raise(i => i.OnDataReceived += null, key);

            mockAsymmetric.Verify(m => m.Decrypt(iv));
            mockAsymmetric.Verify(m => m.Decrypt(key));
            mockSymmetric.VerifySet(m => m.IV  = iv);
            mockSymmetric.VerifySet(m => m.Key = key);

            Assert.IsTrue(OnConnectedHasBeenCalled);
        }
Exemplo n.º 4
0
        public ConnectionBase CreateConnection(string uri)
        {
            Socket         socket     = null;
            ConnectionBase connection = null;

            try
            {
                if (this.ConnectionType == ConnectionType.Tcp)
                {
#if !DEBUG
                    {
                        Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                        var   match = regex.Match(uri);
                        Uri   url   = new Uri(string.Format("{0}://{1}:{2}", match.Groups[1], match.Groups[2], match.Groups[3]));

                        if (url.HostNameType == UriHostNameType.IPv4)
                        {
                            var myIpAddress = IPAddress.Parse(url.Host);

                            if (IPAddress.Any.ToString() == myIpAddress.ToString() ||
                                IPAddress.Loopback.ToString() == myIpAddress.ToString() ||
                                IPAddress.Broadcast.ToString() == myIpAddress.ToString())
                            {
                                return(null);
                            }
                            if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("10.0.0.0").GetAddressBytes()) >= 0 &&
                                Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("10.255.255.255").GetAddressBytes()) <= 0)
                            {
                                return(null);
                            }
                            if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("172.16.0.0").GetAddressBytes()) >= 0 &&
                                Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("172.31.255.255").GetAddressBytes()) <= 0)
                            {
                                return(null);
                            }
                            if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("127.0.0.0").GetAddressBytes()) >= 0 &&
                                Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("127.255.255.255").GetAddressBytes()) <= 0)
                            {
                                return(null);
                            }
                            if (Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("192.168.0.0").GetAddressBytes()) >= 0 &&
                                Collection.Compare(myIpAddress.GetAddressBytes(), IPAddress.Parse("192.168.255.255").GetAddressBytes()) <= 0)
                            {
                                return(null);
                            }
                        }
                        else if (url.HostNameType == UriHostNameType.IPv6)
                        {
                            var myIpAddress = IPAddress.Parse(url.Host);

                            if (IPAddress.IPv6Any.ToString() == myIpAddress.ToString() ||
                                IPAddress.IPv6Loopback.ToString() == myIpAddress.ToString() ||
                                IPAddress.IPv6None.ToString() == myIpAddress.ToString())
                            {
                                return(null);
                            }
                            if (myIpAddress.ToString().ToLower().StartsWith("fe80:"))
                            {
                                return(null);
                            }
                        }
                    }
#endif

                    connection = new TcpConnection(SessionManager.Connect(SessionManager.GetIpEndPoint(uri), new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager);
                }
                else if (this.ConnectionType == ConnectionType.Socks4Proxy)
                {
                    Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                    var   match = regex.Match(uri);
                    if (!match.Success)
                    {
                        return(null);
                    }

                    socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10));
                    var proxy = new Socks4ProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value));

                    connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager);
                }
                else if (this.ConnectionType == ConnectionType.Socks4aProxy)
                {
                    Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                    var   match = regex.Match(uri);
                    if (!match.Success)
                    {
                        return(null);
                    }

                    socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10));
                    var proxy = new Socks4aProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value));

                    connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager);
                }
                else if (this.ConnectionType == ConnectionType.Socks5Proxy)
                {
                    Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                    var   match = regex.Match(uri);
                    if (!match.Success)
                    {
                        return(null);
                    }

                    socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10));
                    var proxy = new Socks5ProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value));

                    connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager);
                }
                else if (this.ConnectionType == ConnectionType.HttpProxy)
                {
                    Regex regex = new Regex(@"(.*?):(.*):(\d*)");
                    var   match = regex.Match(uri);
                    if (!match.Success)
                    {
                        return(null);
                    }

                    socket = SessionManager.Connect(SessionManager.GetIpEndPoint(this.ProxyUri), new TimeSpan(0, 0, 10));
                    var proxy = new HttpProxyClient(socket, match.Groups[2].Value, int.Parse(match.Groups[3].Value));

                    connection = new TcpConnection(proxy.CreateConnection(new TimeSpan(0, 0, 10)), SessionManager.MaxReceiveCount, _bufferManager);
                }

                var secureConnection = new SecureClientConnection(connection, null, _bufferManager);
                secureConnection.Connect(new TimeSpan(0, 0, 20));

                return(new CompressConnection(secureConnection, SessionManager.MaxReceiveCount, _bufferManager));
            }
            catch (Exception)
            {
                if (socket != null)
                {
                    socket.Close();
                }
                if (connection != null)
                {
                    connection.Dispose();
                }
            }

            return(null);
        }