Skip to content

Commit

Permalink
Treat decentralized DNS servers as available before first probe is co…
Browse files Browse the repository at this point in the history
…mpleted
  • Loading branch information
yrliou committed Mar 10, 2021
1 parent a9c6cdb commit b20ad62
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
11 changes: 11 additions & 0 deletions chromium_src/net/dns/host_resolver.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/net/dns/brave_resolve_context.h"
#include "net/dns/context_host_resolver.h"

#define ResolveContext BraveResolveContext
#include "../../../../net/dns/host_resolver.cc"
#undef ResolveContext
24 changes: 24 additions & 0 deletions chromium_src/net/dns/resolve_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_NET_DNS_RESOLVE_CONTEXT_H_
#define BRAVE_CHROMIUM_SRC_NET_DNS_RESOLVE_CONTEXT_H_

namespace net {
class BraveResolveContext;
} // namespace net

#define GetDohServerAvailability virtual GetDohServerAvailability
#define NumAvailableDohServers virtual NumAvailableDohServers
#define BRAVE_RESOLVE_CONTEXT_H \
private: \
friend class BraveResolveContext;

#include "../../../../net/dns/resolve_context.h"
#undef GetDohServerAvailability
#undef NumAvailableDohServers
#undef BRAVE_RESOLVE_CONTEXT_H

#endif // BRAVE_CHROMIUM_SRC_NET_DNS_RESOLVE_CONTEXT_H_
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "components/prefs/pref_service.h"
#include "components/security_interstitials/content/settings_page_helper.h"
#include "components/security_interstitials/core/metrics_helper.h"
#include "content/public/browser/web_contents.h"

namespace decentralized_dns {

Expand Down Expand Up @@ -57,7 +58,8 @@ void DecentralizedDnsInterstitialControllerClient::SetResolveMethodAndReload(
? kUnstoppableDomainsResolveMethod
: kENSResolveMethod;
local_state_->SetInteger(pref_name, static_cast<int>(type));
Reload();
web_contents_->GetController().Reload(content::ReloadType::BYPASSING_CACHE,
true);
}

} // namespace decentralized_dns
68 changes: 68 additions & 0 deletions net/dns/brave_resolve_context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/net/dns/brave_resolve_context.h"

#include <string>

#include "brave/net/decentralized_dns/constants.h"
#include "net/dns/dns_session.h"

namespace {

bool IsDecentralizedDNSResolver(const std::string& server) {
return server == decentralized_dns::kUnstoppableDomainsDoHResolver ||
server == decentralized_dns::kENSDoHResolver;
}

} // namespace

namespace net {

BraveResolveContext::BraveResolveContext(URLRequestContext* url_request_context,
bool enable_caching)
: ResolveContext(url_request_context, enable_caching) {}

BraveResolveContext::~BraveResolveContext() = default;

bool BraveResolveContext::IsFirstProbeCompleted(const ServerStats& stat) const {
return !(stat.last_failure_count == 0 &&
stat.current_connection_success == false);
}

bool BraveResolveContext::GetDohServerAvailability(
size_t doh_server_index,
const DnsSession* session) const {
// Return decentralized DNS resolvers as available before the first probe is
// completed. It is to avoid falling back to non-secure DNS servers before
// the first probe is completed when users using automatic mode, which will
// lead to an error page with HOSTNAME_NOT_RESOLVED error right after user
// opt-in from the interstitial page.
if (IsDecentralizedDNSResolver(session->config()
.dns_over_https_servers[doh_server_index]
.server_template) &&
!IsFirstProbeCompleted(doh_server_stats_[doh_server_index]))
return true;

return ResolveContext::GetDohServerAvailability(doh_server_index, session);
}

size_t BraveResolveContext::NumAvailableDohServers(
const DnsSession* session) const {
size_t num = 0;

// Treat decentralized DNS resolvers as available before the first probe is
// completed.
for (size_t i = 0; i < doh_server_stats_.size(); i++) {
if (IsDecentralizedDNSResolver(
session->config().dns_over_https_servers[i].server_template) &&
!IsFirstProbeCompleted(doh_server_stats_[i]))
num++;
}

return num + ResolveContext::NumAvailableDohServers(session);
}

} // namespace net
37 changes: 37 additions & 0 deletions net/dns/brave_resolve_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_NET_DNS_BRAVE_RESOLVE_CONTEXT_H_
#define BRAVE_NET_DNS_BRAVE_RESOLVE_CONTEXT_H_

#include "net/base/net_export.h"
#include "net/dns/resolve_context.h"

namespace net {

class DnsSession;
class URLRequestContext;

class NET_EXPORT_PRIVATE BraveResolveContext : public ResolveContext {
public:
BraveResolveContext(URLRequestContext* url_request_context,
bool enable_caching);

BraveResolveContext(const BraveResolveContext&) = delete;
BraveResolveContext& operator=(const BraveResolveContext&) = delete;

~BraveResolveContext() override;

bool GetDohServerAvailability(size_t doh_server_index,
const DnsSession* session) const override;
size_t NumAvailableDohServers(const DnsSession* session) const override;

private:
bool IsFirstProbeCompleted(const ServerStats& stat) const;
};

} // namespace net

#endif // BRAVE_NET_DNS_BRAVE_RESOLVE_CONTEXT_H_
2 changes: 2 additions & 0 deletions net/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import("//brave/components/decentralized_dns/buildflags/buildflags.gni")

brave_net_sources = [
"//brave/net/decentralized_dns/constants.h",
"//brave/net/dns/brave_resolve_context.cc",
"//brave/net/dns/brave_resolve_context.h",
"//brave/net/proxy_resolution/proxy_config_service_tor.cc",
"//brave/net/proxy_resolution/proxy_config_service_tor.h",
]
12 changes: 12 additions & 0 deletions patches/net-dns-resolve_context.h.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/net/dns/resolve_context.h b/net/dns/resolve_context.h
index fc43188340e54dd509aa58f93a84ca6b770e5a6f..81fa236a973ae31f6ac165b14ee0cc7754253f03 100644
--- a/net/dns/resolve_context.h
+++ b/net/dns/resolve_context.h
@@ -176,6 +176,7 @@ class NET_EXPORT_PRIVATE ResolveContext : public base::CheckedObserver {
// (alternative service info if it supports QUIC, for instance).
const IsolationInfo& isolation_info() const { return isolation_info_; }

+ BRAVE_RESOLVE_CONTEXT_H
private:
friend DohDnsServerIterator;
friend ClassicDnsServerIterator;

0 comments on commit b20ad62

Please sign in to comment.