Example #1
0
        public void TestArgumentExceptions()
        {
            var credentials = new NetworkCredential("user", "password");
            var proxy       = new HttpProxyClient("http.proxy.com", 0, credentials);

            Assert.Throws <ArgumentNullException> (() => new HttpProxyClient(null, 1080));
            Assert.Throws <ArgumentException> (() => new HttpProxyClient(string.Empty, 1080));
            Assert.Throws <ArgumentOutOfRangeException> (() => new HttpProxyClient(proxy.ProxyHost, -1));
            Assert.Throws <ArgumentNullException> (() => new HttpProxyClient(proxy.ProxyHost, 1080, null));

            Assert.AreEqual(1080, proxy.ProxyPort);
            Assert.AreEqual("http.proxy.com", proxy.ProxyHost);
            Assert.AreEqual(credentials, proxy.ProxyCredentials);

            Assert.Throws <ArgumentNullException> (() => proxy.Connect(null, 80));
            Assert.Throws <ArgumentNullException> (() => proxy.Connect(null, 80, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 80));
            Assert.ThrowsAsync <ArgumentNullException> (async() => await proxy.ConnectAsync(null, 80, ConnectTimeout));

            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 80));
            Assert.Throws <ArgumentException> (() => proxy.Connect(string.Empty, 80, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 80));
            Assert.ThrowsAsync <ArgumentException> (async() => await proxy.ConnectAsync(string.Empty, 80, ConnectTimeout));

            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 0, ConnectTimeout));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 0));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 0, ConnectTimeout));

            Assert.Throws <ArgumentOutOfRangeException> (() => proxy.Connect("www.google.com", 80, -ConnectTimeout));
            Assert.ThrowsAsync <ArgumentOutOfRangeException> (async() => await proxy.ConnectAsync("www.google.com", 80, -ConnectTimeout));
        }
        public async Task ConnectAsync_HttpProxyClient_Invalid()
        {
            // Set an invalid proxy
            var settings = new ProxySettings()
            {
                Host = "example.com", Port = 80
            };
            var proxy = new HttpProxyClient(settings);

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            await Assert.ThrowsAsync <ProxyException>(async() => await proxy.ConnectAsync("example.com", 80, null, cts.Token));
        }
        public async Task ConnectAsync_HttpProxyClient_Http()
        {
            var settings = new ProxySettings()
            {
                Host = "127.0.0.1", Port = 8888
            };
            var proxy = new HttpProxyClient(settings);

            var cts    = new CancellationTokenSource(TimeSpan.FromSeconds(5));
            var client = await proxy.ConnectAsync("example.com", 80, null, cts.Token);

            var response = await GetResponseAsync(client, BuildSampleGetRequest(), cts.Token);

            Assert.Contains("Example Domain", response);
        }
Example #4
0
        public async Task TestMethodNotAllowedAsync()
        {
            var    proxy  = new HttpProxyClient("www.google.com", 80);
            Stream stream = null;

            try {
                stream = await proxy.ConnectAsync("www.google.com", 80);

                Assert.Fail("www.google.com is not an HTTP proxy, so CONNECT should have failed.");
            } catch (ProxyProtocolException ex) {
                // This is expected since this proxy does not support Socks4a
                Assert.AreEqual("Failed to connect to www.google.com:80: HTTP/1.1 405 Method Not Allowed", ex.Message);
            } catch (TimeoutException) {
                Assert.Inconclusive("Timed out.");
            } catch (Exception ex) {
                Assert.Fail(ex.Message);
            } finally {
                stream?.Dispose();
            }
        }
Example #5
0
        public async Task TestConnectWithCredentialsAsync()
        {
            using (var server = new HttpProxyListener()) {
                server.Start(IPAddress.Loopback, 0);

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

                try {
                    stream = await proxy.ConnectAsync("www.google.com", 80, ConnectTimeout);
                } catch (TimeoutException) {
                    Assert.Inconclusive("Timed out.");
                } catch (Exception ex) {
                    Assert.Fail(ex.Message);
                } finally {
                    stream?.Dispose();
                }
            }
        }
Example #6
0
        public async ValueTask <ICap?> ConnectAsync(OmniAddress address, CancellationToken cancellationToken = default)
        {
            using (await _asyncLock.LockAsync())
            {
                this.ThrowIfDisposingRequested();

                var config = _tcpConnectOptions;
                if (config == null || !config.Enabled)
                {
                    return(null);
                }

                if (!TryGetEndpoint(address, out var ipAddress, out ushort port))
                {
                    return(null);
                }

                var disposableList = new List <IDisposable>();

                try
                {
#if !DEBUG
                    if (!IsGlobalIpAddress(ipAddress))
                    {
                        return(null);
                    }
#endif

                    if (config.ProxyOptions != null)
                    {
                        if (!TryGetEndpoint(config.ProxyOptions.Address, out var proxyAddress, out ushort proxyPort, true))
                        {
                            return(null);
                        }

                        if (config.ProxyOptions.Type == TcpProxyType.Socks5Proxy)
                        {
                            var socket = await ConnectAsync(new IPEndPoint(proxyAddress, proxyPort));

                            if (socket == null)
                            {
                                return(null);
                            }

                            disposableList.Add(socket);

                            var proxy = new Socks5ProxyClient(ipAddress.ToString(), port);
                            await proxy.ConnectAsync(socket, cancellationToken);

                            var cap = new SocketCap(socket);
                            disposableList.Add(cap);

                            return(cap);
                        }
                        else if (config.ProxyOptions.Type == TcpProxyType.HttpProxy)
                        {
                            var socket = await ConnectAsync(new IPEndPoint(proxyAddress, proxyPort));

                            if (socket == null)
                            {
                                return(null);
                            }

                            disposableList.Add(socket);

                            var proxy = new HttpProxyClient(ipAddress.ToString(), port);
                            await proxy.ConnectAsync(socket, cancellationToken);

                            var cap = new SocketCap(socket);
                            disposableList.Add(cap);

                            return(cap);
                        }
                    }
                    else
                    {
                        var socket = await ConnectAsync(new IPEndPoint(ipAddress, port));

                        if (socket == null)
                        {
                            return(null);
                        }

                        disposableList.Add(socket);

                        var cap = new SocketCap(socket);
                        disposableList.Add(cap);

                        return(cap);
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e);

                    foreach (var item in disposableList)
                    {
                        item.Dispose();
                    }
                }

                return(null);
            }
        }