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

Commit

Permalink
Add "SetTorNewIdentity"
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Feb 6, 2018
1 parent edad1b2 commit 7e052dd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
9 changes: 9 additions & 0 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ bool Session::IsOffTheRecord() const {
return false;
}

void Session::SetTorNewIdentity(const GURL& origin) const {
brave::BraveBrowserContext* brave_browser_context =
brave::BraveBrowserContext::FromBrowserContext(profile_);
if (!brave_browser_context->IsIsolatedStorage())
return;
brave_browser_context->SetTorNewIdentity(origin);
}

// static
mate::Handle<Session> Session::CreateFrom(
v8::Isolate* isolate, content::BrowserContext* browser_context) {
Expand Down Expand Up @@ -660,6 +668,7 @@ void Session::BuildPrototype(v8::Isolate* isolate,
.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
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Session: public mate::TrackableObject<Session>,
v8::Local<v8::Value> Extensions(v8::Isolate* isolate);
bool Equal(Session* session) const;
bool IsOffTheRecord() const;
void SetTorNewIdentity(const GURL& origin) const;

protected:
Session(v8::Isolate* isolate, Profile* browser_context);
Expand Down
66 changes: 45 additions & 21 deletions brave/browser/brave_browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#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"
Expand Down Expand Up @@ -344,27 +345,7 @@ BraveBrowserContext::CreateRequestContextForStoragePartition(
std::move(request_interceptors));
StoragePartitionDescriptor descriptor(partition_path, in_memory);
url_request_context_getter_map_[descriptor] = url_request_context_getter;
if (!tor_proxy_.empty() && GURL(tor_proxy_).is_valid()) {
auto proxy_service = url_request_context_getter->GetURLRequestContext()->
proxy_service();
net::ProxyConfig config;
// 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::string encoded_password;
std::vector<uint8_t> password(kTorPasswordLength);
crypto::RandBytes(password.data(), password.size());
encoded_password = base::HexEncode(password.data(), password.size());
std::string url = tor_proxy_;
base::ReplaceFirstSubstringAfterOffset(
&url, 0, "//", "//" + origin + ":" + encoded_password + "@");
// TODO(darkdh): using URL with auth after
// https://github.com/brave/muon/pull/470
config.proxy_rules().ParseFromString(tor_proxy_);
proxy_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(config)));
proxy_service->ForceReloadProxyConfig();
}
TorSetProxy(url_request_context_getter, partition_path);
return url_request_context_getter;
} else {
return nullptr;
Expand Down Expand Up @@ -446,6 +427,34 @@ void BraveBrowserContext::UpdateDefaultZoomLevel() {
->OnDefaultZoomLevelChanged();
}

void BraveBrowserContext::TorSetProxy(
brightray::URLRequestContextGetter* url_request_context_getter,
const base::FilePath partition_path) {
if (!url_request_context_getter || !isolated_storage_)
return;
if (!tor_proxy_.empty() && GURL(tor_proxy_).is_valid()) {
auto proxy_service = url_request_context_getter->GetURLRequestContext()->
proxy_service();
net::ProxyConfig config;
// 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::string encoded_password;
std::vector<uint8_t> password(kTorPasswordLength);
crypto::RandBytes(password.data(), password.size());
encoded_password = base::HexEncode(password.data(), password.size());
std::string url = tor_proxy_;
base::ReplaceFirstSubstringAfterOffset(
&url, 0, "//", "//" + origin + ":" + encoded_password + "@");
// TODO(darkdh): using URL with auth after
// https://github.com/brave/muon/pull/470
config.proxy_rules().ParseFromString(tor_proxy_);
proxy_service->ResetConfigService(base::WrapUnique(
new net::ProxyConfigServiceFixed(config)));
proxy_service->ForceReloadProxyConfig();
}
}

content::PermissionManager* BraveBrowserContext::GetPermissionManager() {
if (!permission_manager_.get())
permission_manager_.reset(new BravePermissionManager);
Expand Down Expand Up @@ -731,6 +740,21 @@ void BraveBrowserContext::SetExitType(ExitType exit_type) {
}
}

void BraveBrowserContext::SetTorNewIdentity(const GURL& origin) {
const std::string host = origin.host();
base::FilePath partition_path = this->GetPath().Append(
content::StoragePartitionImplMap::GetStoragePartitionPath(host, host));
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).get();
else
return;
TorSetProxy(url_request_context_getter, partition_path);
}

scoped_refptr<base::SequencedTaskRunner>
BraveBrowserContext::GetIOTaskRunner() {
return io_task_runner_;
Expand Down
6 changes: 6 additions & 0 deletions brave/browser/brave_browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class BraveBrowserContext : public Profile {

bool IsIsolatedStorage() const { return isolated_storage_; }

void SetTorNewIdentity(const GURL& origin);

private:
typedef std::map<StoragePartitionDescriptor,
scoped_refptr<brightray::URLRequestContextGetter>,
Expand All @@ -152,6 +154,10 @@ class BraveBrowserContext : public Profile {
const content::HostZoomMap::ZoomLevelChange& change);
void UpdateDefaultZoomLevel();

void TorSetProxy(
brightray::URLRequestContextGetter* url_request_context_getter,
const base::FilePath partition_path);

scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
std::unique_ptr<sync_preferences::PrefServiceSyncable> user_prefs_;
std::unique_ptr<PrefChangeRegistrar> user_prefs_registrar_;
Expand Down

0 comments on commit 7e052dd

Please sign in to comment.