Giter Site home page Giter Site logo

torsharp's Introduction

TorSharp

Use Tor for your C# HTTP clients. Use Privoxy or .NET 6+ SOCKS support to proxy HTTP traffic.

All you need is client code that can use a simple HTTP proxy.

NuGet downloads Build

Notice

This product is produced independently from the Tor® anonymity software and carries no guarantee from The Tor Project about quality, suitability or anything else.

Details

  • Supports:
    • .NET Core (.NET Standard 2.0 and later, including .NET 5+)
    • .NET Framework (.NET Framework 4.6.2 and later)
    • Windows
      • ✔️ Windows 10 version 1903
      • ✔️ Windows 11 version 21H2
      • Older Windows should work too
    • Linux
      • ✔️ Ubuntu 20.04
      • ✔️ Ubuntu 18.04
      • ✔️ Ubuntu 16.04
      • ✔️ Debian 10
      • ⚠️ Debian 9 (confirmed by a user but may have issues)
      • ⚠️ CentOS 7 supported via ExecutablePathOverride (see below)
      • ⚠️ Alpine 3.18 supported via ExecutablePathOverride (must run apk add tor privoxy or apk add tor and disable Privoxy)
    • ❌ Mac OS X support is not planned. I don't have a Mac 😕
  • Uses Privoxy to redirect HTTP proxy traffic to Tor (can be disabled).
  • On Windows, uses virtual desktops to manage Tor and Privoxy processes and hide the windows so it's cleaner.
  • Optionally downloads the latest version of Tor and Privoxy for you.

Install

dotnet add package Knapcode.TorSharp

Example using .NET 6+ SOCKS support

Starting on .NET 6, there is built-in support for SOCKS proxies. This means you don't need Privoxy. Thanks, .NET team!

See samples/NativeSocksProxy/Program.cs for a working sample.

var settings = new TorSharpSettings
{
    PrivoxySettings = { Disable = true }
};

// download Tor
using (var httpClient = new HttpClient())
{
    var fetcher = new TorSharpToolFetcher(settings, httpClient);
    await fetcher.FetchAsync();
}

// execute
using (var proxy = new TorSharpProxy(settings))
{
    await proxy.ConfigureAndStartAsync();

    var handler = new HttpClientHandler
    {
        Proxy = new WebProxy(new Uri("socks5://localhost:" + settings.TorSettings.SocksPort))
    };

    using (handler)
    using (var httpClient = new HttpClient(handler))
    {
        var result = await httpClient.GetStringAsync("https://check.torproject.org/api/ip");

        Console.WriteLine();
        Console.WriteLine("Are we using Tor?");
        Console.WriteLine(result);
    }

    proxy.Stop();
}

Example using Privoxy

See samples/TorSharp.Sandbox/Program.cs for a working sample.

// 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();

FAQ

The tool fetcher is throwing an exception. What do I do?

This most likely is happening because the URLs where we fetch Tor or Privoxy from are down or have changed. I would recommend:

  1. Open an issue so I can look into it.

  2. Work around the issue by setting up the tools manually and not using TorSharpToolFetcher. See below.

  3. Investigate the issue yourself. The TorSharp.Sandbox project is helpful for this. Pull requests accepted 🏆.

How do I set up the tools manually?

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.

  1. Make a directory that will hold the zipped Tor and Privoxy binaries.
  2. Put a Tor Win32 ZIP in that folder with the file name like: tor-win32-{version}.zip
    • {version} must be parsable as a System.Version meaning it is major.minor[.build[.revision]].
    • Example: tor-win32-0.3.5.8.zip
    • The ZIP is expected to have Tor\tor.exe.
  3. Put a Privoxy Win32 ZIP in that folder with a file name like: privoxy-win32-{version}.zip
    • Again, {version} must be parsable as a System.Version.
    • Example: privoxy-win32-3.0.26.zip
    • The ZIP is expected to have privoxy.exe.
  4. Initialize a TorSharpSettings instance where ZippedToolsDirectory is the directory created above.
  5. Pass this settings instance to the TorSharpProxy constructor.

Can I run multiple instances in parallel?

Yes, you can. See this sample: samples/MultipleInstances/Program.cs.

However, you need to adhere to the following guidance.

