This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use network delegate to intercept every requests and apply proxy config
fix brave/browser-laptop#14390 Auditors: @bridiver, @riastradh-brave
- Loading branch information
Showing
8 changed files
with
200 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ using content::BrowserThread; | |
const int kTorPasswordLength = 16; | ||
|
||
ProxyConfigServiceTor::ProxyConfigServiceTor( | ||
const std::string& tor_proxy) { | ||
const std::string& tor_proxy, const std::string& username, | ||
TorProxyMap* tor_proxy_map) { | ||
if (tor_proxy.length()) { | ||
url::Parsed url; | ||
url::ParseStandardURL( | ||
|
@@ -50,53 +51,51 @@ ProxyConfigServiceTor::ProxyConfigServiceTor( | |
std::string(tor_proxy.begin() + url.port.begin, | ||
tor_proxy.begin() + url.port.begin + url.port.len); | ||
} | ||
std::string proxy_url; | ||
if (tor_proxy_map || username.empty()) { | ||
auto found = tor_proxy_map->find(username); | ||
std::string password; | ||
if (found == tor_proxy_map->end()) { | ||
password = GenerateNewPassword(); | ||
tor_proxy_map->insert(std::pair<std::string, std::string>(username, | ||
password)); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
darkdh
Author
Member
|
||
} else { | ||
password = found->second; | ||
} | ||
proxy_url = std::string(scheme_ + "://" + username + ":" + password + | ||
"@" + host_ + ":" + port_); | ||
} else { | ||
proxy_url = std::string(scheme_ + "://" + host_ + ":" + port_); | ||
} | ||
config_.proxy_rules().ParseFromString(proxy_url); | ||
} | ||
config_.proxy_rules().ParseFromString(std::string(scheme_ + "://" + host_ | ||
+ ":" + port_)); | ||
} | ||
|
||
ProxyConfigServiceTor::~ProxyConfigServiceTor() {} | ||
|
||
void ProxyConfigServiceTor::TorSetProxy( | ||
scoped_refptr<brightray::URLRequestContextGetter> | ||
url_request_context_getter, | ||
const std::string tor_proxy, | ||
const bool isolated_storage, | ||
const base::FilePath partition_path) { | ||
DCHECK_CURRENTLY_ON(BrowserThread::UI); | ||
if (!url_request_context_getter || !isolated_storage) | ||
net::ProxyResolutionService* service, | ||
const std::string& tor_proxy, | ||
const std::string& site_url, | ||
TorProxyMap* tor_proxy_map, | ||
bool new_password) { | ||
if (!service) | ||
return; | ||
auto proxy_service = url_request_context_getter->GetURLRequestContext()-> | ||
proxy_resolution_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().AsUTF8Unsafe(); | ||
if (new_password && tor_proxy_map) | ||
tor_proxy_map->erase(site_url); | ||
std::unique_ptr<net::ProxyConfigServiceTor> | ||
config(new ProxyConfigServiceTor(tor_proxy)); | ||
config->SetUsername(origin); | ||
proxy_service->ResetConfigService(std::move(config)); | ||
config(new ProxyConfigServiceTor(tor_proxy, site_url, tor_proxy_map)); | ||
service->ResetConfigService(std::move(config)); | ||
} | ||
|
||
ProxyConfigServiceTor::ConfigAvailability | ||
ProxyConfigServiceTor::GetLatestProxyConfig(ProxyConfig* config) { | ||
if (scheme_ != kSocksProxy || host_.empty() || port_.empty()) | ||
return CONFIG_UNSET; | ||
std::string password = GenerateNewPassword(); | ||
std::string url = std::string(scheme_ + "://" + username_ + ":" + password + | ||
"@" + host_ + ":" + port_); | ||
config_.proxy_rules().ParseFromString(url); | ||
*config = config_; | ||
return CONFIG_VALID; | ||
} | ||
|
||
bool ProxyConfigServiceTor::SetUsername(const std::string& username) { | ||
if (username.empty()) | ||
return false; | ||
username_ = username; | ||
return true; | ||
} | ||
|
||
|
||
std::string ProxyConfigServiceTor::GenerateNewPassword() { | ||
std::vector<uint8_t> password(kTorPasswordLength); | ||
crypto::RandBytes(password.data(), password.size()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2018 The Brave Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "brave/browser/net/tor_proxy_network_delegate.h" | ||
|
||
#include "brave/browser/net/proxy_resolution/proxy_config_service_tor.h" | ||
#include "content/public/browser/browser_thread.h" | ||
#include "content/public/browser/site_instance.h" | ||
#include "extensions/common/constants.h" | ||
#include "net/base/url_util.h" | ||
#include "net/url_request/url_request.h" | ||
#include "net/url_request/url_request_context.h" | ||
|
||
using content::BrowserThread; | ||
|
||
namespace brave { | ||
|
||
TorProxyNetworkDelegate::TorProxyNetworkDelegate( | ||
Profile* profile, | ||
extensions::InfoMap* info_map, | ||
extensions::EventRouterForwarder* event_router) : | ||
extensions::AtomExtensionsNetworkDelegate(profile, info_map, | ||
event_router), | ||
browser_context_(static_cast<BraveBrowserContext*>(profile)) {} | ||
|
||
TorProxyNetworkDelegate::~TorProxyNetworkDelegate() {} | ||
|
||
int TorProxyNetworkDelegate::OnBeforeURLRequest( | ||
net::URLRequest* request, | ||
const net::CompletionCallback& callback, | ||
GURL* new_url) { | ||
ConfigTorProxyInteral(request); | ||
return extensions::AtomExtensionsNetworkDelegate::OnBeforeURLRequest(request, | ||
callback, | ||
new_url); | ||
} | ||
|
||
int TorProxyNetworkDelegate::OnBeforeStartTransaction( | ||
net::URLRequest* request, | ||
const net::CompletionCallback& callback, | ||
net::HttpRequestHeaders* headers) { | ||
ConfigTorProxyInteral(request); | ||
return extensions::AtomExtensionsNetworkDelegate:: | ||
OnBeforeStartTransaction(request, | ||
callback, | ||
headers); | ||
} | ||
|
||
void TorProxyNetworkDelegate::OnBeforeRedirect( | ||
net::URLRequest* request, | ||
const GURL& new_location) { | ||
ConfigTorProxyInteral(request); | ||
extensions::AtomExtensionsNetworkDelegate::OnBeforeRedirect(request, | ||
new_location); | ||
} | ||
|
||
void TorProxyNetworkDelegate::ConfigTorProxyInteral(net::URLRequest* request) { | ||
if (!request) | ||
return; | ||
auto proxy_service = request->context()->proxy_resolution_service(); | ||
GURL url(content::SiteInstance::GetSiteForURL(browser_context_, | ||
request->url())); | ||
if (!url.SchemeIs(extensions::kExtensionScheme) && !net::IsLocalhost(url)) | ||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | ||
base::Bind(&net::ProxyConfigServiceTor::TorSetProxy, | ||
proxy_service, | ||
browser_context_->tor_proxy(), | ||
url.host(), | ||
browser_context_->tor_proxy_map(), | ||
false)); | ||
} | ||
|
||
} // namespace brave |
Oops, something went wrong.
Do these entries ever get garbage-collected?