1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/net/network_diagnostics/http_firewall_routine.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/mojom/network_context.mojom.h"
namespace ash {
namespace network_diagnostics {
namespace {
namespace mojom = ::chromeos::network_diagnostics::mojom;
// Http port number.
constexpr int kHttpPort = 80;
// Http scheme.
constexpr char kHttpScheme[] = "http://";
// Effetively, the number of random hosts to query.
// total hosts queried by this routine = random hosts + fixed hosts, where
// information about fixed hosts is found in network_diagnostics_util.cc.
constexpr int kTotalAdditionalHostsToQuery = 3;
// The length of a random eight letter prefix obtained by the characters from
// |kPossibleChars|.
constexpr int kHostPrefixLength = 8;
// The threshold describing the number of DNS resolution failures permitted.
// E.g. If 10 host resolution attempts are made, any more than two DNS failures
// would result in a problem.
constexpr double kDnsResolutionFailureRateThreshold = 0.2;
// The threshold describing number of TLS probe failures permitted. E.g. If 10
// TLS probes are attempted, any more than two failures would result in a
// problem. This number does not take into account the number of TLS probes that
// failed due to unsuccessful DNS resolution.
constexpr double kTlsProbeFailureRateThreshold = 0.2;
// For an explanation of error codes, see "net/base/net_error_list.h".
constexpr int kRetryResponseCodes[] = {net::ERR_TIMED_OUT,
net::ERR_DNS_TIMED_OUT};
// Returns the network context.
network::mojom::NetworkContext* GetNetworkContext() {
Profile* profile = util::GetUserProfile();
DCHECK(profile);
return profile->GetDefaultStoragePartition()->GetNetworkContext();
}
class HttpFirewallDelegate : public HttpFirewallRoutine::Delegate {
public:
// Delegate:
std::unique_ptr<TlsProber> CreateAndExecuteTlsProber(
network::NetworkContextGetter network_context_getter,
net::HostPortPair host_port_pair,
bool negotiate_tls,
TlsProber::TlsProbeCompleteCallback callback) override {
return std::make_unique<TlsProber>(std::move(network_context_getter),
host_port_pair, negotiate_tls,
std::move(callback));
}
};
} // namespace
const int HttpFirewallRoutine::kTotalNumRetries;
HttpFirewallRoutine::HttpFirewallRoutine(mojom::RoutineCallSource source)
: NetworkDiagnosticsRoutine(source),
delegate_(std::make_unique<HttpFirewallDelegate>()),
num_retries_(kTotalNumRetries) {
std::vector<std::string> url_strings =
util::GetRandomAndFixedHostsWithSchemeAndPort(
kTotalAdditionalHostsToQuery, kHostPrefixLength, kHttpScheme,
kHttpPort);
for (const auto& url_string : url_strings) {
urls_to_query_.push_back(GURL(url_string));
}
num_urls_to_query_ = urls_to_query_.size();
}
HttpFirewallRoutine::~HttpFirewallRoutine() = default;
mojom::RoutineType HttpFirewallRoutine::Type() {
return mojom::RoutineType::kHttpFirewall;
}
void HttpFirewallRoutine::Run() {
ProbeNextUrl();
}
void HttpFirewallRoutine::AnalyzeResultsAndExecuteCallback() {
// There should at least `kTotalAdditionalHostsToQuery` (=3) URLs to query.
DCHECK(num_urls_to_query_);
double dns_resolution_failure_rate =
static_cast<double>(dns_resolution_failures_) /
static_cast<double>(num_urls_to_query_);
if (dns_resolution_failure_rate > kDnsResolutionFailureRateThreshold) {
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(
mojom::HttpFirewallProblem::kDnsResolutionFailuresAboveThreshold);
} else {
// When `dns_resolution_failure_rate` is below the threshold, there must be
// probes that is not "DNS failure".
DCHECK(num_no_dns_failure_tls_probes_attempted_);
double tls_probe_failure_rate =
static_cast<double>(tls_probe_failures_) /
static_cast<double>(num_no_dns_failure_tls_probes_attempted_);
if (tls_probe_failure_rate <= kTlsProbeFailureRateThreshold) {
set_verdict(mojom::RoutineVerdict::kNoProblem);
} else if (tls_probe_failures_ ==
num_no_dns_failure_tls_probes_attempted_) {
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(mojom::HttpFirewallProblem::kFirewallDetected);
} else {
// It cannot be conclusively determined whether a firewall exists;
// however, since reaching this case means tls_probe_failure_rate >
// kTlsProbeFailureRateThreshold, a firewall could potentially
// exist.
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(mojom::HttpFirewallProblem::kPotentialFirewall);
}
}
set_problems(mojom::RoutineProblems::NewHttpFirewallProblems(problems_));
ExecuteCallback();
}
void HttpFirewallRoutine::ProbeNextUrl() {
DCHECK(!urls_to_query_.empty());
auto url = urls_to_query_.back();
urls_to_query_.pop_back();
AttemptProbe(url);
}
void HttpFirewallRoutine::AttemptProbe(const GURL& url) {
// Store the instance of TlsProber.
tls_prober_ = delegate_->CreateAndExecuteTlsProber(
base::BindRepeating(&GetNetworkContext), net::HostPortPair::FromURL(url),
/*negotiate_tls=*/false,
base::BindOnce(&HttpFirewallRoutine::OnProbeComplete, weak_ptr(), url));
}
void HttpFirewallRoutine::OnProbeComplete(
const GURL& url,
int result,
TlsProber::ProbeExitEnum probe_exit_enum) {
if (probe_exit_enum == TlsProber::ProbeExitEnum::kDnsFailure) {
dns_resolution_failures_++;
} else {
const auto* iter = std::ranges::find(kRetryResponseCodes, result);
if (iter != std::end(kRetryResponseCodes) && num_retries_ > 0) {
num_retries_--;
AttemptProbe(url);
return;
}
if (result < 0) {
tls_probe_failures_++;
}
num_no_dns_failure_tls_probes_attempted_++;
}
if (urls_to_query_.empty()) {
AnalyzeResultsAndExecuteCallback();
return;
}
ProbeNextUrl();
}
} // namespace network_diagnostics
} // namespace ash
|