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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/proxy_resolving_client_socket.h"
#include <stdint.h>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_address.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/privacy_mode.h"
#include "net/base/proxy_delegate.h"
#include "net/dns/public/secure_dns_policy.h"
#include "net/http/http_auth_controller.h"
#include "net/http/http_network_session.h"
#include "net/http/http_request_headers.h"
#include "net/http/proxy_client_socket.h"
#include "net/http/proxy_fallback.h"
#include "net/log/net_log_source_type.h"
#include "net/proxy_resolution/configured_proxy_resolution_service.h"
#include "net/proxy_resolution/proxy_resolution_request.h"
#include "net/socket/connect_job_factory.h"
#include "net/socket/socket_tag.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
namespace network {
ProxyResolvingClientSocket::ProxyResolvingClientSocket(
net::HttpNetworkSession* network_session,
const net::CommonConnectJobParams* common_connect_job_params,
const GURL& url,
const net::NetworkAnonymizationKey& network_anonymization_key,
bool use_tls,
const net::ConnectJobFactory* connect_job_factory)
: network_session_(network_session),
common_connect_job_params_(common_connect_job_params),
connect_job_factory_(connect_job_factory),
url_(url),
network_anonymization_key_(network_anonymization_key),
use_tls_(use_tls),
net_log_(net::NetLogWithSource::Make(network_session_->net_log(),
net::NetLogSourceType::SOCKET)),
next_state_(STATE_NONE) {
// TODO(xunjieli): Handle invalid URLs more gracefully (at mojo API layer
// or when the request is created).
DCHECK(url_.is_valid());
DCHECK(connect_job_factory_);
}
ProxyResolvingClientSocket::~ProxyResolvingClientSocket() {}
int ProxyResolvingClientSocket::Read(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
if (socket_)
return socket_->Read(buf, buf_len, std::move(callback));
return net::ERR_SOCKET_NOT_CONNECTED;
}
int ProxyResolvingClientSocket::ReadIfReady(
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) {
if (socket_)
return socket_->ReadIfReady(buf, buf_len, std::move(callback));
return net::ERR_SOCKET_NOT_CONNECTED;
}
int ProxyResolvingClientSocket::CancelReadIfReady() {
if (socket_)
return socket_->CancelReadIfReady();
// Return net::OK as ReadIfReady() is canceled when socket is disconnected.
return net::OK;
}
int ProxyResolvingClientSocket::Write(
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation) {
if (socket_) {
return socket_->Write(buf, buf_len, std::move(callback),
traffic_annotation);
}
return net::ERR_SOCKET_NOT_CONNECTED;
}
int ProxyResolvingClientSocket::SetReceiveBufferSize(int32_t size) {
if (socket_)
return socket_->SetReceiveBufferSize(size);
return net::ERR_SOCKET_NOT_CONNECTED;
}
int ProxyResolvingClientSocket::SetSendBufferSize(int32_t size) {
if (socket_)
return socket_->SetSendBufferSize(size);
return net::ERR_SOCKET_NOT_CONNECTED;
}
int ProxyResolvingClientSocket::Connect(net::CompletionOnceCallback callback) {
DCHECK(user_connect_callback_.is_null());
DCHECK(!socket_);
next_state_ = STATE_PROXY_RESOLVE;
int result = DoLoop(net::OK);
if (result == net::ERR_IO_PENDING) {
user_connect_callback_ = std::move(callback);
}
return result;
}
void ProxyResolvingClientSocket::Disconnect() {
connect_job_.reset();
socket_.reset();
if (proxy_resolve_request_)
proxy_resolve_request_.reset();
user_connect_callback_.Reset();
}
bool ProxyResolvingClientSocket::IsConnected() const {
if (!socket_)
return false;
return socket_->IsConnected();
}
bool ProxyResolvingClientSocket::IsConnectedAndIdle() const {
if (!socket_)
return false;
return socket_->IsConnectedAndIdle();
}
int ProxyResolvingClientSocket::GetPeerAddress(net::IPEndPoint* address) const {
if (!socket_)
return net::ERR_SOCKET_NOT_CONNECTED;
if (proxy_info_.is_direct())
return socket_->GetPeerAddress(address);
net::IPAddress ip_address;
if (!ip_address.AssignFromIPLiteral(url_.HostNoBrackets())) {
// Do not expose the proxy IP address to the caller.
return net::ERR_NAME_NOT_RESOLVED;
}
*address = net::IPEndPoint(ip_address, url_.EffectiveIntPort());
return net::OK;
}
int ProxyResolvingClientSocket::GetLocalAddress(
net::IPEndPoint* address) const {
if (socket_)
return socket_->GetLocalAddress(address);
return net::ERR_SOCKET_NOT_CONNECTED;
}
const net::NetLogWithSource& ProxyResolvingClientSocket::NetLog() const {
if (socket_)
return socket_->NetLog();
return net_log_;
}
bool ProxyResolvingClientSocket::WasEverUsed() const {
if (socket_)
return socket_->WasEverUsed();
return false;
}
net::NextProto ProxyResolvingClientSocket::GetNegotiatedProtocol() const {
if (socket_)
return socket_->GetNegotiatedProtocol();
return net::NextProto::kProtoUnknown;
}
bool ProxyResolvingClientSocket::GetSSLInfo(net::SSLInfo* ssl_info) {
if (socket_)
return socket_->GetSSLInfo(ssl_info);
return false;
}
int64_t ProxyResolvingClientSocket::GetTotalReceivedBytes() const {
NOTIMPLEMENTED();
return 0;
}
void ProxyResolvingClientSocket::ApplySocketTag(const net::SocketTag& tag) {
NOTIMPLEMENTED();
}
void ProxyResolvingClientSocket::OnIOComplete(int result) {
DCHECK_NE(net::ERR_IO_PENDING, result);
int net_error = DoLoop(result);
if (net_error != net::ERR_IO_PENDING)
std::move(user_connect_callback_).Run(net_error);
}
int ProxyResolvingClientSocket::DoLoop(int result) {
DCHECK_NE(next_state_, STATE_NONE);
int rv = result;
do {
State state = next_state_;
next_state_ = STATE_NONE;
switch (state) {
case STATE_PROXY_RESOLVE:
DCHECK_EQ(net::OK, rv);
rv = DoProxyResolve();
break;
case STATE_PROXY_RESOLVE_COMPLETE:
rv = DoProxyResolveComplete(rv);
break;
case STATE_INIT_CONNECTION:
DCHECK_EQ(net::OK, rv);
rv = DoInitConnection();
break;
case STATE_INIT_CONNECTION_COMPLETE:
rv = DoInitConnectionComplete(rv);
break;
default:
NOTREACHED() << "bad state";
}
} while (rv != net::ERR_IO_PENDING && next_state_ != STATE_NONE);
return rv;
}
int ProxyResolvingClientSocket::DoProxyResolve() {
next_state_ = STATE_PROXY_RESOLVE_COMPLETE;
// base::Unretained(this) is safe because resolution request is canceled when
// |proxy_resolve_request_| is destroyed.
return network_session_->proxy_resolution_service()->ResolveProxy(
url_, net::HttpRequestHeaders::kPostMethod, network_anonymization_key_,
&proxy_info_,
base::BindOnce(&ProxyResolvingClientSocket::OnIOComplete,
base::Unretained(this)),
&proxy_resolve_request_, net_log_, net::DEFAULT_PRIORITY);
}
int ProxyResolvingClientSocket::DoProxyResolveComplete(int result) {
proxy_resolve_request_ = nullptr;
if (result == net::OK) {
// Removes unsupported proxies from the list. Currently, this removes
// just the SCHEME_QUIC proxy.
// TODO(crbug.com/41409577): Allow QUIC proxy once
// net::QuicProxyClientSocket supports ReadIfReady() and
// CancelReadIfReady().
proxy_info_.RemoveProxiesWithoutScheme(
net::ProxyServer::SCHEME_HTTP | net::ProxyServer::SCHEME_HTTPS |
net::ProxyServer::SCHEME_SOCKS4 | net::ProxyServer::SCHEME_SOCKS5);
if (proxy_info_.is_empty()) {
// No proxies/direct to choose from. This happens when we don't support
// any of the proxies in the returned list.
return net::ERR_NO_SUPPORTED_PROXIES;
}
next_state_ = STATE_INIT_CONNECTION;
return net::OK;
}
return result;
}
int ProxyResolvingClientSocket::DoInitConnection() {
DCHECK(!socket_);
// QUIC proxies are currently not supported.
DCHECK(proxy_info_.is_direct() ||
!proxy_info_.proxy_chain().Last().is_quic());
next_state_ = STATE_INIT_CONNECTION_COMPLETE;
std::optional<net::NetworkTrafficAnnotationTag> proxy_annotation_tag =
proxy_info_.is_direct() ? std::nullopt
: std::optional<net::NetworkTrafficAnnotationTag>(
proxy_info_.traffic_annotation());
connect_job_ = connect_job_factory_->CreateConnectJob(
use_tls_, net::HostPortPair::FromURL(url_), proxy_info_.proxy_chain(),
proxy_annotation_tag, /*force_tunnel=*/true, net::PRIVACY_MODE_DISABLED,
net::OnHostResolutionCallback(), net::MAXIMUM_PRIORITY, net::SocketTag(),
network_anonymization_key_, net::SecureDnsPolicy::kAllow,
common_connect_job_params_, this);
return connect_job_->Connect();
}
int ProxyResolvingClientSocket::DoInitConnectionComplete(int result) {
if (result != net::OK) {
connect_job_.reset();
// ReconsiderProxyAfterError either returns an error (in which case it is
// not reconsidering a proxy) or returns ERR_IO_PENDING if it is considering
// another proxy.
return ReconsiderProxyAfterError(result);
}
socket_ = connect_job_->PassSocket();
connect_job_.reset();
network_session_->proxy_resolution_service()->ReportSuccess(proxy_info_);
return net::OK;
}
void ProxyResolvingClientSocket::OnConnectJobComplete(int result,
net::ConnectJob* job) {
DCHECK_EQ(next_state_, STATE_INIT_CONNECTION_COMPLETE);
DCHECK_EQ(connect_job_.get(), job);
OnIOComplete(result);
}
void ProxyResolvingClientSocket::OnNeedsProxyAuth(
const net::HttpResponseInfo& response,
net::HttpAuthController* auth_controller,
base::OnceClosure restart_with_auth_callback,
net::ConnectJob* job) {
DCHECK_EQ(next_state_, STATE_INIT_CONNECTION_COMPLETE);
DCHECK_EQ(connect_job_.get(), job);
// If there are credentials available to try and use, use them.
if (auth_controller->HaveAuth()) {
std::move(restart_with_auth_callback).Run();
return;
}
// Otherwise, cancel the ConnectJob and continue.
connect_job_.reset();
OnIOComplete(net::ERR_PROXY_AUTH_REQUESTED);
}
net::Error ProxyResolvingClientSocket::OnDestinationDnsAliasesResolved(
const std::set<std::string>& aliases,
net::ConnectJob* job) {
// Ignore DNS aliases for proxy hostnames since higher-level layers will not
// take action on these.
return net::OK;
}
int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error) {
DCHECK(!socket_);
DCHECK(!proxy_resolve_request_);
DCHECK_NE(error, net::OK);
DCHECK_NE(error, net::ERR_IO_PENDING);
// Check if the error was a proxy failure.
if (!net::CanFalloverToNextProxy(
proxy_info_.proxy_chain(), error, &error,
common_connect_job_params_->proxy_delegate)) {
return error;
}
// TODO(davidben): When adding proxy client certificate support to this class,
// clear the SSLClientAuthCache entries on error.
// There was nothing left to fall-back to, so fail the transaction
// with the last connection error we got.
if (!proxy_info_.Fallback(error, net_log_))
return error;
next_state_ = STATE_INIT_CONNECTION;
return net::OK;
}
} // namespace network
|