コード例 #1
0
        public async Task<HttpResponseMessage> GetResponse(SocksHttpManagerParameters parameters)
        {
            return await HandleRedirects(parameters, async uri =>
            {
                switch (parameters.ProxyType)
                {
                    case SocksProxyTypes.CannotVerify:
                    case SocksProxyTypes.ChangesContent:
                    case SocksProxyTypes.Unchecked:
                        try
                        {
                            return await ReadHttpResponseMessage(parameters, uri, async (stream, remoteEP, cancellationToken) =>
                            {
                                await new SocksRequest().V4(stream, remoteEP, cancellationToken);
                                Application.SocksProxyHashTable.Add(GetProxyUri(parameters, uri).ToString(), SocksProxyTypes.Socks4);
                            });
                        }
                        catch (SocksRequestFailedException)
                        {
                        }

                        return await ReadHttpResponseMessage(parameters, uri, async (stream, remoteEP, cancellationToken) =>
                        {
                            await new SocksRequest().V5(stream, remoteEP, cancellationToken);
                            Application.SocksProxyHashTable.Add(GetProxyUri(parameters, uri).ToString(), SocksProxyTypes.Socks5);
                        });
                    case SocksProxyTypes.Socks4:
                        return await ReadHttpResponseMessage(parameters, uri, new SocksRequest().V4);
                    case SocksProxyTypes.Socks5:
                        return await ReadHttpResponseMessage(parameters, uri, new SocksRequest().V5);
                    default:
                        throw new InvalidOperationException(string.Format("Value '{0}' is not expected at SocksHttpManager.GetResponse", parameters.ProxyType));
                }
            });
        }
コード例 #2
0
        private async Task <HttpResponseMessage> ReadHttpResponseMessage(SocksHttpManagerParameters parameters, Uri uri, Func <NetworkStream, Uri, CancellationToken, Task> sendRequest)
        {
            using (TcpClientExtended tcpClient = new TcpClientExtended())
            {
                Uri proxyUri = GetProxyUri(parameters, uri);

                await tcpClient.ConnectAsync(proxyUri.Host, proxyUri.Port, parameters.CancellationToken);

                await sendRequest(tcpClient.GetStream(), uri, parameters.CancellationToken);

                StringBuilder responseBuilder = new StringBuilder();

                byte[] requestBytes = Encoding.UTF8.GetBytes(BuildHttpRequestMessage(parameters.Request));
                await tcpClient.GetStream().WriteAsync(requestBytes, 0, requestBytes.Length, parameters.CancellationToken);

                FireEventProgress(parameters.ReportRequestProgress, requestBytes.Length, requestBytes.Length);

                long?total         = null;
                var  buffer        = new byte[1024];
                var  bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);

                while (bytesReceived > 0)
                {
                    responseBuilder.Append(Encoding.UTF8.GetString(buffer, 0, bytesReceived));

                    if (parameters.ReportResponseProgress != null && !total.HasValue)
                    {
                        total = GetContentLength(responseBuilder.ToString());
                    }

                    if (total.HasValue)
                    {
                        FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, total);
                    }

                    bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);
                }

                HttpResponseMessage result = BuildHttpResponseMessage(responseBuilder.ToString());

                FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, responseBuilder.Length);
                return(result);
            }
        }
コード例 #3
0
        private async Task<HttpResponseMessage> ReadHttpResponseMessage(SocksHttpManagerParameters parameters, Uri uri, Func<NetworkStream, Uri, CancellationToken, Task> sendRequest)
        {
            using (TcpClientExtended tcpClient = new TcpClientExtended())
            {
                Uri proxyUri = GetProxyUri(parameters, uri);

                await tcpClient.ConnectAsync(proxyUri.Host, proxyUri.Port, parameters.CancellationToken);

                await sendRequest(tcpClient.GetStream(), uri, parameters.CancellationToken);

                StringBuilder responseBuilder = new StringBuilder();

                byte[] requestBytes = Encoding.UTF8.GetBytes(BuildHttpRequestMessage(parameters.Request));
                await tcpClient.GetStream().WriteAsync(requestBytes, 0, requestBytes.Length, parameters.CancellationToken);
                FireEventProgress(parameters.ReportRequestProgress, requestBytes.Length, requestBytes.Length);

                long? total = null;
                var buffer = new byte[1024];
                var bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);

                while (bytesReceived > 0)
                {
                    responseBuilder.Append(Encoding.UTF8.GetString(buffer, 0, bytesReceived));

                    if (parameters.ReportResponseProgress != null && !total.HasValue)
                    {
                        total = GetContentLength(responseBuilder.ToString());
                    }

                    if (total.HasValue)
                    {
                        FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, total);
                    }

                    bytesReceived = await tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length, parameters.CancellationToken);
                }

                HttpResponseMessage result = BuildHttpResponseMessage(responseBuilder.ToString());

                FireEventProgress(parameters.ReportResponseProgress, responseBuilder.Length, responseBuilder.Length);
                return result;
            }
        }
