joelverhagen / TorSharp

Use Tor for your C# HTTP clients. Use Privoxy or .NET 6+ SOCKS support to proxy HTTP traffic.
MIT License
323 stars 61 forks source link

Can't receive request body using TorSharp #86

Closed devgopher closed 1 year ago

devgopher commented 2 years ago

Hello! I've a client-srver app, that using your component, and I faced a problem: I can't receive request body.

On a client side I 've:

        private void  InitHttpClient()
        {
            _logger.Info($"{nameof(InitHttpClient)}() started WebProxy...");

            _webProxy = new WebProxy(new Uri($"http://localhost:{_settings.CurrentValue.InnerProxySettings.PrivoxySettings.Port}"))
            {
                BypassProxyOnLocal = false,
                Credentials = null,
                UseDefaultCredentials = true
            };

            var handler = new HttpClientHandler
            {
                Proxy = _webProxy,
                MaxRequestContentBufferSize = 100000,
                MaxResponseHeadersLength = 100000,
                PreAuthenticate = false,
                AutomaticDecompression = DecompressionMethods.All,
                UseProxy = true,
                UseDefaultCredentials = false
            };

            _httpClient = new HttpClient(handler, false)
            {
                Timeout = TimeSpan.FromMinutes(3),
                DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact,
                MaxResponseContentBufferSize = 1024576
            };
        }

        private async Task<R> InnerSend<R>(string targetUri, HttpContent content) where R : class
        {
            try
            {
                var response = await _httpClient.PostAsJsonAsync(targetUri, content);

                if (!response.IsSuccessStatusCode)
                    throw new OnionMessengerClientException(
                        $"Failed status code: {response.StatusCode} reason: {response.ReasonPhrase}!");

                var respContent = await response.Content.ReadAsStringAsync();
                if (string.IsNullOrWhiteSpace(respContent))
                    throw new OnionMessengerClientException("No content!");

                return JsonConvert.DeserializeObject<R>(respContent);
            }
            catch (Exception ex) when (ex.GetType() != typeof(OnionMessengerClientException))
            {
                _logger.Error(ex, $"Exception was thrown! {ex.Message}");
            }

            return default;
        }

on a server side I tried to make some workaround:

app.Use(async (context, next) =>
{
    if (context.Request.Path.ToString().Contains("Ingress"))
    {
        var body = context.Request.Body.ContentToString();
    }

    await next.Invoke();
});
    public static class StreamUtils
    {
        public static string ContentToString(this Stream source)
        {
            using var sr = new StreamReader(source);

            return sr.ReadLine(); // READING stucks here
        }
    }

Does anybody knows any Solution?

TorSharp v.2.8.1/.Net Core 6/Windows 10

joelverhagen commented 2 years ago

Could you provide a full repro in a Gist or GitHub repo? This will help us understand.

joelverhagen commented 1 year ago

I haven't heard back from you. I'll close this now.