Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Tor browser context #473

Merged
merged 36 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3c49f14
Tor Browser Context
darkdh Feb 1, 2018
3c4b47b
Get tor path from browser-laptop
jumde Mar 15, 2018
207975d
WIP: skip torrc altogether, pass command-line arguments
riastradh-brave Mar 24, 2018
f4b2b29
Make sure tor process is launched before finishing browser context
darkdh Mar 29, 2018
3a37921
Use pipe hack to notify browser process that tor process has terminated
darkdh Apr 4, 2018
06873bb
Only handle child core dump or be terminated by signal or exit.
darkdh Apr 4, 2018
4ca20e9
Replace GURL in tor_browser_context & move tor out of brave browser c…
jumde Apr 4, 2018
a9dec23
Move waitpid to monitor thread
darkdh Apr 6, 2018
a06ff8d
Windows Fix
darkdh Apr 9, 2018
6e5096f
Prevent launching tor per origin and cleanup
darkdh Apr 13, 2018
ed092ca
C66 rebase
darkdh Apr 25, 2018
a0c005f
rename tor_imp -> tor_launcher_impl
darkdh Apr 25, 2018
80a8283
linux header fix (Mac and Windows have build/precompile.h)
darkdh Apr 25, 2018
bb6efb2
Avoid compiler unused return value warning
darkdh May 1, 2018
579d7d2
Inherits web requests handlers from default parition
darkdh May 7, 2018
9b7ab85
Use TorLauncherFactory to launch tor when browser context init
darkdh May 8, 2018
b79be43
Put tor console log into a file
darkdh May 9, 2018
ee12072
Config start_hidden in order to set wShowWindow = SW_HIDE for child p…
darkdh May 9, 2018
92cc9e2
use start_hidden for utility process launching tor
darkdh May 11, 2018
3a07530
Add --TruncateLogFile and --pidfile
darkdh May 11, 2018
bc12974
Enable tor daemon control port.
riastradh-brave May 16, 2018
bef8c90
Put the control auth cookie in the watch directory too.
riastradh-brave May 17, 2018
1d26b72
Fixes #602 - Add Overlay pref store for tor partition
jumde Jun 6, 2018
1588b13
Use network delegate to intercept every requests and apply proxy config
darkdh Jun 14, 2018
756c3ca
Propagate off-the-recordness to URL request context.
riastradh-brave Jun 14, 2018
982d067
C67 rebase
darkdh Jun 18, 2018
86b4d81
Export Session.relaunchTor API which will terminate current tor process
darkdh Jun 21, 2018
53efe51
Export Session.setTorLauncherCallback((result, pid) =>{...}) and Sess…
darkdh Jun 22, 2018
5657527
Retire tor circuit isolation key only when it is about to be used
darkdh Jun 22, 2018
fbf2a29
Use a priority queue to expire all >10min-old tor circuit records.
riastradh-brave Jun 23, 2018
c59eec1
Use a timer to expire circuits promptly.
riastradh-brave Jun 25, 2018
1ee6a7f
patch cleanup
darkdh Jul 12, 2018
24d5346
Prevent `SuicideOnChannelErrorFilter` to be added to tor_launcher uti…
darkdh Jul 12, 2018
65872a4
Use new site instance for SessionStorageNamespaceImpl clone when open…
darkdh Jul 14, 2018
fb9327a
Merge pull request #631 from brave/tor_cleanup
darkdh Jul 17, 2018
0ef0d3c
Merge pull request #632 from brave/noopener_check
darkdh Jul 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
import("//v8/gni/v8.gni")
import("//third_party/icu/config.gni")
import("//media/cdm/library_cdm/cdm_paths.gni")
import("//mojo/public/tools/bindings/mojom.gni")
import("//electron/build/config.gni")

group("electron") {
Expand Down Expand Up @@ -55,7 +56,10 @@ group("electron") {
}

if (use_aura && (is_win || is_linux)) {
data_deps += [ "app:service_manifests" ]
data_deps += [
"//chrome/app:service_manifests",
"//chrome/app:chrome_renderer_manifest",
]
}
}

