QuickStart TOR in .NET Core

Introduction

To mention there is no TOR library that supports .NET Core. Since a programmer needed, he put them together. It couldn’t do much magic, but it will perform the job 99% of the times, let us explain how:

  • You want to make anonymous requests.
  • You want to make requests to onion APIs.

They are the most common use cases those made someone to use TOR inside their application. This blogtopic aims to serve these basic needs and nothing more. In other words: if you simply want to “change your IP”, this article is for you.

Understand the problem

Some people tries to launch the Tor process in their local machine, giving the process a port number for the SOCKS port, and then configure their browser to point to the Tor process. This won’t work.

Tor is a SOCKS proxy that means it is able to connect to IP addresses and ports, but it fails to understand HTTP requests.

Configure TOR

  1. Download TOR Expert Bundle.
  2. You can find a sample TOR config file, let it be torrc.
  3. Put torrc in the proper default location (depending on your OS), or just run the tor process later by feeding it with the right configuration.
  4. Edit the sample config file:
    • Optionally change and edit the SocksPort, if you don’t change, it will go to 9050 port by default.
    • Change the default ControlPort 9051, optionally edit.
    • Change and modify the password hash:
      • To run a tes, for the ControlPortPassword = “ILoveBitcoin21″ the hash must be: HashedControlPassword 16:0978DBAF70EEB5C46063F3F6FD8CBC7A86DF70D2206916C1E2AE29EAF6

      • You must use different and more complicated one. Keep in mind that you can start tor like this: tor –hash-password password where password is the password you’ve chosen. It will give you the hashed password.
  5. Start tor, it will listen to the ports you set in the config file.

SocksPort vs ControlPort

The control port controls the Tor, mainly through other software like Arm. The Socks port is the port running as a SOCKS5 proxy. This is the one you wanted to use. Remember that Tor is not an HTTP proxy, so your script will need to be configured to use a SOCKS5 proxy.

Create & Test

You can skip this section and get the NuGet package and use it, but you might save time if you make sure everything runs fine, by running these tests. Now get .NET Core, if you haven’t already done so and fire up a terminal:

  1. git clone https://github.com/nopara73/DotNetTor
  2. cd DotNetTor/
  3. dotnet restore
  4. cd src/DotNetTor.Tests/
  5. dotnet test

How to use it

Consider the following simple console app, where

  1. You write out your real IP
  2. Your TOR IP
  3. Change your TOR circuit
  4. Finally write out the changed TOR IP.

var requestUri = “http://icanhazip.com/”; // Gives back your IP

// 1. Get your (real) IP, just like you normally do

using (var httpClient = new HttpClient())

{

var content = httpClient.GetAsync(requestUri).Result.Content.ReadAsStringAsync().Result;

Console.WriteLine($“Your IP: \t\t{content}”);

}

// 2. Get your IP through TOR

using (var socksPortClient = new SocksPort.Client())

{

var handler = socksPortClient.GetHandlerFromDomain(“icanhazip.com”);

using (var httpClient = new HttpClient(handler))

{

var content = httpClient.GetAsync(requestUri).Result.Content.ReadAsStringAsync().Result;

Console.WriteLine($“Your TOR IP: \t\t{ content}”);

}

// 3. Change TOR IP

var controlPortClient = new ControlPort.Client(password: “ILoveBitcoin21″);

controlPortClient.ChangeCircuit();

// 4. Get changed TOR IP

handler = socksPortClient.GetHandlerFromRequestUri(requestUri);

using (var httpClient = new HttpClient(handler))

{

var content = httpClient.GetAsync(requestUri).Result.Content.ReadAsStringAsync().Result;

Console.WriteLine($“Your other TOR IP: \t{content}”);

}

}

Summary

While this article did not cover a full-featured TOR library, but a minimal one, without any dependency, you must acknowledge that this article covered the most popular needs in a platform (.NET Core) where nothing else is available at the time of writing.

We conclude the discussion here. Let us know your opinion in the comments sections below. And feel free to refer Microsoft’s site to gather more information.

If you want to improve your skill in ASP.Net and excel yourself in ASP.NET training program; our institute, CRB Tech Solutions would be of great help and for you. Come and join us with our well planned and structured program for .Net.

Stay connected to CRB Tech for more technical optimization and other updates and information.

Don't be shellfish...Buffer this pageEmail this to someoneDigg thisShare on FacebookShare on Google+Share on LinkedInPrint this pageShare on RedditPin on PinterestShare on StumbleUponTweet about this on TwitterShare on Tumblr

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>