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 | #ifndef SYSTEM_PROXY_PROXY_CONNECT_JOB_H_ |
| 5 | #define SYSTEM_PROXY_PROXY_CONNECT_JOB_H_ |
| 6 | |
| 7 | #include <list> |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <vector> |
| 12 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 13 | #include <curl/curl.h> |
| 14 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 15 | #include <base/callback_forward.h> |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 16 | #include <base/cancelable_callback.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 17 | #include <base/files/file_descriptor_watcher_posix.h> |
| 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 19 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 20 | namespace patchpanel { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 21 | class SocketForwarder; |
| 22 | class Socket; |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 23 | } // namespace patchpanel |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 24 | |
| 25 | namespace system_proxy { |
| 26 | // ProxyConnectJob asynchronously sets up a connection to a remote target on |
| 27 | // behalf of a client. Internally, it performs the following steps: |
| 28 | // - waits for the client to send a HTTP connect request; |
| 29 | // - extracts the target url from the connect request; |
| 30 | // - requests proxy resolution for the target url and waits for the result; |
| 31 | // - performs the proxy authentication and connection setup to the remote |
| 32 | // target. |
| 33 | class ProxyConnectJob { |
| 34 | public: |
| 35 | using OnConnectionSetupFinishedCallback = base::OnceCallback<void( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 36 | std::unique_ptr<patchpanel::SocketForwarder>, ProxyConnectJob*)>; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 37 | |
| 38 | // Will be invoked by ProxyConnectJob to resolve the proxy for |target_url_|. |
| 39 | // The passed |callback| is expected to be called with the list of proxy |
| 40 | // servers, which will always contain at least one entry, the default proxy. |
| 41 | using ResolveProxyCallback = base::OnceCallback<void( |
| 42 | const std::string& url, |
| 43 | base::OnceCallback<void(const std::list<std::string>&)> callback)>; |
| 44 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 45 | ProxyConnectJob(std::unique_ptr<patchpanel::Socket> socket, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 46 | const std::string& credentials, |
| 47 | ResolveProxyCallback resolve_proxy_callback, |
| 48 | OnConnectionSetupFinishedCallback setup_finished_callback); |
| 49 | ProxyConnectJob(const ProxyConnectJob&) = delete; |
| 50 | ProxyConnectJob& operator=(const ProxyConnectJob&) = delete; |
| 51 | virtual ~ProxyConnectJob(); |
| 52 | |
| 53 | // Marks |client_socket_| as non-blocking and adds a watcher that calls |
| 54 | // |OnClientReadReady| when the socket is read ready. |
| 55 | virtual bool Start(); |
| 56 | void OnProxyResolution(const std::list<std::string>& proxy_servers); |
| 57 | |
| 58 | friend std::ostream& operator<<(std::ostream& stream, |
| 59 | const ProxyConnectJob& job); |
| 60 | |
| 61 | private: |
| 62 | friend class ServerProxyTest; |
| 63 | friend class ProxyConnectJobTest; |
| 64 | FRIEND_TEST(ServerProxyTest, HandlePendingJobs); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 65 | FRIEND_TEST(ServerProxyTest, HandleConnectRequest); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 66 | FRIEND_TEST(ProxyConnectJobTest, SuccessfulConnection); |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 67 | FRIEND_TEST(ProxyConnectJobTest, SuccessfulConnectionAltEnding); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 68 | FRIEND_TEST(ProxyConnectJobTest, BadHttpRequestWrongMethod); |
| 69 | FRIEND_TEST(ProxyConnectJobTest, BadHttpRequestNoEmptyLine); |
| 70 | |
| 71 | // Reads data from the socket into |raw_request| until the first empty line, |
| 72 | // which would mark the end of the HTTP request header. |
| 73 | // This method does not validate the headers. |
| 74 | bool TryReadHttpHeader(std::vector<char>* raw_request); |
| 75 | |
| 76 | // Called when the client socket is ready for reading. |
| 77 | void OnClientReadReady(); |
| 78 | |
| 79 | // Called from |OnProxyResolution|, after the proxy for |target_url_| is |
| 80 | // resolved. |
| 81 | void DoCurlServerConnection(const std::string& proxy_url); |
| 82 | |
| 83 | void OnError(const std::string_view& http_error_message); |
| 84 | |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 85 | void OnClientConnectTimeout(); |
| 86 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 87 | // Sends the server response to the client. Returns true if the headers and |
| 88 | // body were sent successfully, false otherwise. In case of failure, calls |
| 89 | // |OnError|. The response headers and body can be empty if the libcurl |
| 90 | // connection fails. In this case, this will send the client an error message |
| 91 | // based on the HTTP status code |http_response_code_|. |
| 92 | bool SendHttpResponseToClient(const std::vector<char>& http_response_headers, |
| 93 | const std::vector<char>& http_response_body); |
| 94 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 95 | std::string target_url_; |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 96 | // HTTP proxy response code to the CONNECT request. |
| 97 | int64_t http_response_code_ = 0; |
| 98 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 99 | const std::string credentials_; |
| 100 | std::list<std::string> proxy_servers_; |
| 101 | ResolveProxyCallback resolve_proxy_callback_; |
| 102 | OnConnectionSetupFinishedCallback setup_finished_callback_; |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 103 | base::CancelableClosure client_connect_timeout_callback_; |
| 104 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 105 | std::unique_ptr<patchpanel::Socket> client_socket_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 106 | std::unique_ptr<base::FileDescriptorWatcher::Controller> read_watcher_; |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame] | 107 | base::WeakPtrFactory<ProxyConnectJob> weak_ptr_factory_{this}; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 108 | }; |
| 109 | } // namespace system_proxy |
| 110 | |
| 111 | #endif // SYSTEM_PROXY_PROXY_CONNECT_JOB_H_ |