Expand Down Expand Up @@ -84,7 +88,7 @@ grit("brave_resources") {
]

deps = [
"app:brave_content_manifest_overlays",
"//chrome/app:chrome_content_manifest_overlays",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused by this change. Why are switching to chrome_content_manifest_overlays and then patching them to add tor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

original app:brave_content_manifest_overlays doesn't work at all.
It supposed to package //chrome/utility:profile_import_manifest but it never does.

"//chrome/browser/safe_browsing",
]

Expand Down Expand Up @@ -239,13 +243,25 @@ source_set("common") {
"//base",
"//components/url_formatter",
"//content/public/child",
":mojo_bindings",
]

if (enable_extensions) {
public_deps += [
"brave/common/extensions/api",
]
}

}

mojom("mojo_bindings") {
sources = [
"brave/common/tor/tor.mojom",
]

public_deps = [
"//mojo/public/mojom/base",
]
}

source_set("utility") {
Expand All @@ -266,6 +282,7 @@ source_set("utility") {
"//components/cookie_config",
"//services/proxy_resolver:lib",
"//third_party/protobuf:protobuf_lite",
":tor",
]

if (use_glib) {
Expand All @@ -291,6 +308,19 @@ source_set("utility") {
]
}

source_set("tor") {
sources = [
"brave/utility/tor/tor_launcher_impl.cc",
"brave/utility/tor/tor_launcher_impl.h",
"brave/utility/tor/tor_service.cc",
"brave/utility/tor/tor_service.h",
]

deps = [
":mojo_bindings",
]
}

source_set("renderer") {
public_configs = [
"build:electron_config",
Expand Down
30 changes: 3 additions & 27 deletions app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,9 @@ import("//build/config/ui.gni")
import("//tools/grit/grit_rule.gni")
import("//services/service_manager/public/service_manifest.gni")

service_manifest("brave_content_packaged_services_manifest_overlay") {
source = "//chrome/browser/chrome_content_packaged_services_manifest_overlay.json"
packaged_services = [
"//chrome/utility:profile_import_manifest",
]
}

group("brave_content_manifest_overlays") {
deps = [
"//chrome/app:chrome_content_manifest_overlays",
":brave_content_packaged_services_manifest_overlay",
]
}

if (use_aura) {
service_manifest("brave_content_packaged_services_manifest") {
source_manifest = "//content/public/app:packaged_services_manifest"
overlays = [ ":brave_content_packaged_services_manifest_overlay" ]
}

group("service_manifests") {
data_deps = [
"//chrome/app:service_manifests",
"//chrome/app:chrome_renderer_manifest",
":brave_content_packaged_services_manifest_overlay",
]
}
service_manifest("tor_launcher_manifest") {
name = "tor_launcher"
source = "//electron//brave/utility/tor/tor_manifest.json"
}

grit("brave_strings") {
Expand Down
3 changes: 3 additions & 0 deletions app/brave_strings.grd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Brave. These strings will be translated for each locale that Brave supports -->
<message name="IDS_SHORT_PRODUCT_NAME" desc="The Brave application short name">
Brave
</message>
<message name="IDS_UTILITY_PROCESS_TOR_LAUNCHER_NAME" desc="The utility process which launch Tor">
Tor Launcher
</message>
</messages>
</release>
</grit>
109 changes: 92 additions & 17 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "base/time/time.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/browser/brave_permission_manager.h"
#include "brave/browser/tor/tor_launcher_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -177,6 +178,19 @@ struct Converter<net::ProxyConfig> {
}
};

template<>
struct Converter<brave::TorLauncherFactory::TorProcessState> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate, brave::TorLauncherFactory::TorProcessState val) {
if (val == brave::TorLauncherFactory::TorProcessState::LAUNCH_SUCCEEDED)
return mate::StringToV8(isolate, "launch-succeeded");
else if (val == brave::TorLauncherFactory::TorProcessState::LAUNCH_FAILED)
return mate::StringToV8(isolate, "launch-failed");
else if (val == brave::TorLauncherFactory::TorProcessState::CRASHED)
return mate::StringToV8(isolate, "crashed");
}
};

} // namespace mate

