Click here to Skip to main content
15,499,046 members
Articles / Web Development / HTML
Posted 28 Jan 2016


20 bookmarked

Chromium (CefSharp) Tor Browser

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
28 Jan 2016CPOL5 min read
An alternative Tor Browser built with C# using CefSharp and Tor.NET.


My favorite browser is Google Chrome. It's fast and easy to use.

When I hit a web site which I can not access because of a web filter, one of my options is to use the original Tor Browser which is a custom version of Firefox and I can simply say that "I don't like it".

When I read the excellent article "Tor.NET - A managed Tor network library" by Chris Copeland, I started to play with the sample application in the project which uses the Internet Explorer component (WebBrowser). I've been using CefSharp Chromium browser component in my projects for some time and I changed the WebBrowser in the project with CefSharp and this is how this application came to life.


This application uses CefSharp as the web browser and the Tor.NET library to connect to the Tor network via an HTTP proxy. Please see the references section for detailed information about these two components.


This is how the application looks when you start it.

Main screen of the application

It is a standard tabbed web browser which mimics Google Chrome.

When the application starts, it connects to the Tor network and starts the HTTP proxy. When Tor is ready to use, the address bar goes pale green.

"Downloads" button opens the downloads page.

"DuckDuckGo" button opens the alternative DuckDuckGo search engine with a ".onion" Tor address.

You can check if you are really connected to the Tor network by clicking the "Check if Tor is active" button.

Interesting Parts of the Code

The application is a WinForms application with only one form. However there are some interesting parts in the code which may be helpful for curious coders.

The following is the CefSharp initialization code.

CefSettings settings = new CefSettings();

//Set proxy for Tor
settings.CefCommandLineArgs.Add("proxy-server", "");

//Load pepper flash player
//settings.CefCommandLineArgs.Add("ppapi-flash-path", appPath + @"PepperFlash\pepflashplayer.dll");

