Click here to Skip to main content
13,764,506 members
Click here to Skip to main content
Add your own
alternative version

Stats

16.2K views
16 bookmarked
Posted 14 Dec 2016
Licenced MIT

QuickStart TOR in .NET Core

, 18 Jan 2017
Rate this:
Please Sign up or sign in to vote.
How to use TOR in .NET Core

Introduction

At the time of writing there is no TOR library that supports .NET Core. I needed one for one of my project I am working on, called NTumbleBit, so I had to create it myself. It is not a full featured library, but rather a lean, robust one. Only implements essential TOR features.

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

These are the most common use cases those lead someone to use TOR inside their application. This library aims to serve these basic needs and nothing more. 

Understanding the Problem

Why we even need to use a third party library for TOR.

Quote:

Some people attempt to launch the Tor process on 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 will not work.

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

(Source)

Configure TOR

  1. Download TOR Expert Bundle: https://www.torproject.org/download/download
  2. Download the torrc config file sample: https://github.com/nopara73/DotNetTor/blob/master/torrc
  3. Place torrc in the proper default location (depending on your OS) and edit it:
    • Optionally uncomment and edit the SocksPort, if you don't uncomment it will default to 9050 port anyway
    • The default ControlPort in the sample is 9051, optionally edit it.
    • Modify the password hash
      • To run my tests, for the ControlPortPassword = "ILoveBitcoin21" the hash should be: HashedControlPassword 16:0978DBAF70EEB5C46063F3F6FD8CBC7A86DF70D2206916C1E2AE29EAF6
      • For your application you should use different one, a more complicated one. Then start tor like this: tor --hash-password password where password is the password you've chosen. It will give you the HashedControlPassword.
  4. Start tor, it will listen to the ports you set in the config file.

SocksPort vs ControlPort

Quote:

The control port is used for controlling Tor, usually via other software like Arm. The Socks port is the port running as a SOCKS5 proxy. This is the one you want to use. Please note that Tor is not an HTTP proxy, so your script will need to be configured to use a SOCKS5 proxy.

(Source)

Build & Test

If you are in a hurry, you can skip this section and just get the NuGet package and use it. 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. I 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/";

// 1. Get real IP
using (var httpClient = new HttpClient())
{
    var message = httpClient.GetAsync(requestUri).Result;
    var content = message.Content.ReadAsStringAsync().Result;
    Console.WriteLine($"Your real IP: \t\t{content}");
}

// 2. Get TOR IP
using (var httpClient = new HttpClient(new SocksPortHandler("127.0.0.1", socksPort: 9050)))
{
    var message = httpClient.GetAsync(requestUri).Result;
    var content = message.Content.ReadAsStringAsync().Result;
    Console.WriteLine($"Your TOR IP: \t\t{content}");

    // 3. Change TOR IP
    var controlPortClient = new ControlPort.Client("127.0.0.1", controlPort: 9051, password: "ILoveBitcoin21");
    controlPortClient.ChangeCircuitAsync().Wait();

    // 4. Get changed TOR IP
    message = httpClient.GetAsync(requestUri).Result;
    content = message.Content.ReadAsStringAsync().Result;
    Console.WriteLine($"Your other TOR IP: \t{content}");
}

Conclusion

While I did not provide a full-featured TOR library, but rather a minimal one, without any external dependency, I know I covered the most popular needs in a platform (.NET Core) where there is nothing else available at the time of writing.

Acknowledgements

Originally the SocksPort part of this project was a leaned down, modified and .NET Core ported version of the SocketToMe project.
Originally the ControlPort part of this project was a leaned down, modified and .NET Core ported version of the Tor.NET project.
At this point of the development you can still find parts of the former project in the codebase, however the latter has been completely replaced.

Updates

  • 2017.01.24
    • Version 2.1 is out
    • Limitations are removed
    • You can request different addresses within the same `HttpClient`
  • 2017.01.19
    • Version 2.0 is out
    • Simplified usage
    • Not breaking old userspace
    • Async support
    • Bug fixes
    • Performance increase
  • 2016.12.27
    • Fix torrc sample reference link
    • Fix some typo and formatting mistake
    • Add updates section
    • Change licence to MIT

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Ádám Ficsór
CEO of Bitcoin
Hungary Hungary
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionDoes not seem to reference the current torrc sample file Pin
Member 956675526-Dec-16 13:16
memberMember 956675526-Dec-16 13:16 
AnswerRe: Does not seem to reference the current torrc sample file Pin
Ádám Ficsór27-Dec-16 5:13
memberÁdám Ficsór27-Dec-16 5:13 
GeneralWhat is this TOR thing? Pin
Tiago Freitas Leal23-Dec-16 13:34
professionalTiago Freitas Leal23-Dec-16 13:34 
GeneralNeeds More Background Pin
Eric Nielsen15-Dec-16 14:02
memberEric Nielsen15-Dec-16 14:02 
GeneralRe: Needs More Background Pin
Ádám Ficsór16-Dec-16 1:09
memberÁdám Ficsór16-Dec-16 1:09 
GeneralRe: Needs More Background Pin
dandy7218-Dec-16 3:59
memberdandy7218-Dec-16 3:59 
GeneralRe: Needs More Background Pin
Overtkill19-Dec-16 12:25
memberOvertkill19-Dec-16 12:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.181113.4 | Last Updated 19 Jan 2017
Article Copyright 2016 by Ádám Ficsór
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid