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