Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetworkService migration for Tor #2647

Merged
merged 32 commits into from
Jul 10, 2019
Merged

NetworkService migration for Tor #2647

merged 32 commits into from
Jul 10, 2019

Conversation

darkdh
Copy link
Member

@darkdh darkdh commented Jun 10, 2019

brave-browser: a0b6c4d (75.0.3770.90)

fix brave/brave-browser#4312

When NetworkService is enabled, we only have access to profile-wise
proxy config. So we discard proxy config per url request path to have a
global proxy config for tor profile and insert username/password when it
is about to resolve proxy in net::ProxyResolutionService.

Note

  1. new flow works no matter NetworkService enabled or not.
  2. ProxyConfigServiceTor has to be NET exported to avoid //net circular
    dependency.
  3. ProxyConfigMonitor chromium overrides is for setting profile-wise
    proxy config without setting proxy_config::prefs::kProxy which will be
    interfered by other components(ex. DataReductionProxyService)

Submitter Checklist:

Test Plan:

Launch Brave with/without --enable-features=NetworkService and test following scenario separately

  1. Go to https://check.torproject.org/ in non tor window, make sure it doesn't connect to tor network
  2. Go to https://check.torproject.org/ in tor window, remember the ip address
  3. Click New Tor Identity, it will trigger a auto refresh and ip should be different than step 2

Reviewer Checklist:

  • New files have MPL-2.0 license header.
  • Request a security/privacy review as needed.
  • Adequate test coverage exists to prevent regressions
  • Verify test plan is specified in PR before merging to source

After-merge Checklist:

  • The associated issue milestone is set to the smallest version that the
    changes has landed on.
  • All relevant documentation has been updated.

@darkdh darkdh marked this pull request as ready for review June 11, 2019 22:35
@darkdh darkdh self-assigned this Jun 11, 2019
@bsclifton bsclifton requested a review from iefremov June 12, 2019 16:11
const int kTorPasswordLength = 16;
// Default tor circuit life time is 10 minutes
constexpr base::TimeDelta kTenMins = base::TimeDelta::FromMinutes(10);
constexpr base::TimeDelta kTenMins = base::TimeDelta::FromMinutes(1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look like ten minutes :)

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, I was doing some testing and forgot to change back

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/url_request/url_request_context.h"
#include "net/proxy_resolution/proxy_config_with_annotation.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

wrong sorting (just apply clang-format)

Copy link
Member Author

@darkdh darkdh Jun 17, 2019

Choose a reason for hiding this comment

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

fixed in 4985fce

proxy_url = std::string(scheme_ + "://" + host_ + ":" + port_);
}
std::string proxy_url =
std::string(scheme_ + "://" + host_ + ":" + port_);
Copy link
Contributor

Choose a reason for hiding this comment

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

whitespace could be fixed by clang-format

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use const std::string and generally const as much as possible

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 4985fce

TorProxyConfigService::TorProxyConfigService(
const std::string& tor_proxy, const std::string& username,
TorProxyMap* tor_proxy_map) {
ProxyConfigServiceTor::ProxyConfigServiceTor(const std::string& tor_proxy) {
Copy link
Contributor

@iefremov iefremov Jun 17, 2019

Choose a reason for hiding this comment

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

why do we receive a string here instead of getting net::ProxyServer ? We could avoid all that parsing and use well-formed data structures

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 51b3f3c

} // namespace

namespace brave {

void NewTorIdentityCallback(WebContents* current_tab, bool success) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why success is unused?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 51b3f3c

case version_info::Channel::DEV:
return std::string("socks5://127.0.0.1:9370");
case version_info::Channel::CANARY:
return std::string("socks5://127.0.0.1:9380");
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't it possible to return GURL?

Copy link
Member Author

Choose a reason for hiding this comment

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

it is an uri not url, see ProxyServer::FromURI and ProxyServer::ToURI.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yea, socks5 is not a valid scheme for GURL afaik

return std::make_unique<net::ProxyConfigServiceTor>(
tor::GetTorProxyString());
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

#endif // BUILDFLAG ...

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 51b3f3c

GURL url = request_url.ReplaceComponents(replacements);
tor_circuit_callback_ = std::move(callback);

DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Copy link
Contributor

Choose a reason for hiding this comment

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

typically such checks are placed in the very beginning of a method

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 51b3f3c

base::Unretained(this), net::ERR_ABORTED, base::nullopt));
// Force lookup to erase the old circuit
storage_partition->GetNetworkContext()->
LookUpProxyForURL(url, std::move(proxy_lookup_client_ptr));
Copy link
Contributor

Choose a reason for hiding this comment

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

whitespace

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 4985fce

@iefremov
Copy link
Contributor

generally looks good, but I will take more time to understand details.

if (current_state_ == STATE_NONE)
ApplyProxyConfigIfAvailable();

+ if (IsTorProxy(config_->value()) && raw_url.ref() == "NewTorCircuit")
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO we should hide this under ENABLE_TOR, though not sure if it's possible

Copy link
Member Author

Choose a reason for hiding this comment

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

We try to make //net not to rely on any brave stuff to avoid circular dependency.

Copy link
Collaborator

Choose a reason for hiding this comment

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

please update patches similar to discussions about sync patches

Copy link
Collaborator

Choose a reason for hiding this comment

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

