Use Tor for your C# HTTP clients. Use Privoxy or .NET 6+ SOCKS support to proxy HTTP traffic.
$ dotnet add package Knapcode.TorSharpUse Tor for your C# HTTP clients. Tor + Privoxy = :heart:
All you need is client code that can use a simple HTTP proxy.
This product is produced independently from the Tor® anonymity software and carries no guarantee from The Tor Project about quality, suitability or anything else.
ExecutablePathOverride (see below)Install-Package Knapcode.TorSharp
// configure
var settings = new TorSharpSettings
{
ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
PrivoxySettings = { Port = 1337 },
TorSettings =
{
SocksPort = 1338,
ControlPort = 1339,
ControlPassword = "foobar",
},
};
// download tools
await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();
// execute
var proxy = new TorSharpProxy(settings);
var handler = new HttpClientHandler
{
Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxySettings.Port))
};
var httpClient = new HttpClient(handler);
await proxy.ConfigureAndStartAsync();
Console.WriteLine(await httpClient.GetStringAsync("http://api.ipify.org"));
await proxy.GetNewIdentityAsync();
Console.WriteLine(await httpClient.GetStringAsync("http://api.ipify.org"));
proxy.Stop();This most likely is happening because the URLs where we fetch Tor or Privoxy from are down or have changed. I would recommend:
Open an issue so I can look into it.
Work around the issue by setting up the tools manually and not using TorSharpToolFetcher. See below.
Investigate the issue yourself. The TorSharp.Sandbox project is helpful for this. Pull requests accepted 🏆.
If you don't want to use the TorSharpToolFetcher to download the latest version of the tools for you or if you want to use a specific version of Tor and Privoxy, follow these steps.
tor-win32-{version}.zip
{version} must be parsable as a System.Version meaning it is major.minor[.build[.revision]].tor-win32-0.3.5.8.zipTor\tor.exe.privoxy-win32-{version}.zip
{version} must be parsable as a System.Version.privoxy-win32-3.0.26.zipprivoxy.exe.TorSharpSettings instance where ZippedToolsDirectory is the directory created above.TorSharpProxy constructor.It's possible some expected shared libraries aren't there. Try to look at the error message and judge which library needs to be installed from your distro's package repository.
This includes Microsoft's .NET Core 3.1 runtime image mcr.microsoft.com/dotnet/core/runtime:3.1 and Microsoft's .NET 5.0 runtime image mcr.microsoft.com/dotnet/runtime:5.0.
Problem: On Debian 10 the following errors may appear:
/tmp/TorExtracted/privoxy-linux64-3.0.29/usr/sbin/privoxy: error while loading shared libraries: libbrotlidec.so.1: cannot open shared object file: No such file or directory
/tmp/TorExtracted/privoxy-linux64-3.0.29/usr/sbin/privoxy: error while loading shared libraries: libmbedtls.so.12: cannot open shared object file: No such file or directory
Solution: install two missing dependencies. Thanks for the heads up, @cod3rshotout!
[joel@debian10]$ sudo apt-get install -y libbrotli1 libmbedtls-devExecutablePathOverride.On Linux, the Privoxy binaries fetched seem to be built for the latest Debian and Ubuntu distributions. I can confirm that some other distributions don't work.
I'm no Linux expert but my guess is that there are missing shared libraries that are different on the running platform than the Debian platform that Privoxy was compiled for. The easiest workaround is to install Privoxy to your system and set the TorSharpSettings.PrivoxySetting.ExecutablePathOverride configuration setting to "privoxy" (i.e. use Privoxy from PATH).
After you install it, make sure privoxy is in the PATH.
[joel@linux]$ which privoxy
/usr/sbin/privoxyAfter this is done, just configure TorSharp to use the system Privoxy with the ExecutablePathOverride setting:
var settings = new TorSharpSettings();
settings.PrivoxySettings.ExecutablePathOverride = "privoxy";Note that you may encounter warning or error messages in the output due to new configuration being used with an older executable. I haven't ran into any problems with this myself but it's possible things could get weird.
Problem: the following error appears:
/tmp/TorExtracted/privoxy-linux64-3.0.28/usr/sbin/privoxy: error while loading shared libraries: libpcre.so.3: cannot open shared object file: No such file or directory
Solution: install Privoxy. It is available on epel-release.
[joel@centos]$ sudo yum install epel-release -y
...
[joel@centos]$ sudo yum install privoxy -yThis includes Microsoft's .NET Core 2.1 runtime image: mcr.microsoft.com/dotnet/core/runtime:2.1.
Problem: the following errors may appear:
/tmp/TorExtracted/privoxy-linux64-3.0.29/usr/sbin/privoxy: error while loading shared libraries: libbrotlidec.so.1: cannot open shared object file: No such file or directory
/tmp/TorExtracted/privoxy-linux64-3.0.29/usr/sbin/privoxy: error while loading shared libraries: libmbedtls.so.12: cannot open shared object file: No such file or directory
Solution: install Privoxy. It is available in the default source lists.
[joel@debian9]$ sudo apt-get install privoxy -y