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

Commit

Permalink
Tor Browser Context
Browse files Browse the repository at this point in the history
1. context creating options: "isolated_storage", "tor_proxy"
2. setTorNewIdentity API
3. automatically launch tor when tor browser context created and tear it
down when tor browser context destroyed

fix #468
fix #464
fix #509
  • Loading branch information
darkdh committed Mar 20, 2018
1 parent 3fb05f2 commit 5e85f53
Show file tree
Hide file tree
Showing 15 changed files with 388 additions and 48 deletions.
43 changes: 29 additions & 14 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,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_service();
proxy_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(config)));
// 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 @@ -457,8 +445,14 @@ void Session::FlushStorageData() {

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));
auto proxy_service =
request_context_getter_->GetURLRequestContext()->proxy_service();
proxy_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(config)));
// Refetches and applies the new pac script if provided.
proxy_service->ForceReloadProxyConfig();
if (callback)
callback.Run();
}

void Session::SetDownloadPath(const base::FilePath& path) {
Expand Down Expand Up @@ -592,6 +586,25 @@ 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->IsIsolatedStorage())
return;
brave_browser_context->SetTorNewIdentity(url, callback);
}

// static
mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate, content::BrowserContext* browser_context) {
Expand Down Expand Up @@ -649,6 +662,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
&Session::AllowNTLMCredentialsForDomains)
.SetMethod("setEnableBrotli", &Session::SetEnableBrotli)
.SetMethod("equal", &Session::Equal)
.SetMethod("isOffTheRecord", &Session::IsOffTheRecord)
.SetMethod("setTorNewIdentity", &Session::SetTorNewIdentity)
.SetProperty("partition", &Session::Partition)
.SetProperty("contentSettings", &Session::ContentSettings)
.SetProperty("userPrefs", &Session::UserPrefs)
Expand Down
3 changes: 3 additions & 0 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ 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;

