Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "system-proxy/proxy_connect_job.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 11 | #include <curl/easy.h> |
| 12 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 13 | #include <base/base64.h> |
| 14 | #include <base/bind.h> |
| 15 | #include <base/bind_helpers.h> |
| 16 | #include <base/callback_helpers.h> |
| 17 | #include <base/files/file_util.h> |
| 18 | #include <base/strings/stringprintf.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 19 | #include <base/strings/string_util.h> |
| 20 | #include <base/time/time.h> |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 21 | #include <base/threading/thread.h> |
| 22 | #include <base/threading/thread_task_runner_handle.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 23 | #include <brillo/http/http_transport.h> |
Garrick Evans | cd8c297 | 2020-04-14 14:35:52 +0900 | [diff] [blame] | 24 | #include <chromeos/patchpanel/net_util.h> |
| 25 | #include <chromeos/patchpanel/socket.h> |
| 26 | #include <chromeos/patchpanel/socket_forwarder.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 27 | |
| 28 | #include "system-proxy/curl_socket.h" |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 29 | #include "system-proxy/http_util.h" |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 30 | |
Garrick Evans | 2d5e7c9 | 2020-06-08 14:14:28 +0900 | [diff] [blame] | 31 | // The libpatchpanel-util library overloads << for socket data structures. |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 32 | // By C++'s argument-dependent lookup rules, operators defined in a |
| 33 | // different namespace are not visible. We need the using directive to make |
| 34 | // the overload available this namespace. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 35 | using patchpanel::operator<<; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 36 | |
| 37 | namespace { |
| 38 | // There's no RFC recomandation for the max size of http request headers but |
| 39 | // popular http server implementations (Apache, IIS, Tomcat) set the lower limit |
| 40 | // to 8000. |
| 41 | constexpr int kMaxHttpRequestHeadersSize = 8000; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 42 | constexpr base::TimeDelta kCurlConnectTimeout = |
| 43 | base::TimeDelta::FromSeconds(30); |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 44 | constexpr base::TimeDelta kWaitClientConnectTimeout = |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 45 | base::TimeDelta::FromSeconds(15); |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 46 | // Time to wait for proxy authentication credentials to be fetched from the |
| 47 | // browser. The credentials are retrieved either from the Network Service or, if |
| 48 | // the Network Service doesn't have them, directly from the user via a login |
| 49 | // dialogue. |
| 50 | constexpr base::TimeDelta kCredentialsRequestTimeout = |
| 51 | base::TimeDelta::FromMinutes(1); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 52 | constexpr size_t kMaxBadRequestPrintSize = 120; |
| 53 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 54 | constexpr int64_t kHttpCodeProxyAuthRequired = 407; |
| 55 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 56 | // HTTP error codes and messages with origin information for debugging (RFC723, |
| 57 | // section 6.1). |
| 58 | const std::string_view kHttpBadRequest = |
| 59 | "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 60 | const std::string_view kHttpConnectionTimeout = |
| 61 | "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 62 | const std::string_view kHttpInternalServerError = |
| 63 | "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n"; |
| 64 | const std::string_view kHttpBadGateway = |
| 65 | "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 66 | const std::string_view kHttpProxyAuthRequired = |
| 67 | "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n"; |
| 68 | constexpr char kHttpErrorTunnelFailed[] = |
| 69 | "HTTP/1.1 %s Error creating tunnel - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 70 | } // namespace |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 71 | |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 72 | namespace system_proxy { |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 73 | // CURLOPT_HEADERFUNCTION callback implementation that only returns the headers |
| 74 | // from the last response sent by the sever. This is to make sure that we |
| 75 | // send back valid HTTP replies and auhentication data from the HTTP messages is |
| 76 | // not being leaked to the client. |userdata| is set on the libcurl CURL handle |
| 77 | // used to configure the request, using the the CURLOPT_HEADERDATA option. Note, |
| 78 | // from the libcurl documentation: This callback is being called for all the |
| 79 | // responses received from the proxy server after intiating the connection |
| 80 | // request. Multiple responses can be received in an authentication sequence. |
| 81 | // Only the last response's headers should be forwarded to the System-proxy |
| 82 | // client. The header callback will be called once for each header and only |
| 83 | // complete header lines are passed on to the callback. |
| 84 | static size_t WriteHeadersCallback(char* contents, |
| 85 | size_t size, |
| 86 | size_t nmemb, |
| 87 | void* userdata) { |
| 88 | std::vector<char>* vec = (std::vector<char>*)userdata; |
| 89 | |
| 90 | // Check if we are receiving a new HTTP message (after the last one was |
| 91 | // terminated with an empty line). |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 92 | if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) { |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 93 | VLOG(1) << "Removing the http reply headers from the server " |
| 94 | << base::StringPiece(vec->data(), vec->size()); |
| 95 | vec->clear(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 96 | } |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 97 | vec->insert(vec->end(), contents, contents + (nmemb * size)); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 98 | return size * nmemb; |
| 99 | } |
| 100 | |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 101 | // CONNECT requests may have a reply body. This method will capture the reply |
| 102 | // and save it in |userdata|. |userdata| is set on the libcurl CURL handle |
| 103 | // used to configure the request, using the the CURLOPT_WRITEDATA option. |
| 104 | static size_t WriteCallback(char* contents, |
| 105 | size_t size, |
| 106 | size_t nmemb, |
| 107 | void* userdata) { |
| 108 | std::vector<char>* vec = (std::vector<char>*)userdata; |
| 109 | vec->insert(vec->end(), contents, contents + (nmemb * size)); |
| 110 | return size * nmemb; |
| 111 | } |
| 112 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 113 | ProxyConnectJob::ProxyConnectJob( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 114 | std::unique_ptr<patchpanel::Socket> socket, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 115 | const std::string& credentials, |
| 116 | ResolveProxyCallback resolve_proxy_callback, |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 117 | AuthenticationRequiredCallback auth_required_callback, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 118 | OnConnectionSetupFinishedCallback setup_finished_callback) |
| 119 | : credentials_(credentials), |
| 120 | resolve_proxy_callback_(std::move(resolve_proxy_callback)), |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 121 | auth_required_callback_(std::move(auth_required_callback)), |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 122 | setup_finished_callback_(std::move(setup_finished_callback)), |
| 123 | // Safe to use |base::Unretained| because the callback will be canceled |
| 124 | // when it goes out of scope. |
| 125 | client_connect_timeout_callback_(base::Bind( |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 126 | &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))), |
| 127 | credentials_request_timeout_callback_(base::Bind( |
| 128 | &ProxyConnectJob::OnAuthenticationTimeout, base::Unretained(this))) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 129 | client_socket_ = std::move(socket); |
| 130 | } |
| 131 | |
| 132 | ProxyConnectJob::~ProxyConnectJob() = default; |
| 133 | |
| 134 | bool ProxyConnectJob::Start() { |
| 135 | // Make the socket non-blocking. |
| 136 | if (!base::SetNonBlocking(client_socket_->fd())) { |
| 137 | PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking."; |
| 138 | client_socket_->SendTo(kHttpInternalServerError.data(), |
| 139 | kHttpInternalServerError.size()); |
| 140 | return false; |
| 141 | } |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 142 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 143 | FROM_HERE, client_connect_timeout_callback_.callback(), |
| 144 | kWaitClientConnectTimeout); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 145 | read_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame] | 146 | client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady, |
| 147 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
| 151 | void ProxyConnectJob::OnClientReadReady() { |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 152 | if (!read_watcher_) { |
| 153 | // The connection has timed out while waiting for the client's HTTP CONNECT |
| 154 | // request. See |OnClientConnectTimeout|. |
| 155 | return; |
| 156 | } |
| 157 | client_connect_timeout_callback_.Cancel(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 158 | // Stop watching. |
| 159 | read_watcher_.reset(); |
| 160 | // The first message should be a HTTP CONNECT request. |
| 161 | std::vector<char> connect_request; |
| 162 | if (!TryReadHttpHeader(&connect_request)) { |
| 163 | std::string encoded; |
| 164 | base::Base64Encode( |
| 165 | base::StringPiece(connect_request.data(), connect_request.size()), |
| 166 | &encoded); |
| 167 | LOG(ERROR) << *this |
| 168 | << " Failure to read proxy CONNECT request. Base 64 encoded " |
| 169 | "request message from client: " |
| 170 | << encoded; |
| 171 | OnError(kHttpBadRequest); |
| 172 | return; |
| 173 | } |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 174 | base::StringPiece request(connect_request.data(), connect_request.size()); |
| 175 | target_url_ = GetUriAuthorityFromHttpHeader(request); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 176 | if (target_url_.empty()) { |
| 177 | LOG(ERROR) |
| 178 | << *this |
| 179 | << " Failed to extract target url from the HTTP CONNECT request."; |
| 180 | OnError(kHttpBadRequest); |
| 181 | return; |
| 182 | } |
| 183 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 184 | // The proxy resolution service in Chrome expects a proper URL, formatted as |
| 185 | // scheme://host:port. It's safe to assume only https will be used for the |
| 186 | // target url. |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 187 | std::move(resolve_proxy_callback_) |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 188 | .Run(base::StringPrintf("https://%s", target_url_.c_str()), |
| 189 | base::Bind(&ProxyConnectJob::OnProxyResolution, |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame] | 190 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 194 | size_t read_byte_count = 0; |
| 195 | raw_request->resize(kMaxHttpRequestHeadersSize); |
| 196 | |
| 197 | // Read byte-by-byte and stop when reading an empty line (only CRLF) or when |
| 198 | // exceeding the max buffer size. |
| 199 | // TODO(acostinas, chromium:1064536) This may have some measurable performance |
| 200 | // impact. We should read larger blocks of data, consume the HTTP headers, |
| 201 | // cache the tunneled payload that may have already been included (e.g. TLS |
| 202 | // ClientHello) and send it to server after the connection is established. |
| 203 | while (read_byte_count < kMaxHttpRequestHeadersSize) { |
| 204 | if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <= |
| 205 | 0) { |
| 206 | raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize)); |
| 207 | return false; |
| 208 | } |
| 209 | ++read_byte_count; |
| 210 | |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 211 | if (IsEndingWithHttpEmptyLine( |
| 212 | base::StringPiece(raw_request->data(), read_byte_count))) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 213 | raw_request->resize(read_byte_count); |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | void ProxyConnectJob::OnProxyResolution( |
| 221 | const std::list<std::string>& proxy_servers) { |
| 222 | proxy_servers_ = proxy_servers; |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 223 | DoCurlServerConnection(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 226 | void ProxyConnectJob::AuthenticationRequired( |
| 227 | const std::vector<char>& http_response_headers) { |
| 228 | DCHECK(!proxy_servers_.empty()); |
| 229 | SchemeRealmPairList scheme_realm_pairs = ParseAuthChallenge(base::StringPiece( |
| 230 | http_response_headers.data(), http_response_headers.size())); |
| 231 | if (scheme_realm_pairs.empty()) { |
| 232 | LOG(ERROR) << "Failed to parse authentication challenge"; |
| 233 | OnError(kHttpBadGateway); |
| 234 | return; |
| 235 | } |
| 236 | |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 237 | if (!authentication_timer_started_) { |
| 238 | authentication_timer_started_ = true; |
| 239 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 240 | FROM_HERE, credentials_request_timeout_callback_.callback(), |
| 241 | kCredentialsRequestTimeout); |
| 242 | } |
| 243 | |
| 244 | auth_required_callback_.Run( |
| 245 | proxy_servers_.front(), scheme_realm_pairs.front().first, |
| 246 | scheme_realm_pairs.front().second, credentials_, |
| 247 | base::BindRepeating(&ProxyConnectJob::OnAuthCredentialsProvided, |
| 248 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void ProxyConnectJob::OnAuthCredentialsProvided( |
| 252 | const std::string& credentials) { |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 253 | // If no credentials were returned or if the same bad credentials were |
| 254 | // returned twice, quit the connection. This is to ensure that bad credentials |
| 255 | // acquired from the Network Service won't trigger an authentication loop. |
| 256 | if (credentials.empty() || credentials_ == credentials) { |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 257 | SendHttpResponseToClient(/* http_response_headers= */ {}, |
| 258 | /* http_response_body= */ {}); |
| 259 | std::move(setup_finished_callback_).Run(nullptr, this); |
| 260 | return; |
| 261 | } |
| 262 | credentials_ = credentials; |
| 263 | VLOG(1) << "Connecting to the remote server with provided credentials"; |
| 264 | DoCurlServerConnection(); |
| 265 | } |
| 266 | |
| 267 | bool ProxyConnectJob::AreAuthCredentialsRequired(CURL* easyhandle) { |
| 268 | if (http_response_code_ != kHttpCodeProxyAuthRequired) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | CURLcode res; |
| 273 | int64_t server_proxy_auth_scheme = 0; |
| 274 | res = curl_easy_getinfo(easyhandle, CURLINFO_PROXYAUTH_AVAIL, |
| 275 | &server_proxy_auth_scheme); |
| 276 | if (res != CURLE_OK || !server_proxy_auth_scheme) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // If kerberos is enabled, then we need to wait for the user to request a |
| 281 | // kerberos ticket from Chrome. |
| 282 | return !(server_proxy_auth_scheme & CURLAUTH_NEGOTIATE); |
| 283 | } |
| 284 | |
| 285 | void ProxyConnectJob::DoCurlServerConnection() { |
| 286 | DCHECK(!proxy_servers_.empty()); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 287 | CURL* easyhandle = curl_easy_init(); |
| 288 | CURLcode res; |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 289 | curl_socket_t newSocket = -1; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 290 | |
| 291 | if (!easyhandle) { |
| 292 | // Unfortunately it's not possible to get the failure reason. |
| 293 | LOG(ERROR) << *this << " Failure to create curl handle."; |
| 294 | curl_easy_cleanup(easyhandle); |
| 295 | OnError(kHttpInternalServerError); |
| 296 | return; |
| 297 | } |
| 298 | curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str()); |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 299 | std::vector<char> http_response_headers; |
| 300 | std::vector<char> http_response_body; |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 301 | |
| 302 | if (proxy_servers_.front().c_str() != brillo::http::kDirectProxy) { |
| 303 | curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_servers_.front().c_str()); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 304 | curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L); |
| 305 | curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1); |
| 306 | // Allow libcurl to pick authentication method. Curl will use the most |
| 307 | // secure one the remote site claims to support. |
| 308 | curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
| 309 | curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str()); |
| 310 | } |
| 311 | curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS, |
| 312 | kCurlConnectTimeout.InMilliseconds()); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 313 | curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback); |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 314 | curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &http_response_headers); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 315 | curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback); |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 316 | curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &http_response_body); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 317 | |
| 318 | res = curl_easy_perform(easyhandle); |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 319 | curl_easy_getinfo(easyhandle, CURLINFO_HTTP_CONNECTCODE, |
| 320 | &http_response_code_); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 321 | |
| 322 | if (res != CURLE_OK) { |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 323 | LOG(ERROR) << *this << " curl_easy_perform() failed with error: " |
| 324 | << curl_easy_strerror(res); |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 325 | if (AreAuthCredentialsRequired(easyhandle)) { |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 326 | AuthenticationRequired(http_response_headers); |
| 327 | curl_easy_cleanup(easyhandle); |
| 328 | return; |
| 329 | } |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 330 | credentials_request_timeout_callback_.Cancel(); |
| 331 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 332 | curl_easy_cleanup(easyhandle); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 333 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 334 | SendHttpResponseToClient(/* http_response_headers= */ {}, |
| 335 | /* http_response_body= */ {}); |
| 336 | std::move(setup_finished_callback_).Run(nullptr, this); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 337 | return; |
| 338 | } |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 339 | credentials_request_timeout_callback_.Cancel(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 340 | // Extract the socket from the curl handle. |
| 341 | res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket); |
| 342 | if (res != CURLE_OK) { |
| 343 | LOG(ERROR) << *this << " Failed to get socket from curl with error: " |
| 344 | << curl_easy_strerror(res); |
| 345 | curl_easy_cleanup(easyhandle); |
| 346 | OnError(kHttpBadGateway); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle()); |
| 351 | auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket), |
| 352 | std::move(scoped_handle)); |
| 353 | |
| 354 | // Send the server reply to the client. If the connection is successful, the |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 355 | // reply headers should be "HTTP/1.1 200 Connection Established". |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 356 | if (!SendHttpResponseToClient(http_response_headers, http_response_body)) { |
| 357 | std::move(setup_finished_callback_).Run(nullptr, this); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 358 | return; |
| 359 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 360 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 361 | auto fwd = std::make_unique<patchpanel::SocketForwarder>( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 362 | base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()), |
| 363 | std::move(client_socket_), std::move(server_conn)); |
| 364 | // Start forwarding data between sockets. |
| 365 | fwd->Start(); |
| 366 | std::move(setup_finished_callback_).Run(std::move(fwd), this); |
| 367 | } |
| 368 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 369 | bool ProxyConnectJob::SendHttpResponseToClient( |
| 370 | const std::vector<char>& http_response_headers, |
| 371 | const std::vector<char>& http_response_body) { |
| 372 | if (http_response_code_ == 0) { |
| 373 | // No HTTP CONNECT response code is available. |
| 374 | return client_socket_->SendTo(kHttpInternalServerError.data(), |
| 375 | kHttpInternalServerError.size()); |
| 376 | } |
| 377 | |
| 378 | if (http_response_code_ == kHttpCodeProxyAuthRequired) { |
| 379 | // This will be a hint for the user to authenticate via the Browser or |
| 380 | // acquire a Kerberos ticket. |
| 381 | return client_socket_->SendTo(kHttpProxyAuthRequired.data(), |
| 382 | kHttpProxyAuthRequired.size()); |
| 383 | } |
| 384 | |
| 385 | if (http_response_code_ >= 400) { |
| 386 | VLOG(1) << "Failed to set up HTTP tunnel with code " << http_response_code_; |
| 387 | std::string http_error = base::StringPrintf( |
| 388 | kHttpErrorTunnelFailed, std::to_string(http_response_code_).c_str()); |
| 389 | return client_socket_->SendTo(http_error.c_str(), http_error.size()); |
| 390 | } |
| 391 | |
| 392 | if (http_response_headers.empty()) { |
| 393 | return client_socket_->SendTo(kHttpInternalServerError.data(), |
| 394 | kHttpInternalServerError.size()); |
| 395 | } |
| 396 | |
| 397 | VLOG(1) << "Sending server reply to client"; |
| 398 | if (!client_socket_->SendTo(http_response_headers.data(), |
| 399 | http_response_headers.size())) { |
| 400 | PLOG(ERROR) << "Failed to send HTTP server response headers to client"; |
| 401 | return false; |
| 402 | } |
| 403 | if (!http_response_body.empty()) { |
| 404 | if (!client_socket_->SendTo(http_response_body.data(), |
| 405 | http_response_body.size())) { |
| 406 | PLOG(ERROR) << "Failed to send HTTP server response payload to client"; |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | return true; |
| 411 | } |
| 412 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 413 | void ProxyConnectJob::OnError(const std::string_view& http_error_message) { |
| 414 | client_socket_->SendTo(http_error_message.data(), http_error_message.size()); |
| 415 | std::move(setup_finished_callback_).Run(nullptr, this); |
| 416 | } |
| 417 | |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 418 | void ProxyConnectJob::OnClientConnectTimeout() { |
| 419 | // Stop listening for client connect requests. |
| 420 | read_watcher_.reset(); |
| 421 | LOG(ERROR) << *this |
| 422 | << " Connection timed out while waiting for the client to send a " |
| 423 | "connect request."; |
| 424 | OnError(kHttpConnectionTimeout); |
| 425 | } |
| 426 | |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 427 | void ProxyConnectJob::OnAuthenticationTimeout() { |
| 428 | LOG(ERROR) |
| 429 | << *this |
| 430 | << "The connect job timed out while waiting for proxy authentication " |
| 431 | "credentials"; |
| 432 | OnError(kHttpProxyAuthRequired); |
| 433 | } |
| 434 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 435 | std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) { |
| 436 | stream << "{fd: " << job.client_socket_->fd(); |
| 437 | if (!job.target_url_.empty()) { |
| 438 | stream << ", url: " << job.target_url_; |
| 439 | } |
| 440 | stream << "}"; |
| 441 | return stream; |
| 442 | } |
| 443 | |
| 444 | } // namespace system_proxy |