Beispiel #1
0
        public async void TestConnectByIPv4Async()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    socks  = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port);
                var    host   = "74.125.197.99";            // ResolveIPv4 ("www.google.com");
                Socket socket = null;

                if (host == null)
                {
                    return;
                }

                try {
                    socket = await socks.ConnectAsync(host, 80, ConnectTimeout);

                    socket.Disconnect(false);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    if (socket != null)
                    {
                        socket.Dispose();
                    }
                }
            }
        }
Beispiel #2
0
        public async void TestConnectByIPv6Async()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    socks  = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port);
                var    host   = "2607:f8b0:400e:c03::69";            // ResolveIPv6 ("www.google.com");
                Socket socket = null;

                if (host == null)
                {
                    return;
                }

                try {
                    socket = await socks.ConnectAsync(host, 80, ConnectTimeout);

                    socket.Disconnect(false);
                } catch (ProxyProtocolException ex) {
                    Assert.IsTrue(ex.Message.StartsWith($"Failed to connect to {host}:80: ", StringComparison.Ordinal), ex.Message);
                    Assert.Inconclusive(ex.Message);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    if (socket != null)
                    {
                        socket.Dispose();
                    }
                }
            }
        }
Beispiel #3
0
        public async void TestConnectWithBadCredentialsAsync()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    credentials = new NetworkCredential("username", "bad");
                var    socks       = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port, credentials);
                Socket socket      = null;

                try {
                    socket = await socks.ConnectAsync("www.google.com", 80, ConnectTimeout);

                    socket.Disconnect(false);
                    Assert.Fail("Expected AuthenticationException");
                } catch (AuthenticationException) {
                    // this is what we expect to get
                    Assert.Pass("Got an AuthenticationException just as expected");
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    if (socket != null)
                    {
                        socket.Dispose();
                    }
                }
            }
        }
Beispiel #4
0
        public async Task TestTimeoutException()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    socks  = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port);
                Stream stream = null;

                try {
                    stream = await socks.ConnectAsync("example.com", 25, 1000);
                } catch (TimeoutException) {
                    Assert.Pass();
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    stream?.Dispose();
                }
            }
        }
Beispiel #5
0
        public async Task TestConnectAnonymousAsync()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    socks  = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port);
                Stream stream = null;

                try {
                    stream = await socks.ConnectAsync("www.google.com", 80, ConnectTimeout);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    stream?.Dispose();
                }
            }
        }
Beispiel #6
0
        public void TestConnectAnonymous()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    socks  = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port);
                Socket socket = null;

                try {
                    socket = socks.Connect("www.google.com", 80, ConnectTimeout);
                    socket.Disconnect(false);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    if (socket != null)
                    {
                        socket.Dispose();
                    }
                }
            }
        }
Beispiel #7
0
        public void TestConnectWithCredentials()
        {
            using (var proxy = new Socks5ProxyListener()) {
                proxy.Start(IPAddress.Loopback, 0);

                var    credentials = new NetworkCredential("username", "password");
                var    socks       = new Socks5Client(proxy.IPAddress.ToString(), proxy.Port, credentials);
                Stream stream      = null;

                try {
                    stream = socks.Connect("www.google.com", 80, ConnectTimeout);
                } catch (AuthenticationException) {
                    // this is what we expect to get
                    Assert.Pass("Got an AuthenticationException just as expected");
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    stream?.Dispose();
                }
            }
        }