namespace atom {
Expand Down Expand Up @@ -215,14 +229,14 @@ class ResolveProxyHelper {
const GURL& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

net::ProxyResolutionService* proxy_service =
net::ProxyResolutionService* proxy_resolution_service =
context_getter->GetURLRequestContext()->proxy_resolution_service();
net::CompletionCallback completion_callback =
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
base::Unretained(this));

// Start the request.
int result = proxy_service->ResolveProxy(
int result = proxy_resolution_service->ResolveProxy(
url, "GET", &proxy_info_, completion_callback,
&pac_req_, nullptr, net::NetLogWithSource());

Expand Down Expand Up @@ -293,20 +307,6 @@ void DoCacheActionInIO(
on_get_backend.Run(net::OK);
}

void SetProxyInIO(scoped_refptr<net::URLRequestContextGetter> getter,
const net::ProxyConfig& config,
const base::Closure& callback) {
auto proxy_service =
getter->GetURLRequestContext()->proxy_resolution_service();
proxy_service->ResetConfigService(
base::WrapUnique(new net::ProxyConfigServiceFixed(
net::ProxyConfigWithAnnotation(config, NO_TRAFFIC_ANNOTATION_YET))));
// Refetches and applies the new pac script if provided.
proxy_service->ForceReloadProxyConfig();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, callback);
}

void SetCertVerifyProcInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
const AtomCertVerifier::VerifyProc& proc) {
Expand Down Expand Up @@ -464,10 +464,24 @@ void Session::FlushStorageData() {
storage_partition->Flush();
}

void SetProxyInIO(scoped_refptr<net::URLRequestContextGetter> getter,
const net::ProxyConfig& config,
const base::Closure& callback) {
auto proxy_resolution_service =
getter->GetURLRequestContext()->proxy_resolution_service();
proxy_resolution_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(
net::ProxyConfigWithAnnotation(config, NO_TRAFFIC_ANNOTATION_YET))));
// Refetches and applies the new pac script if provided.
proxy_resolution_service->ForceReloadProxyConfig();
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, callback);
}

void Session::SetProxy(const net::ProxyConfig& config,
const base::Closure& callback) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&SetProxyInIO, request_context_getter_, config, callback));
base::Bind(&SetProxyInIO, request_context_getter_, config, callback));
}

void Session::SetDownloadPath(const base::FilePath& path) {
Expand Down Expand Up @@ -601,6 +615,62 @@ bool Session::Equal(Session* session) const {
#endif
}

bool Session::IsOffTheRecord() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (brave_browser_context->IsOffTheRecord())
return true;
if (brave_browser_context->IsIsolatedStorage())
return true;
return false;
}

void Session::SetTorNewIdentity(const GURL& url,
const base::Closure& callback) const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->SetTorNewIdentity(url, callback);
}

void Session::RelaunchTor() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->RelaunchTor();
}

int64_t Session::GetTorPid() const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return -1;
}
return brave_browser_context->GetTorPid();
}

void Session::SetTorLauncherCallback(mate::Arguments* args) {
brave::TorLauncherFactory::TorLauncherCallback callback;
if (!args->GetNext(&callback)) {
args->ThrowError("`callback(result, pid)` is a required field");
return;
}
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsTorBrowserContext()) {
LOG(ERROR) << __func__ << " only available for tor browser context";
return;
}
brave_browser_context->SetTorLauncherCallback(callback);
}