settings.RegisterScheme(new CefCustomScheme
    SchemeName = SchemeHandlerFactory.SchemeName,
    SchemeHandlerFactory = new SchemeHandlerFactory()

We make the CefSharp component to use our Tor proxy by adding the "proxy-server" command line argument.

If you want to use Flash in your browser, you can copy the PepperFlash folder from your Chrome installation to the application directory and uncomment the PepperFlash related line. Chrome is generally located in a folder like the following one:

C:\Program Files (x86)\Google\Chrome\Application\47.0.2526.111

But keep in mind that, if you are really concerned about your privacy, you are not recommended to use Flash when using Tor because Flash plugins might reach the Internet bypassing your proxy.

Tor is initialized by calling the InitializeTor function which is an exact copy from the Tor.NET sample application.

Handlers are used to make CefSharp to "behave". Here is a list of the CefSharp handlers and what they do:

DownloadHandler: Tells the application when a new download starts and about the progress of the ongoing downloads.

KeyboardHandler: When the focus is on the CefSharp browser, it "eats" all the keys pressed by the user. The application uses Ctrl-F4 to close the active browser tab so this handler helps to inform the application that Ctrl-F4 was pressed in the browser.

LifeSpanHandler: CefSharp calls the OnBeforePopup function of this handler before it opens a pop-up window and it tells CefSharp to open it in a new tab and not in a seperate window.

MenuHandler: I added the items from the Chrome Browser which I use most to the browser context menu. This handler does that stuff. An interesting extra feature of this context menu is the "Save as Pdf" option which does not exist in Google Chrome.

SchemeHandler: This handler helps to load the web pages that start with "chrome://".

"Downloads" Page

The "Downloads" Page

Google Chrome uses HTML interface for Settings, Downloads, Extensions and other pages so I did the same for the "Downloads" page.

The "Downloads" page of the application is a "clone" from the Chrome Browser which is an HTML page stored in the "storage" folder. To be able to load this page in the application, I defined a SchemeHandler and named it "chrome:". So when you type "chrome://storage/downloads.htm" in the address bar, CefSharp asks the SchemeHandler how to handle it and the handler returns the requested file from the storage folder.

Here is the JavaScript code from the downloads.htm page.

<script type="text/javascript">

    var $container;
    var $template;
    var timer;

    $(document).ready(function () {
        $container = $("#downloads-display");
        $template = $("#template");
        timer = setInterval(UpdateList, 500);


    function UpdateItem(item) {
        var $item;
        var id = "d" + item.Id;
        $item = $("#" + id);
        //Add item if it does not exist
        if ($item.length == 0) {
            $item = $($template[0].outerHTML);
            $item.attr("id", id);
            $item.find("a.cancel").click(function () {
            $item.find("img.icon").attr("src", "chrome://fileicon/" + item.SuggestedFileName);
            var startTime = getDate(item.StartTime);
            if (item.SuggestedFileName != "") $item.find("").text(item.SuggestedFileName);
            $item.find("a.src-url").attr("href", item.Url).text(item.Url);
        var progress = "";
        if (item.IsInProgress) {
            progress = formatBytes(item.CurrentSpeed) + "/s - " + formatBytes(item.ReceivedBytes, 2);
            if (item.TotalBytes > 0) progress += " of " + formatBytes(item.TotalBytes, 2);
            if (item.PercentComplete > 0) progress += " (" + item.PercentComplete + "%)";
        } else {
            if (item.IsComplete) progress = "Complete";
            else if (item.IsCancelled) progress = "Cancelled";

    function UpdateList() {
        host.getDownloads().then(function (res) {
            var list = JSON.parse(res);
            $.each(list, function (key, item) {


When the page loads, it sets a timer to update the downloads list every 500 miliseconds. 

timer = setInterval(UpdateList, 500);

The update function requests the list of download items from the main application and populates the list using this information.

function UpdateList() {
    host.getDownloads().then(function (res) {
        var list = JSON.parse(res);
        $.each(list, function (key, item) {

With CefSharp, you can call .NET functions of your application from JavaScript.

The following is the line of code which binds a .NET class to the browser object:

            if (url.StartsWith("chrome:"))
                browser.RegisterAsyncJsObject("host", host, true);

For security reasons, only pages loaded from inside the application are given a "host" object.

Functions on your "host" object can return only primitive types like String, Integer, Float etc. so I used a JSON Serializer (see References) to return a JSON string for complex objects.

public string getDownloads()
        string x = JsonSerializer.SerializeToString(myForm.downloads);
        return x;

The calling function in JavaScript uses JSON.parse to access this object:

function UpdateList() {
    host.getDownloads().then(function (res) {
        var list = JSON.parse(res);
        $.each(list, function (key, item) {

Most Challenging Part

Google Chrome displays the icons of the downloaded files in the Downloads page.

Google Chrome downloads page

To able to do the same in my application, I added an extra case in the SchemeHandler:

if (uri.Host == "fileicon")
    Task.Factory.StartNew(() =>
        using (callback)
            stream = GetFileIcon(fileName, IconReader.IconSize.Large);
            mimeType = ResourceHandler.GetMimeType(".png");
    return true;

If you type "chrome://fileicon/" to the address bar, this code gets executed and it asks the operating system to return the icon for the given file type which it returns to the CefSharp browser.

Tab Component

I googled a lot but could not find a "free" tab component which looks like Google Chrome's on the Internet. If you know one, please provide a link in the comments.



  • Version 1.0.0 - Initial version of the application and the article


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Turkey Turkey
I've been working with computers since the glorious times of Commodore 64. My latest beloved programming languages are C# and JavaScript.

Comments and Discussions

GeneralActivex ? Pin
Sido Dz19-Mar-21 14:40
MemberSido Dz19-Mar-21 14:40 
GeneralRe: Activex ? Pin
ahmet_uzun10-May-21 1:23
Memberahmet_uzun10-May-21 1:23 sorunu Pin
Ahmet Sayın15-Jun-17 5:16
MemberAhmet Sayın15-Jun-17 5:16 
QuestionTor error Pin
Member 1258453522-Sep-16 0:48
MemberMember 1258453522-Sep-16 0:48 
QuestionCommand error Pin
Member 1258453526-Aug-16 1:58
MemberMember 1258453526-Aug-16 1:58 
AnswerRe: Command error Pin
ahmet_uzun26-Aug-16 3:15
Memberahmet_uzun26-Aug-16 3:15 
GeneralRe: Command error Pin
Member 1258453526-Aug-16 4:17
MemberMember 1258453526-Aug-16 4:17 
GeneralRe: Command error Pin
ahmet_uzun26-Aug-16 4:50
Memberahmet_uzun26-Aug-16 4:50 
QuestionTor.dll Pin
Jimmy Watson13-Aug-16 3:03
MemberJimmy Watson13-Aug-16 3:03 
AnswerRe: Tor.dll Pin
ahmet_uzun14-Aug-16 1:44
Memberahmet_uzun14-Aug-16 1:44 
AnswerRe: Tor.dll Pin
Member 1258453526-Aug-16 2:18
MemberMember 1258453526-Aug-16 2:18 
GeneralRe: Tor.dll Pin
ahmet_uzun26-Aug-16 3:16
Memberahmet_uzun26-Aug-16 3:16 
GeneralRe: Tor.dll Pin
Member 1258453526-Aug-16 4:14
MemberMember 1258453526-Aug-16 4:14 
GeneralRe: Tor.dll Pin
ahmet_uzun26-Aug-16 4:51
Memberahmet_uzun26-Aug-16 4:51 
GeneralRe: Tor.dll Pin
Member 1258453526-Aug-16 5:55
MemberMember 1258453526-Aug-16 5:55 
Member 125845353-Aug-16 3:10
MemberMember 125845353-Aug-16 3:10 
ahmet_uzun3-Aug-16 22:59
Memberahmet_uzun3-Aug-16 22:59 
Member 125845359-Aug-16 22:35
MemberMember 125845359-Aug-16 22:35 
ahmet_uzun10-Aug-16 1:50
Memberahmet_uzun10-Aug-16 1:50 
QuestionTOR client start button Pin
Member 1258453528-Jul-16 22:27
MemberMember 1258453528-Jul-16 22:27 
AnswerRe: TOR client start button Pin
ahmet_uzun29-Jul-16 8:47
Memberahmet_uzun29-Jul-16 8:47 
QuestionTOR client Pin
Member 1258453520-Jul-16 23:08
MemberMember 1258453520-Jul-16 23:08 
AnswerRe: TOR client Pin
ahmet_uzun23-Jul-16 21:55
Memberahmet_uzun23-Jul-16 21:55 
GeneralRe: TOR client Pin
Member 1258453524-Jul-16 21:53
MemberMember 1258453524-Jul-16 21:53 
GeneralRe: TOR client Pin
ahmet_uzun25-Jul-16 0:41
Memberahmet_uzun25-Jul-16 0:41 

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.