protected:
Session(v8::Isolate* isolate, Profile* browser_context);
Expand Down
8 changes: 8 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,14 @@ 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);
}
session = Session::FromPartition(isolate, partition, session_options);
} else {
// Use the default session if not specified.
Expand Down
16 changes: 16 additions & 0 deletions brave/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ source_set("browser") {

deps = [
":apis",
":proxy",
"//electron/atom/browser",
"//electron:common",
"//electron/muon/app",
Expand Down Expand Up @@ -153,3 +154,18 @@ source_set("apis") {
"component_updater",
]
}

source_set("proxy") {
configs += [
"//electron/build:electron_config",
]

sources = [
"net/proxy/proxy_config_service_tor.cc",
"net/proxy/proxy_config_service_tor.h",
]

deps = [
"//net",
]
}
132 changes: 131 additions & 1 deletion brave/browser/brave_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "base/path_service.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/process/launch.h"
#include "base/trace_event/trace_event.h"
#include "brave/browser/brave_permission_manager.h"
#include "brave/browser/net/proxy/proxy_config_service_tor.h"
#include "chrome/browser/background_fetch/background_fetch_delegate_factory.h"
#include "chrome/browser/background_fetch/background_fetch_delegate_impl.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -41,17 +43,23 @@
#include "components/user_prefs/user_prefs.h"
#include "components/zoom/zoom_event_manager.h"
#include "components/webdata/common/webdata_constants.h"
#include "content/browser/storage_partition_impl_map.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/dom_storage_context.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/pref_names.h"
#include "extensions/features/features.h"
#include "net/base/escape.h"
#include "net/cookies/cookie_store.h"
#include "net/proxy/proxy_service.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "vendor/brightray/browser/browser_client.h"
#include "vendor/brightray/browser/net_log.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "atom/browser/extensions/atom_browser_client_extensions_part.h"
Expand Down Expand Up @@ -153,6 +161,8 @@ BraveBrowserContext::BraveBrowserContext(
ready_(new base::WaitableEvent(
base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED)),
isolated_storage_(false),
in_memory_(in_memory),
io_task_runner_(std::move(io_task_runner)),
delegate_(g_browser_process->profile_manager()) {
std::string parent_partition;
Expand All @@ -162,6 +172,21 @@ BraveBrowserContext::BraveBrowserContext(
atom::AtomBrowserContext::From(parent_partition, false));
}

bool isolated_storage;
if (options.GetBoolean("isolated_storage", &isolated_storage)) {
isolated_storage_ = isolated_storage;
}

std::string tor_proxy;
if (options.GetString("tor_proxy", &tor_proxy)) {
tor_proxy_ = GURL(tor_proxy);
if (!tor_process_.IsValid()) {
base::FilePath tor("/usr/local/bin/tor");
base::CommandLine cmdline(tor);
tor_process_ = base::LaunchProcess(cmdline, base::LaunchOptions());
}
}

if (in_memory) {
original_context_ = static_cast<BraveBrowserContext*>(
atom::AtomBrowserContext::From(partition, false));
Expand All @@ -185,6 +210,10 @@ BraveBrowserContext::BraveBrowserContext(
BraveBrowserContext::~BraveBrowserContext() {
MaybeSendDestroyedNotification();

if (tor_process_.IsValid()) {
base::EnsureProcessTerminated(std::move(tor_process_));
}

if (track_zoom_subscription_.get())
track_zoom_subscription_.reset(nullptr);

Expand Down Expand Up @@ -305,6 +334,61 @@ content::BrowserPluginGuestManager* BraveBrowserContext::GetGuestManager() {
return guest_view::GuestViewManager::FromBrowserContext(this);
}

net::URLRequestContextGetter*
BraveBrowserContext::CreateRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) {
if (isolated_storage_) {
scoped_refptr<brightray::URLRequestContextGetter>
url_request_context_getter =
new brightray::URLRequestContextGetter(
this,
static_cast<brightray::NetLog*>(brightray::BrowserClient::Get()->
GetNetLog()),
partition_path,
in_memory,
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
protocol_handlers,
std::move(request_interceptors));
StoragePartitionDescriptor descriptor(partition_path, in_memory);
url_request_context_getter_map_[descriptor] = url_request_context_getter;
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&BraveBrowserContext::TorSetProxy,
base::Unretained(this),
url_request_context_getter,
partition_path));
return url_request_context_getter.get();
} else {
return nullptr;
}
}

net::URLRequestContextGetter*
BraveBrowserContext::CreateMediaRequestContextForStoragePartition(
const base::FilePath& partition_path,
bool in_memory) {
if (isolated_storage_) {
StoragePartitionDescriptor descriptor(partition_path, in_memory);
URLRequestContextGetterMap::iterator iter =
url_request_context_getter_map_.find(descriptor);
if (iter != url_request_context_getter_map_.end())
return (iter->second).get();
else
return nullptr;
} else {
return nullptr;
}
}

bool BraveBrowserContext::IsOffTheRecord() const {
if (isolated_storage_)
return true;
return in_memory_;
}

void BraveBrowserContext::TrackZoomLevelsFromParent() {
// Here we only want to use zoom levels stored in the main-context's default
// storage partition. We're not interested in zoom levels in special
Expand Down Expand Up @@ -363,6 +447,26 @@ void BraveBrowserContext::UpdateDefaultZoomLevel() {
->OnDefaultZoomLevelChanged();
}

void BraveBrowserContext::TorSetProxy(
scoped_refptr<brightray::URLRequestContextGetter>
url_request_context_getter,
const base::FilePath partition_path) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!url_request_context_getter || !isolated_storage_)
return;
if (tor_proxy_.is_valid()) {
auto proxy_service = url_request_context_getter->GetURLRequestContext()->
proxy_service();
// Notice CreateRequestContextForStoragePartition will only be called once
// per partition_path so there is no need to cache password per origin
std::string origin = partition_path.DirName().BaseName().value();
std::unique_ptr<net::ProxyConfigServiceTor>
config(new net::ProxyConfigServiceTor(tor_proxy_));
config->SetUsername(origin);
proxy_service->ResetConfigService(std::move(config));
}
}

content::PermissionManager* BraveBrowserContext::GetPermissionManager() {
if (!permission_manager_.get())
permission_manager_.reset(new BravePermissionManager);
Expand Down Expand Up @@ -402,6 +506,11 @@ BraveBrowserContext::CreateURLRequestJobFactory(
extensions::CreateExtensionProtocolHandler(IsOffTheRecord(),
info_map_));
#endif
if (!protocol_handler_interceptor_.get()) {
protocol_handler_interceptor_ =
ProtocolHandlerRegistryFactory::GetForBrowserContext(this)
->CreateJobInterceptorFactory();
}
protocol_handler_interceptor_->Chain(std::move(job_factory));
return std::move(protocol_handler_interceptor_);
}
Expand Down Expand Up @@ -605,7 +714,7 @@ std::string BraveBrowserContext::partition_with_prefix() {
if (canonical_partition.empty())
canonical_partition = "default";

if (IsOffTheRecord())
if (IsOffTheRecord() && !isolated_storage_)
return canonical_partition;

return kPersistPrefix + canonical_partition;
Expand Down Expand Up @@ -643,6 +752,27 @@ void BraveBrowserContext::SetExitType(ExitType exit_type) {
}
}

void BraveBrowserContext::SetTorNewIdentity(const GURL& url,
const base::Closure& callback) {
GURL site_url(content::SiteInstance::GetSiteForURL(this, url));
const std::string host = site_url.host();
base::FilePath partition_path = this->GetPath().Append(
content::StoragePartitionImplMap::GetStoragePartitionPath(host, host));
scoped_refptr<brightray::URLRequestContextGetter> url_request_context_getter;
StoragePartitionDescriptor descriptor(partition_path, true);
URLRequestContextGetterMap::iterator iter =
url_request_context_getter_map_.find(descriptor);
if (iter != url_request_context_getter_map_.end())
url_request_context_getter = (iter->second);
else
return;
auto proxy_service = url_request_context_getter->GetURLRequestContext()->
proxy_service();
proxy_service->ForceReloadProxyConfig();
if (callback)
callback.Run();
}

scoped_refptr<base::SequencedTaskRunner>
BraveBrowserContext::GetIOTaskRunner() {
return io_task_runner_;
Expand Down
Loading

0 comments on commit 5e85f53

Please sign in to comment.