None of the types in this library should be considered thread safe. Use separate instances for each parallel task/thread.

  • TorSharpProxy: this is stateful and should not be shared.
  • TorSharpSettings: the values held in the settings class need to be different for parallel threads, so it doesn't make sense to share instances.
  • TorSharpToolFetcher: this is stateless so it may be safe, but I would keep this a singleton since you shouldn't have multiple copies of the zipped tools (just multiple copies of the extracted tools).

Parallel threads must have different values for these settings. The defaults will not work.

  • Must be made unique by you:
    • TorSharpSettings.ExtractedToolsDirectory: this is the parent directory of the tool working directories. Specify a different value for each thread. In the sample above, I see each parallel task to be a sibling directory, e.g. {some_root}/a, {some_root}/b, etc.
    • TorSharpSettings.PrivoxySettings.Port: this is the Privoxy listen port. Each Privoxy process needs its own port. Can be ignored if TorSharpSettings.PrivoxySettings.Disable is true.
    • TorSharpSettings.TorSettings.SocksPort: this is the Tor SOCKS listen port. Each Tor process needs its own port.
  • Must be unique, but only if you set them:
    • TorSharpSettings.VirtualDesktopName: this is automatically generated based on ExtractedToolsDirectory, but if you manually set it, it must be unique.
    • TorSharpSettings.TorSettings.ControlPort: this is the Tor SOCKS listen port. Each Tor process needs its own port.
    • TorSharpSettings.TorSettings.AdditionalSockPorts: if used, it must have unique values.
    • TorSharpSettings.TorSettings.HttpTunnelPort: if used, it must have a unique value.
    • TorSharpSettings.TorSettings.DataDirectory: the default is based ExtractedToolsDirectory, but if you manually set it, it must be unique.

In general, directory configuration values must be different from all of the other directories, except TorSharpSettings.ZippedToolsDirectory which should not be downloaded to in parallel by TorSharpToolFetcher but can be read from in parallel with multiple TorSharpProxy instances. Port configuration values need to all be unique.

How do I change what TorSharp logs?

By default, TorSharp lets the tools (Tor, Privoxy) log to the main process stdout and stderr. If you want to disable this behavior, set TorSharpSettings.WriteToConsole to false. If you want to intercept the output from the tools, attach to the TorSharpProxy.OutputDataReceived (for stdout) and TorSharpProxy.ErrorDataReceived (for stderr) events. In your event handler, you can log to some external sink or enqueue the line for processing. The event handlers are fired from a task using the default task scheduler so this blocks one of the shared worker threads. Don't do too much heavy lifting there, I guess! If you want to know which tool sent the log message, look at the DataEventArgs.ExecutablePath property.

For a full sample, see this: samples/CustomLogging/Program.cs.

Privoxy fetched by TorSharp fails to start? Try installing missing dependencies.

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.

Ubuntu 20.04

Problem: On Ubuntu 20.04 the following errors may appear:

/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 the missing dependencies. Note that these steps install packages from the Debian repository. This is very much not recommended by official guidance. For my own testing, it works well enough. I use this trick in the GitHub Action workflow. ⚠️ Do this at your own risk!