コード例 #4
0
        private async Task <HttpResponseMessage> HandleRedirects(SocksHttpManagerParameters parameters, Func <Uri, Task <HttpResponseMessage> > getResponse)
        {
            HttpResponseMessage response = await getResponse(parameters.Request.RequestUri);

            int redirectsCount = 0;

            while (parameters.Handler.AllowAutoRedirect && redirectCodes.Contains(response.StatusCode))
            {
                if (redirectsCount > parameters.Handler.MaxAutomaticRedirections)
                {
                    throw new InvalidOperationException(Resources.TooManyRedirectsWasRequestedByServer);
                }

                response = await getResponse(response.Headers.Location);

                redirectsCount++;
            }

            return(response);
        }
コード例 #5
0
        public async Task <HttpResponseMessage> GetResponse(SocksHttpManagerParameters parameters)
        {
            return(await HandleRedirects(parameters, async uri =>
            {
                switch (parameters.ProxyType)
                {
                case SocksProxyTypes.CannotVerify:
                case SocksProxyTypes.ChangesContent:
                case SocksProxyTypes.Unchecked:
                    try
                    {
                        return await ReadHttpResponseMessage(parameters, uri, async(stream, remoteEP, cancellationToken) =>
                        {
                            await new SocksRequest().V4(stream, remoteEP, cancellationToken);
                            Application.SocksProxyHashTable.Add(GetProxyUri(parameters, uri).ToString(), SocksProxyTypes.Socks4);
                        });
                    }
                    catch (SocksRequestFailedException)
                    {
                    }

                    return await ReadHttpResponseMessage(parameters, uri, async(stream, remoteEP, cancellationToken) =>
                    {
                        await new SocksRequest().V5(stream, remoteEP, cancellationToken);
                        Application.SocksProxyHashTable.Add(GetProxyUri(parameters, uri).ToString(), SocksProxyTypes.Socks5);
                    });

                case SocksProxyTypes.Socks4:
                    return await ReadHttpResponseMessage(parameters, uri, new SocksRequest().V4);

                case SocksProxyTypes.Socks5:
                    return await ReadHttpResponseMessage(parameters, uri, new SocksRequest().V5);

                default:
                    throw new InvalidOperationException(string.Format("Value '{0}' is not expected at SocksHttpManager.GetResponse", parameters.ProxyType));
                }
            }));
        }
コード例 #6
0
 private Uri GetProxyUri(SocksHttpManagerParameters parameters, Uri uri)
 {
     return parameters.Handler.Proxy.GetProxy(uri);
 }
コード例 #7
0
        private async Task<HttpResponseMessage> HandleRedirects(SocksHttpManagerParameters parameters, Func<Uri, Task<HttpResponseMessage>> getResponse)
        {
            HttpResponseMessage response = await getResponse(parameters.Request.RequestUri);
            int redirectsCount = 0;

            while (parameters.Handler.AllowAutoRedirect && redirectCodes.Contains(response.StatusCode))
            {
                if (redirectsCount > parameters.Handler.MaxAutomaticRedirections)
                {
                    throw new InvalidOperationException(Resources.TooManyRedirectsWasRequestedByServer);
                }

                response = await getResponse(response.Headers.Location);
                redirectsCount++;
            }

            return response;
        }
コード例 #8
0
 private Uri GetProxyUri(SocksHttpManagerParameters parameters, Uri uri)
 {
     return(parameters.Handler.Proxy.GetProxy(uri));
 }