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