Search code examples
c#tor

Download file using Tor in C#


I want to download a file using Tor. Most solutions I found require that additional software (e.g. privoxy) is installed and running, but I don't want to have an additional software running all the time even when I don't use my program.

So I tried the Tor.NET library, but I can't get it using Tor. This example shouldn't return my IP-address, but it does:

ClientCreateParams createParams = new ClientCreateParams(@"D:\tor.exe", 9051);
Client client = Client.Create(createParams);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.icanhazip.com/");
request.Proxy = client.Proxy.WebProxy;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    var reader = new StreamReader(response.GetResponseStream());
    Console.WriteLine(reader.ReadToEnd());
}

Console.WriteLine("Press enter to exit...");
Console.ReadLine();

There are already multiple comments about this but unfortunally the author of the library isn't active anymore.

Maybe you know what I'm doing wrong (is more configuration neccessary?) or have an idea for an alternative way to download a file using tor.


Solution

  • You follow the Tor project manual, command line HTTPTunnelPort, that you find here: first you must start an HTTP tunnel with

    Tor.exe --HTTPTunnelPort 4711
    

    It supplies you with a HTTP tunnel at 127.0.0.1:4711 (see also here). Now you can connect to this proxy:

    WebProxy oWebProxy = new WebProxy (IPAddress.Loopback.ToString (), 4711);
    WebClient oWebClient = new WebClient ();
    oWebClient.Proxy = oWebProxy;
    oWebClient.DownloadFile ("https://myUri", "myFilename");
    

    By now, Microsoft recommends the use of the HttpClient for new developments. Here is the code:

    // we must configure the HttpClient
    HttpClientHandler oHttpClientHandler = new HttpClientHandler ();
    oHttpClientHandler.UseProxy = true;
    oHttpClientHandler.Proxy = 
      new WebProxy (IPAddress.Loopback.ToString (), 4711);
    // we start an HttpClient with the handler; Microsoft recommends to start
    // one HttpClient per application
    HttpClient oHttpClient = new HttpClient (oHttpClientHandler);
    // we request the resource by the GET method
    HttpRequestMessage oHttpRequestMessage = 
      new HttpRequestMessage (HttpMethod.Get, "https://myUri");
    // we make the request and we do only wait for the headers and not for
    // content
    Task<HttpResponseMessage> 
      oTaskSendAsync = 
        oHttpClient.SendAsync 
          (oHttpRequestMessage, HttpCompletionOption.ResponseHeadersRead);
    // we wait for the arrival of the headers
    oTaskSendAsync.Wait ();
    // the function throws an exception if something went wrong
    oTaskSendAsync.Result.EnsureSuccessStatusCode ();
    // we can act on the returned headers
    HttpResponseHeaders oResponseHeaders = oTaskSendAsync.Result.Headers;
    HttpContentHeaders  oContentHeaders  = oTaskSendAsync.Result.Content.Headers;
    // we fetch the content stream
    Task<Stream> oTaskResponseStream = 
        oTaskSendAsync.Result.Content.ReadAsStreamAsync ();
    // we open a file for the download data
    FileStream oFileStream = File.OpenWrite ("myFilename");
    // we delegate the copying of the content to the file
    Task oTaskCopyTo = oTaskResponseStream.Result.CopyToAsync (oFileStream);
    // and wait for its completion
    oTaskCopyTo.Wait ();
    // now we can close the file
    oFileStream.Close ();
    

    Please heed the following in using Tor.exe:

    • If the port is already in use Tor.exe will not be able to supply a proxy. It does not even necessarily inform you about this failure.
    • Make sure that nobody spoofs your program Tor.exe so that it is Tor that supplies you with this proxy. Hence, Tor.exe should be at a secure place in your file system.
    • Inform yourself about other precautions with respect to using Tor.

    At least, you might want to check that your proxy has a different IP address from your local internet connection.