blob: 5501b1ed2e8ee25b9015aa61db749ce75600cf70 [file]
// 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 "net/proxy_resolution/configured_proxy_resolution_request.h"
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_event_type.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "net/proxy_resolution/proxy_info.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_set.h"
namespace net {
namespace {
bool CheckDnsCondition(
const ProxyConfig::ProxyOverrideRule::DnsProbeCondition& dns_condition,
const ResolveHostResult& dns_result) {
switch (dns_condition.result) {
case ProxyConfig::ProxyOverrideRule::DnsProbeCondition::Result::kNotFound:
return dns_result.is_address_list_empty;
case ProxyConfig::ProxyOverrideRule::DnsProbeCondition::Result::kResolved:
return !dns_result.is_address_list_empty;
}
}
base::DictValue CreateEndHostResolutionNetLogParams(
const url::SchemeHostPort& host,
const ResolveHostResult& result,
bool was_resolved_sync) {
base::DictValue dict;
dict.Set("host", host.Serialize());
dict.Set("was_resolved_sync", was_resolved_sync);
result.AddToDict(dict);
return dict;
}
} // namespace
ConfiguredProxyResolutionRequest::ConfiguredProxyResolutionRequest(
ConfiguredProxyResolutionService* service,
const GURL& url,
const std::string& method,
const NetworkAnonymizationKey& network_anonymization_key,
handles::NetworkHandle target_network,
ProxyInfo* results,
CompletionOnceCallback user_callback,
const NetLogWithSource& net_log,
RequestPriority priority)
: service_(service),
user_callback_(std::move(user_callback)),
results_(results),
url_(url),
method_(method),
network_anonymization_key_(network_anonymization_key),
target_network_(target_network),
net_log_(net_log),
priority_(priority),
creation_time_(base::TimeTicks::Now()) {
CHECK(!user_callback_.is_null());
}
ConfiguredProxyResolutionRequest::~ConfiguredProxyResolutionRequest() {
if (service_) {
service_->RemovePendingRequest(this);
CancelActiveJob();
// This should be emitted last, after any message `CancelActiveJob()` may
// trigger.
net_log_.EndEvent(NetLogEventType::PROXY_RESOLUTION_SERVICE);
}
}
// Starts the resolve proxy request.
int ConfiguredProxyResolutionRequest::Start() {
CHECK(!was_completed());
CHECK(!is_started());
CHECK(service_->config_);
traffic_annotation_ = MutableNetworkTrafficAnnotationTag(
service_->config_->traffic_annotation());
if (!service_->config_->value().proxy_override_rules().empty()) {
net_log_.BeginEvent(NetLogEventType::PROXY_RESOLUTION_OVERRIDE_RULES);
for (const auto& rule : service_->config_->value().proxy_override_rules()) {
if (rule.MatchesDestination(url_)) {
bool skip_rule = false;
for (const auto& dns_condition : rule.dns_conditions) {
// DNS resolution requests are keyed by the host and the network
// anonymization key, but not by listeners. This means that a given
// request may end up as listener multiple times for the same DNS host
// resolution requests (e.g. if the same host is in multiple rules).
// This is fine, as long as `OnDnsHostResolved` knows how to handle
// multiple calls for the same host.
auto result = service_->RequestHostResolution(
dns_condition, weak_factory_.GetWeakPtr(),
network_anonymization_key_, target_network_, net_log_, priority_);
if (result) {
net_log_.AddEvent(
NetLogEventType::PROXY_OVERRIDE_END_HOST_RESOLUTION, [&] {
return CreateEndHostResolutionNetLogParams(
dns_condition.host, result.value(),
/*was_resolved_sync=*/true);
});
if (!CheckDnsCondition(dns_condition, result.value())) {
// No need to trigger DNS resolutions for the remaining hosts
// in this rule, as it does not apply.
skip_rule = true;
break;
}
dns_results_.emplace(dns_condition.host, result.value());
}
}
if (!skip_rule) {
applicable_override_rules_.push(rule);
}
}
}
if (applicable_override_rules_.empty()) {
// No override rules applied.
net_log_.EndEvent(NetLogEventType::PROXY_RESOLUTION_OVERRIDE_RULES);
}
}
return ContinueProxyResolution();
}
void ConfiguredProxyResolutionRequest::ResumeOrCompleteSynchronously() {
int rv;
if (override_rules_evaluated_) {
rv = ContinueProxyResolution();
} else {
rv = service_->TryResolveSynchronously(
url_, /*bypass_override_rules=*/false, net_log_, results_);
if (rv == ERR_IO_PENDING) {
rv = Start();
}
}
if (rv != ERR_IO_PENDING) {
OnAsyncResolutionComplete(rv);
}
}
void ConfiguredProxyResolutionRequest::CancelActiveJob() {
net_log_.AddEvent(NetLogEventType::CANCELLED);
if (!applicable_override_rules_.empty()) {
net_log_.EndEvent(NetLogEventType::PROXY_RESOLUTION_OVERRIDE_RULES);
}
ResetActiveJobState();
CHECK(!is_started());
}
bool ConfiguredProxyResolutionRequest::CancelResolveJob() {
if (!resolve_job_) {
return false;
}
net_log_.AddEvent(NetLogEventType::CANCELLED);
resolve_job_.reset();
override_rules_evaluated_ = true;
CHECK(!is_started());
return true;
}
int ConfiguredProxyResolutionRequest::FinalizeRequestResult(int result_code) {
CHECK(!was_completed());
// Clear state so that `is_started()` returns false while
// ProcessResolutionResult() runs.
if (is_started()) {
ResetActiveJobState();
}
// Detach `service_` and `results_` before `ProcessResolutionResult()` in case
// a sibling callback synchronously destroys `this` or `results` (avoids
// double-removal in our destructor and dangling raw_ptrs).
ConfiguredProxyResolutionService* service = std::exchange(service_, nullptr);
ProxyInfo* results = std::exchange(results_, nullptr);
base::WeakPtr<ConfiguredProxyResolutionRequest> weak_this =
weak_factory_.GetWeakPtr();
// Note that ProcessResolutionResult might modify `results`.
int rv =
service->ProcessResolutionResult(url_, network_anonymization_key_,
method_, results, result_code, net_log_);
if (!weak_this) {
return ERR_ABORTED;
}
// Make a note in the results which configuration was in use at the
// time of the resolve.
results->set_proxy_resolve_start_time(creation_time_);
results->set_proxy_resolve_end_time(base::TimeTicks::Now());
// If annotation is not already set, e.g. through TryResolveSynchronously
// function, use in-progress-resolve annotation.
if (!results->traffic_annotation().is_valid()) {
results->set_traffic_annotation(traffic_annotation_);
}
// If proxy is set without error, ensure that an annotation is provided.
if (result_code != ERR_ABORTED && !rv) {
// TODO(crbug.com/469006851): Turn this back into a CHECK once crashes were
// fully fixed.
DCHECK(results->traffic_annotation().is_valid());
}
// Reset the state associated with in-progress-resolve.
traffic_annotation_.reset();
return rv;
}
void ConfiguredProxyResolutionRequest::BeginWaitEvent(
NetLogEventType event_type) {
if (event_type ==
NetLogEventType::
PROXY_RESOLUTION_SERVICE_WAITING_FOR_DYNAMIC_PROXY_CONFIGS &&
service_) {
service_->OnRequestPausedForDynamicRouting(this);
}
if (current_wait_event_ == event_type) {
return;
}
EndWaitEvent();
current_wait_event_ = event_type;
net_log_.BeginEvent(event_type);
}
void ConfiguredProxyResolutionRequest::EndWaitEvent() {
if (!current_wait_event_) {
return;
}
net_log_.EndEvent(*current_wait_event_);
current_wait_event_.reset();
}
LoadState ConfiguredProxyResolutionRequest::GetLoadState() const {
LoadState load_state = LOAD_STATE_IDLE;
if (service_ && service_->GetLoadStateIfAvailable(&load_state))
return load_state;
if (is_started())
return resolve_job_->GetLoadState();
return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
}
void ConfiguredProxyResolutionRequest::OnAsyncResolutionComplete(
int result_code) {
CHECK(!was_completed());
// Remove `this` from the service before notifying it of completion to
// prevent re-entrant dispatch if the service re-initializes synchronously.
service_->RemovePendingRequest(this);
base::WeakPtr<ConfiguredProxyResolutionRequest> weak_this =
weak_factory_.GetWeakPtr();
result_code = FinalizeRequestResult(result_code);
if (!weak_this) {
return;
}
CompletionOnceCallback callback = std::move(user_callback_);
user_callback_.Reset();
std::move(callback).Run(result_code);
}
int ConfiguredProxyResolutionRequest::ContinueProxyResolution() {
if (!applicable_override_rules_.empty()) {
int rv = EvaluateApplicableOverrideRules();
if (rv == ERR_IO_PENDING || !results_->is_empty()) {
return rv;
}
}
if (!service_->IsReady()) {
// A dynamic routing config update started while override rule DNS probes
// were in flight. Pause here (with `is_started() == false`) so the service
// resumes this request once ready.
CHECK(service_->IsPacReady());
BeginWaitEvent(
NetLogEventType::
PROXY_RESOLUTION_SERVICE_WAITING_FOR_DYNAMIC_PROXY_CONFIGS);
return ERR_IO_PENDING;
}
// Required in case some override rules needed to run asynchronously, but
// ended up not applying, and there are e.g. dynamic routing rules or manual
// proxy settings as fallback.
int rv = service_->TryResolveSynchronously(
url_, /*bypass_override_rules=*/true, net_log_, results_);
if (rv != ERR_IO_PENDING) {
return rv;
}
if (!service_->ShouldUseSystemProxyResolver(service_->config_->value()) &&
service_->ApplyPacBypassRules(url_, results_)) {
return OK;
}
return service_->GetProxyResolver()->GetProxyForURL(
url_, network_anonymization_key_, results_,
base::BindOnce(
&ConfiguredProxyResolutionRequest::OnAsyncResolutionComplete,
base::Unretained(this)),
&resolve_job_, net_log_);
}
void ConfiguredProxyResolutionRequest::OnDnsHostResolved(
const url::SchemeHostPort& host,
const ResolveHostResult& result) {
if (applicable_override_rules_.empty()) {
// DNS resolution is only required for override rules. Ignore these function
// calls if the current request is not resolving override rules.
return;
}
// Using `try_emplace` as this line may be hit multiple times for the same
// host (see comment in `Start`).
auto [unused, inserted] = dns_results_.try_emplace(host, result);
// Entry already existed, likely with the same value as before. Same value or
// not, we keep the old result, so there's nothing else to do.
if (!inserted) {
return;
}
net_log_.AddEvent(NetLogEventType::PROXY_OVERRIDE_END_HOST_RESOLUTION, [&] {
return CreateEndHostResolutionNetLogParams(host, result,
/*was_resolved_sync=*/false);
});
if (!service_) {
return;
}
int rv = ContinueProxyResolution();
if (rv == ERR_IO_PENDING) {
// Missing a required DNS resolution, or waiting on service readiness / PAC.
return;
}
OnAsyncResolutionComplete(rv);
}
int ConfiguredProxyResolutionRequest::EvaluateApplicableOverrideRules() {
while (!applicable_override_rules_.empty()) {
const auto& applicable_rule = applicable_override_rules_.front();
// Not having any conditions means they are all satisfied.
bool conditions_satisfied = true;
for (const auto& dns_condition : applicable_rule.dns_conditions) {
// If no results are found in the DNS results map, it means that the DNS
// resolution request owned by the service is still pending.
auto dns_result_it = dns_results_.find(dns_condition.host);
if (dns_result_it == dns_results_.end()) {
return ERR_IO_PENDING;
}
conditions_satisfied =
conditions_satisfied &&
CheckDnsCondition(dns_condition, dns_result_it->second);
}
if (conditions_satisfied) {
net_log_.AddEvent(NetLogEventType::PROXY_RESOLUTION_OVERRIDE_RULE_APPLIED,
[&] { return applicable_rule.ToDict(); });
results_->UseProxyList(applicable_rule.proxy_list);
break;
}
// Conditions were evaluated successfully, but are not satisfied. This
// current rule is therefore no longer applicable.
applicable_override_rules_.pop();
}
override_rules_evaluated_ = results_->is_empty();
dns_results_.clear();
net_log_.EndEvent(NetLogEventType::PROXY_RESOLUTION_OVERRIDE_RULES);
return OK;
}
void ConfiguredProxyResolutionRequest::ResetActiveJobState() {
// The request may already be running in the resolver.
resolve_job_.reset();
applicable_override_rules_ = base::queue<ProxyConfig::ProxyOverrideRule>();
override_rules_evaluated_ = false;
dns_results_.clear();
}
} // namespace net