diff --git a/atom/browser/extensions/atom_extensions_network_delegate.h b/atom/browser/extensions/atom_extensions_network_delegate.h index 3ab0d6114f..e9b10d0952 100644 --- a/atom/browser/extensions/atom_extensions_network_delegate.h +++ b/atom/browser/extensions/atom_extensions_network_delegate.h @@ -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( @@ -57,8 +61,6 @@ class AtomExtensionsNetworkDelegate : public atom::AtomNetworkDelegate { const net::HttpResponseHeaders* original_response_headers, scoped_refptr* 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, diff --git a/brave/browser/BUILD.gn b/brave/browser/BUILD.gn index d4bca2bca9..97ecf5f587 100644 --- a/brave/browser/BUILD.gn +++ b/brave/browser/BUILD.gn @@ -167,6 +167,8 @@ source_set("proxy") { sources = [ "net/proxy_resolution/proxy_config_service_tor.cc", "net/proxy_resolution/proxy_config_service_tor.h", + "net/tor_proxy_network_delegate.cc", + "net/tor_proxy_network_delegate.h", ] deps = [ diff --git a/brave/browser/brave_browser_context.cc b/brave/browser/brave_browser_context.cc index d9a53e411f..c81bf2f87f 100644 --- a/brave/browser/brave_browser_context.cc +++ b/brave/browser/brave_browser_context.cc @@ -15,7 +15,7 @@ #include "base/trace_event/trace_event.h" #include "brave/browser/brave_permission_manager.h" #include "brave/browser/tor/tor_launcher_factory.h" -#include "brave/browser/net/proxy_resolution/proxy_config_service_tor.h" +#include "brave/browser/net/tor_proxy_network_delegate.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" @@ -372,14 +372,6 @@ BraveBrowserContext::CreateRequestContextForStoragePartition( url_request_context_getter->GetURLRequestContext() ->set_network_delegate(default_network_delegate); url_request_context_getter_map_[descriptor] = url_request_context_getter; - if (tor_proxy_.size()) { - BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, - base::Bind(&net::ProxyConfigServiceTor::TorSetProxy, - url_request_context_getter, - tor_proxy_, - isolated_storage_, - partition_path)); - } return url_request_context_getter.get(); } else { return nullptr; @@ -484,9 +476,14 @@ net::URLRequestContextGetter* BraveBrowserContext::GetRequestContext() { net::NetworkDelegate* BraveBrowserContext::CreateNetworkDelegate() { DCHECK_CURRENTLY_ON(BrowserThread::IO); - return new extensions::AtomExtensionsNetworkDelegate(this, - info_map_, - g_browser_process->extension_event_router_forwarder()); + if (isolated_storage_) + return new brave::TorProxyNetworkDelegate(this, + info_map_, + g_browser_process->extension_event_router_forwarder()); + else + return new extensions::AtomExtensionsNetworkDelegate(this, + info_map_, + g_browser_process->extension_event_router_forwarder()); } std::unique_ptr @@ -748,9 +745,15 @@ void BraveBrowserContext::SetTorNewIdentity(const GURL& url, auto proxy_resolution_service = url_request_context_getter->GetURLRequestContext()-> proxy_resolution_service(); - proxy_resolution_service->ForceReloadProxyConfig(); - if (callback) - callback.Run(); + BrowserThread::PostTaskAndReply( + BrowserThread::IO, FROM_HERE, + base::Bind(&net::ProxyConfigServiceTor::TorSetProxy, + proxy_resolution_service, + tor_proxy_, + host, + &tor_proxy_map_, + true), + callback); } scoped_refptr diff --git a/brave/browser/brave_browser_context.h b/brave/browser/brave_browser_context.h index 4bedd9622e..b2dde6ecab 100644 --- a/brave/browser/brave_browser_context.h +++ b/brave/browser/brave_browser_context.h @@ -11,6 +11,7 @@ #include #include "atom/browser/atom_browser_context.h" +#include "brave/browser/net/proxy_resolution/proxy_config_service_tor.h" #include "content/public/browser/host_zoom_map.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/profiles/storage_partition_descriptor.h" @@ -146,6 +147,11 @@ class BraveBrowserContext : public Profile { void SetTorNewIdentity(const GURL& url, const base::Closure& callback); + const std::string& tor_proxy() { return tor_proxy_; } + + net::ProxyConfigServiceTor::TorProxyMap* tor_proxy_map() { + return &tor_proxy_map_; } + private: typedef std::map, @@ -177,6 +183,8 @@ class BraveBrowserContext : public Profile { bool in_memory_; std::string tor_proxy_; + net::ProxyConfigServiceTor::TorProxyMap tor_proxy_map_; + URLRequestContextGetterMap url_request_context_getter_map_; std::unique_ptr web_database_wrapper_; diff --git a/brave/browser/net/proxy_resolution/proxy_config_service_tor.cc b/brave/browser/net/proxy_resolution/proxy_config_service_tor.cc index 3a087768a6..6784f66888 100644 --- a/brave/browser/net/proxy_resolution/proxy_config_service_tor.cc +++ b/brave/browser/net/proxy_resolution/proxy_config_service_tor.cc @@ -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(username, + password)); + } 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 - 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 - 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 password(kTorPasswordLength); crypto::RandBytes(password.data(), password.size()); diff --git a/brave/browser/net/proxy_resolution/proxy_config_service_tor.h b/brave/browser/net/proxy_resolution/proxy_config_service_tor.h index 59e40ba85d..8762177a16 100644 --- a/brave/browser/net/proxy_resolution/proxy_config_service_tor.h +++ b/brave/browser/net/proxy_resolution/proxy_config_service_tor.h @@ -6,6 +6,7 @@ #define BRAVE_BROWSER_NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_TOR_H_ #include +#include #include "base/compiler_specific.h" #include "net/base/net_errors.h" @@ -21,24 +22,26 @@ const char kSocksProxy[] = "socks5"; // Implementation of ProxyConfigService that returns a tor specific result. class NET_EXPORT ProxyConfigServiceTor : public ProxyConfigService { public: - explicit ProxyConfigServiceTor(const std::string& tor_proxy); + // Used to cache of proxies + typedef std::map TorProxyMap; + + explicit ProxyConfigServiceTor(const std::string& tor_proxy, + const std::string& username, + TorProxyMap* map); ~ProxyConfigServiceTor() override; static void TorSetProxy( - scoped_refptr - url_request_context_getter, - const std::string tor_proxy, - const bool isolated_storage, - const base::FilePath partition_path); + net::ProxyResolutionService* service, + const std::string& tor_proxy, + const std::string& site_url, + TorProxyMap* tor_proxy_map, + bool new_password); // ProxyConfigService methods: void AddObserver(Observer* observer) override {} void RemoveObserver(Observer* observer) override {} ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) override; - // Set origin as username - bool SetUsername(const std::string& username); - private: // Generate a new 128 bit random tag std::string GenerateNewPassword(); @@ -48,7 +51,6 @@ class NET_EXPORT ProxyConfigServiceTor : public ProxyConfigService { std::string scheme_; std::string host_; std::string port_; - std::string username_; }; } // namespace net diff --git a/brave/browser/net/tor_proxy_network_delegate.cc b/brave/browser/net/tor_proxy_network_delegate.cc new file mode 100644 index 0000000000..8ebdc6e399 --- /dev/null +++ b/brave/browser/net/tor_proxy_network_delegate.cc @@ -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(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 diff --git a/brave/browser/net/tor_proxy_network_delegate.h b/brave/browser/net/tor_proxy_network_delegate.h new file mode 100644 index 0000000000..66413a3c06 --- /dev/null +++ b/brave/browser/net/tor_proxy_network_delegate.h @@ -0,0 +1,47 @@ +// 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. + +#ifndef BRAVE_BROWSER_NET_TOR_PROXY_NETWORK_DELEGATE_H_ +#define BRAVE_BROWSER_NET_TOR_PROXY_NETWORK_DELEGATE_H_ + +#include "atom/browser/extensions/atom_extensions_network_delegate.h" +#include "brave/browser/brave_browser_context.h" + +namespace extensions { +class EventRouterForwarder; +class InfoMap; +} + +namespace brave { + +class TorProxyNetworkDelegate : + public extensions::AtomExtensionsNetworkDelegate { + public: + explicit TorProxyNetworkDelegate( + Profile* browser_context, + extensions::InfoMap* info_map, + extensions::EventRouterForwarder* event_router); + ~TorProxyNetworkDelegate() override; + + private: + // NetworkDelegate implementation. + 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; + + void ConfigTorProxyInteral(net::URLRequest* request); + + BraveBrowserContext* browser_context_; + + DISALLOW_COPY_AND_ASSIGN(TorProxyNetworkDelegate); +}; + +} // namespace brave + +#endif // BRAVE_BROWSER_NET_TOR_PROXY_NETWORK_DELEGATE_H_ diff --git a/patches/master_patch.patch b/patches/master_patch.patch index 6e9974751e..4eb884081e 100644 --- a/patches/master_patch.patch +++ b/patches/master_patch.patch @@ -2202,21 +2202,6 @@ index e23f2e6e9d9deb5fcd93f39c0e88c63ead47ac87..5c3e259474eec476038ee70bc90d6f0d // The time spent sending the CONNECT request to the SOCKS server. EVENT_TYPE(SOCKS5_HANDSHAKE_WRITE) -diff --git a/net/proxy_resolution/proxy_service.cc b/net/proxy_resolution/proxy_service.cc -index 6b633063ec2f279e24052a9a7af43921f4ebd8d1..adad0cd58889611318b6a4d7f42c203a95cd2a59 100644 ---- a/net/proxy_resolution/proxy_service.cc -+++ b/net/proxy_resolution/proxy_service.cc -@@ -1440,7 +1440,9 @@ void ProxyResolutionService::ResetConfigService( - - void ProxyResolutionService::ForceReloadProxyConfig() { - DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); -- ResetProxyConfig(false); -+ // MUON(darkdh): It should be force fetching config according to the API -+ // description -+ ResetProxyConfig(true); - ApplyProxyConfigIfAvailable(); - } - diff --git a/net/socket/socks5_client_socket.cc b/net/socket/socks5_client_socket.cc index 6b436878f780c0307f3a5508aa5909ad4ce7a23f..8f05c300becbe477c224d803934d60d46f158685 100644 --- a/net/socket/socks5_client_socket.cc