Use Tor for your C# HTTP clients. Tor + Privoxy = ❤️
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.
- Supports:
- .NET Core (.NET Standard 2.0 and later)
- .NET Framework (.NET Framework 4.5 and later)
- Windows
- ✔️ Windows 10 version 1903
- Older Windows should work too
- Linux
- ✔️ Ubuntu 18.04
- ✔️ Ubuntu 16.04
- ✔️ Debian 10
- ✔️ Debian 9 (confirmed by user)
⚠️ CentOS 7 supported viaExecutablePathOverride
(see below)
- ❌ Mac OS X support is not planned. I don't have a Mac 😕
- Uses Privoxy to redirect HTTP proxy traffic to Tor.
- Uses virtual desktops to manage Tor and Privoxy processes.
- Optionally downloads the latest version of Tor and Privoxy.
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.
- Make a directory that will hold the zipped Tor and Privoxy binaries.
- Put a Tor Win32 ZIP in that folder with the file name like:
tor-win32-{version}.zip
{version}
must be parsable as aSystem.Version
meaning it ismajor.minor[.build[.revision]]
.- Example:
tor-win32-0.3.5.8.zip
- The ZIP is expected to have
Tor\tor.exe
.
- Put a Privoxy Win32 ZIP in that folder with a file name like:
privoxy-win32-{version}.zip
- Again,
{version}
must be parsable as aSystem.Version
. - Example:
privoxy-win32-3.0.26.zip
- The ZIP is expected to have
privoxy.exe
.
- Again,
- Initialize a
TorSharpSettings
instance whereZippedToolsDirectory
is the directory created above. - Pass this settings instance to the
TorSharpProxy
constructor.
On Linux, the Privoxy binaries fetched seem to be built for Debian and Ubuntu distributions. I can confirm that some other distributions don't work. For example, on CentOS 7 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
I'm no Linux expert but my guess is that the version of PCRE on CentOS 7 vs. Ubuntu is different so at runtime the
library can't be found on CentOS. 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).
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.
Install Privoxy. It is available on epel-release
.
[joel@centos]$ sudo yum install epel-release -y
...
[joel@centos]$ sudo yum install privoxy -y
Ensure Privoxy is available in PATH.
[joel@centos]$ which privoxy
/usr/sbin/privoxy
Set the Privoxy ExecutablePathOverride
to "privoxy"
.
var settings = new TorSharpSettings();
settings.PrivoxySettings.ExecutablePathOverride = "privoxy";