actually I'd really like to find some way to not have tor-specific stuff in ProxyResolutionService, in particular the tor_proxy_map_. Can we create a class a singleton class in Brave that keeps a map of ProxyResolutionService instance -> tor_proxy_map and use that inside a chromium_src injected method/macro?

};

TEST_F(ProxyResolutionServiceTest, TorProxy) {
MockAsyncProxyResolverFactory* factory =
Copy link
Contributor

Choose a reason for hiding this comment

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

nits: make_unique here and std::move below
const everywhere for local variables that do not change (proxy_uri, site_url, etc)

Copy link
Member Author

Choose a reason for hiding this comment

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

MockAsyncProxyResolverFactory will be used later

Copy link
Member Author

@darkdh darkdh Jun 18, 2019

Choose a reason for hiding this comment

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

we don't actually care about factory->pending_requests().empty() so I will wrap it when constructing ProxyResolutionService

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

MockAsyncProxyResolverFactory* factory =
new MockAsyncProxyResolverFactory(false);
std::string proxy_uri("socks5://127.0.0.1:5566");
ProxyResolutionService service(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can avoid copypaste by initializing this stuff in the test class

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

ProxyConfigServiceTor tor_proxy_config_service(proxy_uri);
ProxyConfigWithAnnotation fetched_config;
tor_proxy_config_service.GetLatestProxyConfig(&fetched_config);
tor_proxy_config_service.SetUsername(
Copy link
Contributor

Choose a reason for hiding this comment

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

Some comments describing the key point would be highly appreciated

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

bool success = proxy_info.has_value() && !proxy_info->is_direct();
if (tor_circuit_callback_) {
std::move(tor_circuit_callback_).Run(success);
binding_.Close();
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we close the binding in any case?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

&tor_proxy_map_,
true);
void TorProfileServiceImpl::OnSetNewTorCircuitComplete(bool success) {
if (tor_circuit_callback_)
Copy link
Contributor

Choose a reason for hiding this comment

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

in what case this is empty? tests? Probably worth adding a comment

Copy link
Member Author

Choose a reason for hiding this comment

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

this function is not used, removed in 58622cb

std::string GetTorProxyURI() {
switch (chrome::GetChannel()) {
case version_info::Channel::STABLE:
return std::string("socks5://127.0.0.1:9350");
Copy link
Contributor

Choose a reason for hiding this comment

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

why not introduce a named constant for 127.0.0.1 and maybe ports

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

EXPECT_EQ(host_port.port(), 5566);
EXPECT_EQ(host_port.username(), isolation_key);

// persistent circuit isolation until timeout
Copy link
Contributor

Choose a reason for hiding this comment

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

small nit for all comments throughout the PR: normally the should look like a sentence, i.e. start with a Capital Letter and end with a dot

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 58622cb

@iefremov
Copy link
Contributor

LGTM with nits. My vote for merging this ASAP :) @bridiver @bsclifton @riastradh-brave WDYT?

@@ -12,13 +12,15 @@
#include "base/files/scoped_temp_dir.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/browser/profiles/tor_unittest_profile_manager.h"
#include "brave/browser/tor/tor_launcher_factory.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should probably keep all the tor stuff here inside ENABLE_TOR since we don't currently have an implementation for android

Copy link
Collaborator

Choose a reason for hiding this comment

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

the same also applies for the tor-specific test files in gn

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 03268dc482e62d0ff2c0573fb193528ca1508a28

@@ -33,3 +35,6 @@ std::unique_ptr<net::ProxyConfigService> CreateProxyConfigServiceTor(
#endif

#include "../../../../../chrome/browser/net/proxy_config_monitor.cc" // NOLINT

// Required by lint
#include "brave/chromium_src/chrome/browser/net/proxy_config_monitor.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should go at the top, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

can we actually disable this lint check for the chromium_src dir in chromium_src/CPPLINT.cfg?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 3bc4178

@@ -18,6 +16,11 @@
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"

#if BUILDFLAG(ENABLE_TOR)
Copy link
Collaborator

Choose a reason for hiding this comment

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

shouldn't the entire TorProfileService be left out of gn if tor is not enabled?

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 9bb8d68

@@ -123,10 +124,12 @@ TorProfileServiceImpl::TorProfileServiceImpl(Profile* profile)
tor_launcher_factory_ = TorLauncherFactory::GetInstance();
tor_launcher_factory_->AddObserver(this);

#if BUILDFLAG(ENABLE_TOR)
Copy link
Collaborator

Choose a reason for hiding this comment

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

TorProfileServiceImpl should definitely not be built at all if tor is not enabled in gn

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 9bb8d68

@darkdh darkdh force-pushed the network_service_tor branch 2 times, most recently from d6fb3aa to 48d0487 Compare July 9, 2019 16:49
@darkdh darkdh merged commit 8b6960b into master Jul 10, 2019
@darkdh darkdh deleted the network_service_tor branch July 10, 2019 00:54
@mkarolin
Copy link
Collaborator

@darkdh this is marked as 0.68.x milestone, but I only see it in master (0.69.x). Are we planning to uplift to 0.68.x and 0.67.x? c76 bump is going to those branches and would need these changes to be there as well, I think.

@darkdh
Copy link
Member Author

darkdh commented Jul 15, 2019

I must marked the milestone too early and train migration happened (it was meant to be nightly).
C76 bump depends on this so it should be wherever C76 bump uplifted to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Tor integration compatible with NetworkService
5 participants