// static
mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate, content::BrowserContext* browser_context) {
Expand Down Expand Up @@ -659,6 +729,11 @@ void Session::BuildPrototype(v8::Isolate* isolate,
&Session::AllowNTLMCredentialsForDomains)
.SetMethod("setEnableBrotli", &Session::SetEnableBrotli)
.SetMethod("equal", &Session::Equal)
.SetMethod("isOffTheRecord", &Session::IsOffTheRecord)
.SetMethod("setTorNewIdentity", &Session::SetTorNewIdentity)
.SetMethod("relaunchTor", &Session::RelaunchTor)
.SetMethod("setTorLauncherCallback", &Session::SetTorLauncherCallback)
.SetMethod("getTorPid", &Session::GetTorPid)
.SetProperty("partition", &Session::Partition)
.SetProperty("contentSettings", &Session::ContentSettings)
.SetProperty("userPrefs", &Session::UserPrefs)
Expand Down
6 changes: 6 additions & 0 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class Session: public mate::TrackableObject<Session>,
v8::Local<v8::Value> SpellChecker(v8::Isolate* isolate);
v8::Local<v8::Value> Extensions(v8::Isolate* isolate);
bool Equal(Session* session) const;
bool IsOffTheRecord() const;
void SetTorNewIdentity(const GURL& url,
const base::Closure& callback) const;
void RelaunchTor() const;
void SetTorLauncherCallback(mate::Arguments* args);
int64_t GetTorPid() const;

protected:
Session(v8::Isolate* isolate, Profile* browser_context);
Expand Down
12 changes: 12 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,18 @@ mate::Handle<api::Session> SessionFromOptions(v8::Isolate* isolate,
if (options.Get("parent_partition", &parent_partition)) {
session_options.SetString("parent_partition", parent_partition);
}
bool isolated_storage;
if (options.Get("isolated_storage", &isolated_storage)) {
session_options.SetBoolean("isolated_storage", isolated_storage);
}
std::string tor_proxy;
if (options.Get("tor_proxy", &tor_proxy)) {
session_options.SetString("tor_proxy", tor_proxy);
}
std::string tor_path;
if (options.Get("tor_path", &tor_path)) {
session_options.SetString("tor_path", tor_path);
}
session = Session::FromPartition(isolate, partition, session_options);
} else {
// Use the default session if not specified.
Expand Down
18 changes: 10 additions & 8 deletions atom/browser/extensions/atom_extensions_network_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ class AtomExtensionsNetworkDelegate : public atom::AtomNetworkDelegate {

static void SetAcceptAllCookies(bool accept);

protected:
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;

private:
// NetworkDelegate implementation.
int OnBeforeURLRequestInternal(
net::URLRequest* request,
GURL* new_url);
int OnBeforeURLRequest(net::URLRequest* request,
const net::CompletionCallback& callback,
GURL* new_url) override;
int OnBeforeStartTransactionInternal(
net::URLRequest* request,
net::HttpRequestHeaders* headers);
int OnBeforeStartTransaction(net::URLRequest* request,
const net::CompletionCallback& callback,
net::HttpRequestHeaders* headers) override;
void OnStartTransaction(net::URLRequest* request,
const net::HttpRequestHeaders& headers) override;
int OnHeadersReceivedInternal(
Expand All @@ -57,8 +61,6 @@ class AtomExtensionsNetworkDelegate : public atom::AtomNetworkDelegate {
const net::HttpResponseHeaders* original_response_headers,
scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
GURL* allowed_unsafe_redirect_url) override;
void OnBeforeRedirect(net::URLRequest* request,
const GURL& new_location) override;
void OnResponseStarted(net::URLRequest* request, int net_error) override;
void OnCompleted(net::URLRequest* request,
bool started,
Expand Down
8 changes: 8 additions & 0 deletions atom/utility/atom_content_utility_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "brave/common/tor/tor.mojom.h"
#include "brave/utility/importer/brave_profile_import_service.h"
#include "brave/utility/tor/tor_service.h"
#include "chrome/common/importer/profile_import.mojom.h"
#include "components/services/unzip/public/interfaces/constants.mojom.h"
#include "components/services/unzip/unzip_service.h"
Expand Down Expand Up @@ -129,6 +131,12 @@ void AtomContentUtilityClient::RegisterServices(
base::BindRepeating(&unzip::UnzipService::CreateService);
services->emplace(unzip::mojom::kServiceName, service_info);
}

service_manager::EmbeddedServiceInfo tor_info;
tor_info.factory =
base::Bind(&TorService::CreateService);
services->emplace(tor::mojom::kTorServiceName,
tor_info);
}

// static
Expand Down
Loading