[joel@ubuntu]$ curl -O http://ftp.us.debian.org/debian/pool/main/m/mbedtls/libmbedcrypto3_2.16.0-1_amd64.deb
[joel@ubuntu]$ curl -O http://ftp.us.debian.org/debian/pool/main/m/mbedtls/libmbedx509-0_2.16.0-1_amd64.deb
[joel@ubuntu]$ curl -O http://ftp.us.debian.org/debian/pool/main/m/mbedtls/libmbedtls12_2.16.0-1_amd64.deb
[joel@ubuntu]$ echo "eb6751c98adfdf0e7a5a52fbd1b5a284ffd429e73abb4f0a4497374dd9f142c7 libmbedcrypto3_2.16.0-1_amd64.deb" > SHA256SUMS
[joel@ubuntu]$ echo "e8ea5dd71b27591c0f55c1d49f597d3d3b5c747bde25d3b3c3b03ca281fc3045 libmbedx509-0_2.16.0-1_amd64.deb" >> SHA256SUMS
[joel@ubuntu]$ echo "786c7e7805a51f3eccd449dd44936f735afa97fc5f3702411090d8b40f0e9eda libmbedtls12_2.16.0-1_amd64.deb" >> SHA256SUMS
[joel@ubuntu]$ sha256sum -c SHA256SUMS || { echo "Checksum failed. Aborting."; exit 1; }
[joel@ubuntu]$ sudo apt-get install -y --allow-downgrades ./*.deb

I have the checksum verification in there because these pages have SSL problems and the advertised URL is HTTP not HTTPS (by design I think).

Debian 10

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-dev

Privoxy fetched by TorSharp fails to start? Try ExecutablePathOverride.

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/privoxy

After 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.

CentOS 7

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 -y

Debian 9

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

torsharp's People

Contributors

abenimatteo avatar ambulatur avatar b1tc0re avatar bibali1980 avatar dermistkaefer avatar i-lamrani avatar joelverhagen avatar jskimming avatar kzu avatar mahmutyaman avatar noneatme avatar stdray avatar username77 avatar zimm-lostpolygon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

torsharp's Issues

NullReferenceException disposing TorControlClient

The following snippet throws NullReferenceException on TorControlClient.Dispose.
The internal TcpClient is initialized and assigned but the reader and writer are not.

namespace Repro
{
    using Knapcode.TorSharp.Tools.Tor;

    class Program
    {
        static void Main()
        {
            using (var client = new TorControlClient())
            {
                try
                {
                    // Invalid ip
                    client.ConnectAsync("300.0.0.0", 12345).Wait();
                }
                catch
                {
                }
            }
        }
    }
}

Failed to download Tor?

Our system started receiving this error. It seems the application cannot download the Tor successfully.

System.NullReferenceException: Object reference not set to an instance of an object.
at Knapcode.TorSharp.TorSharpToolFetcher.d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Knapcode.TorSharp.TorSharpToolFetcher.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

System.Net.Sockets.SocketException when running on an Azure Webjob

Hi,

Have you ever tried to make a http request in a webjob? I'm getting this exception:

"An attempt was made to access a socket in a way forbidden by its access permissions 127.0.0.1:1337"

I'm not sure whether I'm doing something wrong or it is just not allowed.

This is the class I'm using:

using Knapcode.TorSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace MyWebjob
{
    public class TorWebClient : IWebClient, IDisposable
    {

        private readonly TorSharpSettings _Settings;
        private readonly TorSharpProxy _proxy;
        public TorWebClient()
        {
            // configure
            _Settings = new TorSharpSettings
            {
                ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort = 1337,
                TorSocksPort = 1338,
                TorControlPort = 1339,
                TorControlPassword = "foobar",
                ToolRunnerType = ToolRunnerType.Simple
            };

            new TorSharpToolFetcher(_Settings, new HttpClient()).FetchAsync().Wait();

            _proxy = new TorSharpProxy(_Settings);

            _proxy.ConfigureAndStartAsync().Wait();

        }

        public async Task<string> GetStringResponseAsync(string url)
        {
            string html = null;
            using (HttpClientHandler handler = GetHandler())
            {
                using (var httpClient = new HttpClient(handler))
                {
                    html = await httpClient.GetStringAsync(url);
                }
            }
            return html;
        }
        public virtual async Task<ResponseMessage> GetResponseAsync(string url)
        {
            var response = new ResponseMessage();
            using (var handler = GetHandler())
            {
                using (var httpClient = new HttpClient(handler))
                {
                    response.HttpResponseMessage = await httpClient.GetAsync(url);
                }

            }

            return response;
        }

        private HttpClientHandler GetHandler()
        {
            return new HttpClientHandler
            {
                Proxy = new WebProxy(new Uri("http://localhost:" + _Settings.PrivoxyPort))
            };
        }


        public void Dispose()
        {
            _proxy.Stop();
        }

    }
}

Any ideas?

Thanks.

Issue

Knapcode.TorSharp.TorSharpException: 'Failed to extract tool 'Tor'. Verify that the .zip at path 'C:\Users\carol\AppData\Local\Temp\TorZipped\tor-win32-0.4.2.7.zip' is valid.'
Code:

            // configure
            var settings = new TorSharpSettings {
                ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                PrivoxyPort = 1337,
                TorSocksPort = 1338,
                TorControlPort = 1339,
                TorControlPassword = "Tor"
            };

            // 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.PrivoxyPort))
            };
            var httpClient = new HttpClient(handler);
            await proxy.ConfigureAndStartAsync();
            MessageBox.Show(await httpClient.GetStringAsync("http://api.ipify.org"));
            await proxy.GetNewIdentityAsync();
            MessageBox.Show(await httpClient.GetStringAsync("http://api.ipify.org"));
            proxy.Stop();

System.IO.InvalidDataException: End of Central Directory record could not be found.

On Centos7 the function ConfigureAndStartAsync() throws:
System.IO.InvalidDataException: End of Central Directory record could not be found.
at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
at System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding)
at System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding, Boolean overwrite)
at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName)
at Knapcode.TorSharp.TorSharpProxy.ExtractTool(Tool tool, Boolean reloadTool)
at Knapcode.TorSharp.TorSharpProxy.Extract(ToolSettings toolSettings)
at Knapcode.TorSharp.TorSharpProxy.InitializeAsync()
at Knapcode.TorSharp.TorSharpProxy.ConfigureAndStartAsync()

Any idea how to fix it?

InvalidDataException on ToolFetcher

처리되지 않은 예외: System.IO.InvalidDataException: 중앙 디렉터리의 끝 레코드를 찾을 수 없습니다.
   위치: System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
   위치: System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen)
   위치: System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode, Boolean leaveOpen, Encoding entryNameEncoding)
   위치: System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
   위치: System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding)
   위치: System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName)
   위치: Knapcode.TorSharp.TorSharpProxy.ExtractTool(Tool tool, Boolean reloadTool)
   위치: Knapcode.TorSharp.TorSharpProxy.Extract(ToolSettings toolSettings)
   위치: Knapcode.TorSharp.TorSharpProxy.<InitializeAsync>d__10.MoveNext()

Is there any way to setup manually?
Because I already saw this issue and it seems that someone have to make a fix everytime link is broken.

Bootstrapped 0% (starting) Error

Hi, I am using the example code of TorSharp initializing, but when I launch the program, Tor proxy is not starting. I mean It stuck at

Oct 31 18:24:17.000 [notice] Bootstrapped 0% (starting): Starting
Oct 31 18:24:18.000 [notice] Starting with guard context "default"

And nothing. The bootstrapped should be started but it's not starting. Help me, please. Thanks

Documentation? How to specify exit node country?

It seems that there are no documentation... Is the code below how to set the exit node country?

        // configure
        var settings = new TorSharpSettings
        {
	...
        };

        settings.TorExitNodes = "{two letter country code}";

having issues...

Hi guys, I feel like a total dolt. I installed the torsharp nuget into my project in c#, I have installed privoxy, and tor expert bundle, create a c# console app in framework 4.5.2 and have just not been able to get the await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync(); to give me anything but an "object reference not set to instance" issue. Maybe I should just be able to figure this out but there is zero doc on line unfortunately on whether i need to do any further config straight out of the box. I ran the sample and it is just not working for me. I was able to get the ZippedToolsDirectory to populate the privoxy zip, but the ExtractedToolsDirectory I am unclear about what I point that to. I am OK not reloading the latest privoxy and tor each time it loads... Something must be hosed in my environment to use the sample. HELP.,... first time in 15 years I have asked a question on line like this. Thanks for a kickstart. I am an idiot...

Can't fetch files nor configure manually

Trying to use the sample code results in the following error:
Knapcode.TorSharp.TorSharpException: 'Failed to extract tool 'Tor'. Verify that the .zip at path 'C:\Users\Alan\AppData\Local\Temp\TorZipped\tor-win32-0.4.1.5.zip' is valid.'

Looking at the file shows it is only 124k in size, and the one for Privoxy is 0k.

I then tried manually downloading and setting it up, it gets past the problem with Tor, but can't detect that Privoxy is there and an error is thrown on the line:
await proxy.ConfigureAndStartAsync();

Custom Log output

Is there already a way to redirect the console output for custom logging?

Request: Allow to check if new versions are available before update

Currently TorSharpToolFetcher.FetchAsync checks for updates and if possible will execute the update.

The update require some time and in my case it will be better to let the user choose to run the outdated version of privoxy\tor (if already installed) or wait for the update.

is possible to monitor traffic data (upload, download)?

I see some stats every 6h about bandwidth, status (Heartbeat: Tor's uptime is 6:00 hours, with 3 circuits open. I've sent 4.27 MB and received 9.57 MB.) but i'm not able to find who is located the code.

Is possible to call it when i need it?

Why not work in WPF?

In wpf I have problem in string:
var updates = await fetcher.CheckForUpdatesAsync();

this string can't finish execute and not throw mistake

ConfigureAndStartAsync throws in Knapcode.TorSharp.TorSharpProxy.ExtractTool

The tools cannot be extracted - seems like something is wrong with the zip archives.

Version: 1.8.2 (NuGet)

Exception:
Object reference not set to an instance of an object.

Class Stack:
at Knapcode.TorSharp.TorSharpProxy.ExtractTool(Tool tool, Boolean reloadTool)
at Knapcode.TorSharp.TorSharpProxy.Extract(ToolSettings toolSettings)
at Knapcode.TorSharp.TorSharpProxy.d__10.MoveNext()

Cannot run Privoxy on Unknown OS and X64 architecture. OS description: Darwin 17.7.0 Darwin Kernel Version 17.7.0

Getting the following error while trying to run the code from MaxOs:
Knapcode.TorSharp.TorSharpException: Cannot run Privoxy on Unknown OS and X64 architecture. OS description: Darwin 17.7.0 Darwin Kernel Version 17.7.0: Sun Jun 2 20:31:42 PDT 2019; root:xnu-4570.71.46~1/RELEASE_X86_64.
at Knapcode.TorSharp.TorSharpSettings.RejectRuntime(String action)
at Knapcode.TorSharp.ToolUtility.GetPrivoxyToolSettings(TorSharpSettings settings)
at Knapcode.TorSharp.TorSharpToolFetcher.CheckForUpdatesAsync(Boolean allowExistingTools)
at Knapcode.TorSharp.TorSharpToolFetcher.FetchAsync()
at WebCrawler.MyTorProxy.ConfigureAndStartAsync() in /Users/roikrakovski/crawler/crawler/MyTorProxy.cs:line 82

403 question

I notice that some sites seem to block Tor and always return a 403. Yet Tor Browser is able to access these pages just fine.

Is there a reason for this? Really appreciate any insight.

Problem during the upgrade process

In General, observed such a thing: when constantly or every 1-2m you update process, through some time(always it different), simply plainly work does freeze on renewal, i.e. suppose program has updated proxy and work has stalled, I learned button, by clicking on it should be updated proxy somewhere in during 10 minutes after stop, it perhaps again starts work but most often account for launch process with the outset. It seems to me there is no certain exception or check whether the method works or not.

Improvement: separate Configure from Start

Currently, both responsibilities are mixed in the single ConfigureAndStartAsync method.

I found it useful to be able to fully extract+configure the tools separately from actually starting them. For example, to further explore the torrc and make additional tweaks before actually starting it.

The proposal is to introduce two new methods: ConfigureAsync and StartAsync and have ConfigureAndStartAsync essentially call one after the other, so the behavior is exactly like it is now:

    public static class TorSharpProxyExtensions
    {
        public static async Task ConfigureAndStartAsync(this ITorSharpProxy proxy)
        {
            await proxy.ConfigureAsync().ConfigureAwait(false);
            await proxy.StartAsync().ConfigureAwait(false);
        }
    }

    public interface ITorSharpProxy : IDisposable
    {
        Task ConfigureAsync();
        Task StartAsync();
        Task GetNewIdentityAsync();
        void Stop();
    }

This would be source-compatible with the previous versions, although not binary compat (should not be a problem since to update the nuget dependency, you'd need to also ship a new version of your package). The only breaking scenario (for moving ConfigureAndStartAsync would be if a third project/consumer references a nuget pointing to the older TorSharp, and he explicitly references a newer version of TorSharp in the top-level project. The nuget that depended on the older version of TorSharp would fail at run-time because the method wouldn't be found.

I consider that to be an extremely rare case, but would still recommend bumping the major version anyway, so that in that case, users can know there's an API breaking change. Note that the addition of new interface members already justifies bumping major, IMO.

(I have this working on my fork).

Tor won't launch with a space in the Username

My Windows Account Name is "Prefix Suffix". I used the example code provded in the readme.md file.
If the downloaded temp folder contains a space, Tor won't launch with the following Exception:

Apr 19 11:42:14.890 [warn] Command-line option 'Suffix\AppData\Local\Temp\TorEx
tracted\tor-win32-0.2.7.6\Data\Tor\torrc' with no value. Failing.
Apr 19 11:42:14.890 [err] Reading config failed--see warnings above.

The suffix gets interpreted as a new command line option somehow.

Cannot assign requested address (when using Docker)

I'm using this library and all works well when I run my application locally. I've implemented TorSharp in a NetworkHelper class:

using Knapcode.TorSharp;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace SWP.Helpers
{
    public class NetworkHelper
    {
        private int _maxRequests = 100;

        private int _failedAttempts = 10;

        private HttpClient _httpClient = null;

        private TorSharpProxy _proxy = null;

        public async Task<string> GetHtmlAsync(Uri url)
        {
            try
            {
                if (_httpClient == null)
                {
                    var settings = new TorSharpSettings
                    {
                        ZippedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorZipped"),
                        ExtractedToolsDirectory = Path.Combine(Path.GetTempPath(), "TorExtracted"),
                        PrivoxySettings =
                        {
                            Port = 1337,

                        },
                        TorSettings =
                        {
                            ControlPort = 1338,
                            ControlPassword = "foobar"
                        }
                    };

                    // download tools
                    await new TorSharpToolFetcher(settings, new HttpClient()).FetchAsync();

                    // execute
                    _proxy = new TorSharpProxy(settings);
                    var handler = new HttpClientHandler
                    {
                        Proxy = new WebProxy(new Uri("http://localhost:" + settings.PrivoxySettings.Port))
                    };

                    var httpClient = new HttpClient(handler);
                    _httpClient = new HttpClient(handler);

                    await _proxy.ConfigureAndStartAsync();
                }

                string response = await _httpClient.GetStringAsync(url);

                if (_maxRequests == 0)
                {
                    await _proxy.GetNewIdentityAsync();
                    _maxRequests = 100;
                    Console.WriteLine("Ottenimento nuova identità...");
                    var newIP = await _httpClient.GetStringAsync("http://api.ipify.org");
                    Console.WriteLine("Nuovo IP: " + newIP);
                }

                _maxRequests--;

                return response;
            }
            catch (Exception)
            {
                if (_failedAttempts != 0)
                {
                    Console.WriteLine("Failed request, retry ...");
                    _failedAttempts--;
                    await Task.Delay(15000);
                }
                else
                {
                    //_proxy.Stop();
                    throw;
                }

                return await GetHtmlAsync(url);
            }
        }
    }
}

So when I have to grab the html from a web page I simply do:

string html = await _parser.NetworkHelper.GetHtmlAsync(coachLink);

Now the prroblem is that when I run the application locally, so in Visual Studio, everything works well, but when I run it inside a Docker container I get this error:

[16-01-2021 11:55:25]
Jan 16 11:57:33.001 [notice] Tor 0.4.4.6 (git-2a8b789ea6f308d0) running on Linux with Libevent 2.1.11-stable, OpenSSL 1.1.1i, Zlib 1.2.8, Liblzma N/A, and Libzstd N/A.
Jan 16 11:57:33.031 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Jan 16 11:57:33.031 [notice] Read configuration file "/tmp/TorExtracted/tor-linux64-10.0.8/Data/Tor/torrc".
Jan 16 11:57:33.034 [notice] Opening Socks listener on 127.0.0.1:19050
Jan 16 11:57:33.063 [notice] Opened Socks listener on 127.0.0.1:19050
Jan 16 11:57:33.159 [notice] Opening Control listener on 127.0.0.1:1338
Jan 16 11:57:33.175 [notice] Opened Control listener on 127.0.0.1:1338
Jan 16 11:57:33.175 [warn] Fixing permissions on directory /tmp/TorExtracted/tor-linux64-10.0.8/Data/Tor
Jan 16 11:57:33.000 [warn] You are running Tor as root. You don't need to, and you probably shouldn't.
/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


Jan 16 11:57:33.000 [notice] Bootstrapped 0% (starting): Starting
Jan 16 11:57:33.000 [notice] Starting with guard context "default"
Jan 16 11:57:34.000 [notice] Bootstrapped 5% (conn): Connecting to a relay
Jan 16 11:57:34.000 [notice] Bootstrapped 10% (conn_done): Connected to a relay
Jan 16 11:57:34.000 [notice] Bootstrapped 14% (handshake): Handshaking with a relay
Jan 16 11:57:34.000 [notice] Bootstrapped 15% (handshake_done): Handshake with a relay done
Jan 16 11:57:34.000 [notice] Bootstrapped 20% (onehop_create): Establishing an encrypted directory connection
Jan 16 11:57:34.000 [notice] Bootstrapped 25% (requesting_status): Asking for networkstatus consensus
Jan 16 11:57:35.000 [notice] Bootstrapped 30% (loading_status): Loading networkstatus consensus
Jan 16 11:57:36.000 [notice] I learned some more directory information, but not enough to build a circuit: We have no usable consensus.
Jan 16 11:57:36.000 [notice] Bootstrapped 40% (loading_keys): Loading authority key certs
Jan 16 11:57:37.000 [notice] The current consensus has no exit nodes. Tor can only build internal paths, such as paths to onion services.
Jan 16 11:57:37.000 [notice] Bootstrapped 45% (requesting_descriptors): Asking for relay descriptors
Jan 16 11:57:37.000 [notice] I learned some more directory information, but not enough to build a circuit: We need more microdescriptors: we have 0/6896, and can only build 0% of likely paths. (We have 0% of guards bw, 0% of midpoint bw, and 0% of end bw (no exits in consensus, using mid) = 0% of path bw.)
Jan 16 11:57:37.000 [notice] I learned some more directory information, but not enough to build a circuit: We need more microdescriptors: we have 0/6896, and can only build 0% of likely paths. (We have 0% of guards bw, 0% of midpoint bw, and 0% of end bw (no exits in consensus, using mid) = 0% of path bw.)
Jan 16 11:57:38.000 [notice] Bootstrapped 50% (loading_descriptors): Loading relay descriptors
Failed request, retry ...
Jan 16 11:57:39.000 [notice] The current consensus contains exit nodes. Tor can build exit and internal paths.
Jan 16 11:57:42.000 [notice] Bootstrapped 57% (loading_descriptors): Loading relay descriptors
Jan 16 11:57:42.000 [notice] Bootstrapped 64% (loading_descriptors): Loading relay descriptors
Jan 16 11:57:43.000 [notice] Bootstrapped 75% (enough_dirinfo): Loaded enough directory info to build circuits
Jan 16 11:57:43.000 [notice] Bootstrapped 80% (ap_conn): Connecting to a relay to build circuits
Jan 16 11:57:43.000 [notice] Bootstrapped 85% (ap_conn_done): Connected to a relay to build circuits
Jan 16 11:57:44.000 [notice] Bootstrapped 89% (ap_handshake): Finishing handshake with a relay to build circuits
Jan 16 11:57:44.000 [notice] Bootstrapped 90% (ap_handshake_done): Handshake finished with a relay to build circuits
Jan 16 11:57:44.000 [notice] Bootstrapped 95% (circuit_create): Establishing a Tor circuit
Jan 16 11:57:45.000 [notice] Bootstrapped 100% (done): Done
Failed request, retry ...
System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.EstablishProxyTunnel(CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 72
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 101
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Helpers.NetworkHelper.GetHtmlAsync(Uri url) in /src/SWP/Helpers/NetworkHelper.cs:line 104
at SWP.Controllers.CountryController.GetDomesticCountries() in /src/SWP/Controllers/CountryController.cs:line 28
at SoccerWay.Business.CountryManager.GetCountries(Nullable`1 international) in /src/SoccerWay/Business/Manager/Utility/Country.Utility.cs:line 59
at SoccerWay.Scraper.StartAsync(String instance) in /src/SoccerWay/Scraper.cs:line 55

there is the following line that caught my attention:

/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

This is the Dockerfile that I use to compile the Docker image:

FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build
WORKDIR /src

# Copy csproj and restore as distinct layers
COPY /SoccerWay/*.csproj SoccerWay/
COPY /SWP/*.csproj SWP/
RUN dotnet restore SWP/*.csproj
RUN dotnet restore SoccerWay/*.csproj 

# Copy and build app and libraries
COPY SoccerWay/ SoccerWay/
COPY SWP/ SWP/
WORKDIR /src/SoccerWay
RUN dotnet build -c release 

FROM build AS publish
RUN dotnet publish -c release -o /app

# Final stage/image
FROM mcr.microsoft.com/dotnet/core/runtime:2.1
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SoccerWay.dll"]

How can I fix this?

SecurityProtocolType.Ssl3 not supported in last version of ServicePointManager

Hello,

The new version of the ServicePointManager do not support the security protocol SecurityProtocolType.Ssl3.
https://github.com/dotnet/corefx/blob/a9e01da6f1b3a0dfbc36d17823a2264e2ec47050/src/System.Net.ServicePoint/src/System/Net/ServicePointManager.cs

It result an error during TorSharpToolFetcher initialization :

System.NotSupportedException
The requested security protocol is not supported.
at System.Net.ServicePointManager.ValidateSecurityProtocol(SecurityProtocolType value)
at System.Net.ServicePointManager.set_SecurityProtocol(SecurityProtocolType value)
at Knapcode.TorSharp.TorSharpToolFetcher.FetchAsync() 

See ServicePointManager security protocol validation :

private static void ValidateSecurityProtocol(SecurityProtocolType value)
{
        SecurityProtocolType allowed = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        if ((value & ~allowed) != 0)
        {
              throw new NotSupportedException(SR.net_securityprotocolnotsupported);
        }
}

Thanks.

suspicion error

hi,

please note the mistake in the following function

namespace Knapcode.TorSharp.Tools.Tor
{
    public class TorControlClient
    {
        ...
        public void Close()
        {
            if (_tcpClient != null)
            {
                _tcpClient.Close();
                _reader.Dispose();
                _reader.Dispose();
            }
        }

probably replace last _reader.Dispose by _writer.Dispose

cheers
syus

Could not find a part of the path.

hi guy,
thank you for provided dll,

this is code which i'm using as to demonestrated

        try
        {
            var settings = new TorSharpSettings
            {
                ZippedToolsDirectory = Path.Combine(Environment.CurrentDirectory, "TorZipped"),
                ExtractedToolsDirectory = Path.Combine(Environment.CurrentDirectory, "TorExtracted"),
                PrivoxyPort = 1337,
                TorSocksPort = 1338,
                TorControlPort = 1339,
                TorControlPassword = "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.PrivoxyPort))
            };
            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();
        }
        catch (Exception ex)
        {

        }

but i get Could not find a part of the path, which part of path i dont know?

manually added TorZipped folder, copied files
TorZipped/tor-win32-0.3.5.8.zip=>Tor/tor.exe
TorZipped/privoxy-win32-3.0.28.zip=>privoxy.exe(and other files beside it)

whats wrong with?
and is this library suitable to run multiple instances of program at same time?

How to open multiple ports in socks?

Hello , is there any option to open multiple socks from a single controlport (like 1339)?

var settings = new TorSharpSettings {

SocksPort = 1338,1340,1341...etc

};

Check crc of downloaded zip

Sometime happens that the zip file is corrupted and the program crash.

Is it possible to add the signature check of the downloaded files? and in case the crc test is not passed try to download again the file?

Thanks

Problem while Extracting the Tool Zip

Exception thrown: 'Knapcode.TorSharp.TorSharpException' in mscorlib.dll
An unhandled exception of type 'Knapcode.TorSharp.TorSharpException' occurred in mscorlib.dll
Failed to extract tool 'Tor'. Verify that the .zip at path 'C:\Users\xxxx\AppData\Local\Temp\TorZipped\tor-win32-0.4.0.5.zip' is valid.

Windows 7 does not support virtual desktop

Attempting to use virtual desktop (which is the default option on Windows) throws an exception on Windows 7. This is easily resolved by checking System.Environment.OSVersion.Version to be less than 6.2 (details) and reverting to standard mode. You may consider implementing this check in the library itself.

Error 426

While making HttpRequest which worked month ago, now I get
The remote server returned an error: (426) Upgrade Required.

If I disable proxy from handler => no TorSharp request works fine.

Any possible issue during TLS 1.1 ending support?

Request require HTTP version 1.1. 2.0 not supported.

Exception on TorSharpToolFetcher

Currently
await new TorSharpToolFetcher(torSettings, new HttpClient()).FetchAsync();
throws an exception for me:

The element named 'html' and the namespace '' is not a valid feed format.

Is the Download URL maybe no longer Update2Date?

Unable to assign the process to the job object

Hello, I encountered one exception when I tried to start the Tor. It seems I missed some files?

My OS: Windows 7 x64

Somebody have any idea about this issue?

Thanks in advance!

Knapcode.TorSharp.TorSharpException: Unable to assign the process to the job object. Error: The system cannot find the file specified.
at Knapcode.TorSharp.Tools.VirtualDesktopToolRunner.AssociateWithJob(PROCESS_INFORMATION processInformation, Boolean throwOnError)
at Knapcode.TorSharp.Tools.VirtualDesktopToolRunner.StartAsync(Tool tool)
at Knapcode.TorSharp.TorSharpProxy.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Knapcode.TorSharp.TorSharpProxy.d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Knapcode.TorSharp.TorSharpProxy.d__9.MoveNext()

Help?

Is there a list of functions and classes for TorSharp and their uses?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.