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 | |
| 11 | #include <curl/curl.h> |
| 12 | #include <curl/easy.h> |
| 13 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 14 | #include <base/base64.h> |
| 15 | #include <base/bind.h> |
| 16 | #include <base/bind_helpers.h> |
| 17 | #include <base/callback_helpers.h> |
| 18 | #include <base/files/file_util.h> |
| 19 | #include <base/strings/stringprintf.h> |
| 20 | #include <base/strings/string_split.h> |
| 21 | #include <base/strings/string_util.h> |
| 22 | #include <base/time/time.h> |
| 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" |
| 29 | |
| 30 | // The libarcnetwork-util library overloads << for socket data structures. |
| 31 | // By C++'s argument-dependent lookup rules, operators defined in a |
| 32 | // different namespace are not visible. We need the using directive to make |
| 33 | // the overload available this namespace. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 34 | using patchpanel::operator<<; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | // There's no RFC recomandation for the max size of http request headers but |
| 38 | // popular http server implementations (Apache, IIS, Tomcat) set the lower limit |
| 39 | // to 8000. |
| 40 | constexpr int kMaxHttpRequestHeadersSize = 8000; |
| 41 | constexpr char kConnectMethod[] = "CONNECT"; |
| 42 | constexpr char kHttpScheme[] = "http://"; |
| 43 | constexpr base::TimeDelta kCurlConnectTimeout = base::TimeDelta::FromMinutes(2); |
| 44 | constexpr size_t kMaxBadRequestPrintSize = 120; |
| 45 | |
| 46 | // HTTP error codes and messages with origin information for debugging (RFC723, |
| 47 | // section 6.1). |
| 48 | const std::string_view kHttpBadRequest = |
| 49 | "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n"; |
| 50 | const std::string_view kHttpInternalServerError = |
| 51 | "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n"; |
| 52 | const std::string_view kHttpBadGateway = |
| 53 | "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n"; |
| 54 | |
| 55 | static size_t WriteCallback(char* contents, |
| 56 | size_t size, |
| 57 | size_t nmemb, |
| 58 | void* userp) { |
| 59 | for (int i = 0; i < nmemb * size; ++i) { |
| 60 | ((std::vector<char>*)userp)->push_back(contents[i]); |
| 61 | } |
| 62 | return size * nmemb; |
| 63 | } |
| 64 | |
| 65 | // Parses the first line of the http CONNECT request and extracts the target |
| 66 | // url. The destination URI is specified in the request line as the host name |
| 67 | // and destination port number separated by a colon (RFC2817, section 5.2): |
| 68 | // CONNECT server.example.com:80 HTTP/1.1 |
| 69 | // If the first line in |raw_request| (the Request-Line) is a correctly formed |
| 70 | // CONNECT request, it will return the destination URI as scheme://host:port, |
| 71 | // otherwise it will return an empty string. |
| 72 | std::string GetUrlFromHttpHeader(const std::vector<char>& raw_request) { |
| 73 | base::StringPiece request(raw_request.data(), raw_request.size()); |
| 74 | // Request-Line ends with CRLF (RFC2616, section 5.1). |
| 75 | size_t i = request.find_first_of("\r\n"); |
| 76 | if (i == base::StringPiece::npos) |
| 77 | return std::string(); |
| 78 | // Elements are delimited by non-breaking space (SP). |
| 79 | auto pieces = |
| 80 | base::SplitString(request.substr(0, i), " ", base::TRIM_WHITESPACE, |
| 81 | base::SPLIT_WANT_NONEMPTY); |
| 82 | // Request-Line has the format: Method SP Request-URI SP HTTP-Version CRLF. |
| 83 | if (pieces.size() < 3) |
| 84 | return std::string(); |
| 85 | if (pieces[0] != kConnectMethod) |
| 86 | return std::string(); |
| 87 | |
| 88 | return base::JoinString({kHttpScheme, pieces[1]}, ""); |
| 89 | } |
| 90 | } // namespace |
| 91 | |
| 92 | namespace system_proxy { |
| 93 | |
| 94 | ProxyConnectJob::ProxyConnectJob( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 95 | std::unique_ptr<patchpanel::Socket> socket, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 96 | const std::string& credentials, |
| 97 | ResolveProxyCallback resolve_proxy_callback, |
| 98 | OnConnectionSetupFinishedCallback setup_finished_callback) |
| 99 | : credentials_(credentials), |
| 100 | resolve_proxy_callback_(std::move(resolve_proxy_callback)), |
| 101 | setup_finished_callback_(std::move(setup_finished_callback)) { |
| 102 | client_socket_ = std::move(socket); |
| 103 | } |
| 104 | |
| 105 | ProxyConnectJob::~ProxyConnectJob() = default; |
| 106 | |
| 107 | bool ProxyConnectJob::Start() { |
| 108 | // Make the socket non-blocking. |
| 109 | if (!base::SetNonBlocking(client_socket_->fd())) { |
| 110 | PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking."; |
| 111 | client_socket_->SendTo(kHttpInternalServerError.data(), |
| 112 | kHttpInternalServerError.size()); |
| 113 | return false; |
| 114 | } |
| 115 | read_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 116 | client_socket_->fd(), |
| 117 | base::Bind(&ProxyConnectJob::OnClientReadReady, base::Unretained(this))); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void ProxyConnectJob::OnClientReadReady() { |
| 122 | // Stop watching. |
| 123 | read_watcher_.reset(); |
| 124 | // The first message should be a HTTP CONNECT request. |
| 125 | std::vector<char> connect_request; |
| 126 | if (!TryReadHttpHeader(&connect_request)) { |
| 127 | std::string encoded; |
| 128 | base::Base64Encode( |
| 129 | base::StringPiece(connect_request.data(), connect_request.size()), |
| 130 | &encoded); |
| 131 | LOG(ERROR) << *this |
| 132 | << " Failure to read proxy CONNECT request. Base 64 encoded " |
| 133 | "request message from client: " |
| 134 | << encoded; |
| 135 | OnError(kHttpBadRequest); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | target_url_ = GetUrlFromHttpHeader(connect_request); |
| 140 | if (target_url_.empty()) { |
| 141 | LOG(ERROR) |
| 142 | << *this |
| 143 | << " Failed to extract target url from the HTTP CONNECT request."; |
| 144 | OnError(kHttpBadRequest); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | std::move(resolve_proxy_callback_) |
| 149 | .Run(target_url_, base::Bind(&ProxyConnectJob::OnProxyResolution, |
| 150 | base::Unretained(this))); |
| 151 | } |
| 152 | |
| 153 | bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) { |
| 154 | // Used to identify the end of a HTTP header which should be an empty line. |
| 155 | // Note: all HTTP header lines end with CRLF. HTTP connect requests don't have |
| 156 | // a body so end of header is end of request. |
| 157 | std::string crlf_crlf = "\r\n\r\n"; |
| 158 | size_t read_byte_count = 0; |
| 159 | raw_request->resize(kMaxHttpRequestHeadersSize); |
| 160 | |
| 161 | // Read byte-by-byte and stop when reading an empty line (only CRLF) or when |
| 162 | // exceeding the max buffer size. |
| 163 | // TODO(acostinas, chromium:1064536) This may have some measurable performance |
| 164 | // impact. We should read larger blocks of data, consume the HTTP headers, |
| 165 | // cache the tunneled payload that may have already been included (e.g. TLS |
| 166 | // ClientHello) and send it to server after the connection is established. |
| 167 | while (read_byte_count < kMaxHttpRequestHeadersSize) { |
| 168 | if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <= |
| 169 | 0) { |
| 170 | raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize)); |
| 171 | return false; |
| 172 | } |
| 173 | ++read_byte_count; |
| 174 | |
| 175 | // Check if we have an empty line. |
| 176 | if (read_byte_count > crlf_crlf.size() && |
| 177 | std::memcmp(crlf_crlf.data(), |
| 178 | raw_request->data() + read_byte_count - crlf_crlf.size(), |
| 179 | crlf_crlf.size()) == 0) { |
| 180 | raw_request->resize(read_byte_count); |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | void ProxyConnectJob::OnProxyResolution( |
| 188 | const std::list<std::string>& proxy_servers) { |
| 189 | proxy_servers_ = proxy_servers; |
| 190 | DoCurlServerConnection(proxy_servers.front()); |
| 191 | } |
| 192 | |
| 193 | void ProxyConnectJob::DoCurlServerConnection(const std::string& proxy_url) { |
| 194 | CURL* easyhandle = curl_easy_init(); |
| 195 | CURLcode res; |
| 196 | int newSocket = -1; |
| 197 | std::vector<char> server_connect_reply; |
| 198 | |
| 199 | if (!easyhandle) { |
| 200 | // Unfortunately it's not possible to get the failure reason. |
| 201 | LOG(ERROR) << *this << " Failure to create curl handle."; |
| 202 | curl_easy_cleanup(easyhandle); |
| 203 | OnError(kHttpInternalServerError); |
| 204 | return; |
| 205 | } |
| 206 | curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str()); |
| 207 | |
| 208 | if (proxy_url != brillo::http::kDirectProxy) { |
| 209 | curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_url.c_str()); |
| 210 | curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L); |
| 211 | curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1); |
| 212 | // Allow libcurl to pick authentication method. Curl will use the most |
| 213 | // secure one the remote site claims to support. |
| 214 | curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
| 215 | curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str()); |
| 216 | } |
| 217 | curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS, |
| 218 | kCurlConnectTimeout.InMilliseconds()); |
| 219 | curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteCallback); |
| 220 | curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, server_connect_reply.data()); |
| 221 | |
| 222 | res = curl_easy_perform(easyhandle); |
| 223 | |
| 224 | if (res != CURLE_OK) { |
| 225 | LOG(ERROR) << *this << " curl_easy_perform() failed with error: ", |
| 226 | curl_easy_strerror(res); |
| 227 | curl_easy_cleanup(easyhandle); |
| 228 | OnError(kHttpInternalServerError); |
| 229 | return; |
| 230 | } |
| 231 | // Extract the socket from the curl handle. |
| 232 | res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket); |
| 233 | if (res != CURLE_OK) { |
| 234 | LOG(ERROR) << *this << " Failed to get socket from curl with error: " |
| 235 | << curl_easy_strerror(res); |
| 236 | curl_easy_cleanup(easyhandle); |
| 237 | OnError(kHttpBadGateway); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle()); |
| 242 | auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket), |
| 243 | std::move(scoped_handle)); |
| 244 | |
| 245 | // Send the server reply to the client. If the connection is successful, the |
| 246 | // reply should be "HTTP/1.1 200 Connection Established". |
| 247 | client_socket_->SendTo(server_connect_reply.data(), |
| 248 | server_connect_reply.size()); |
| 249 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 250 | auto fwd = std::make_unique<patchpanel::SocketForwarder>( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 251 | base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()), |
| 252 | std::move(client_socket_), std::move(server_conn)); |
| 253 | // Start forwarding data between sockets. |
| 254 | fwd->Start(); |
| 255 | std::move(setup_finished_callback_).Run(std::move(fwd), this); |
| 256 | } |
| 257 | |
| 258 | void ProxyConnectJob::OnError(const std::string_view& http_error_message) { |
| 259 | client_socket_->SendTo(http_error_message.data(), http_error_message.size()); |
| 260 | std::move(setup_finished_callback_).Run(nullptr, this); |
| 261 | } |
| 262 | |
| 263 | std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) { |
| 264 | stream << "{fd: " << job.client_socket_->fd(); |
| 265 | if (!job.target_url_.empty()) { |
| 266 | stream << ", url: " << job.target_url_; |
| 267 | } |
| 268 | stream << "}"; |
| 269 | return stream; |
| 270 | } |
| 271 | |
| 272 | } // namespace system_proxy |