Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +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_SERVER_PROXY_H_ |
| 5 | #define SYSTEM_PROXY_SERVER_PROXY_H_ |
| 6 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 7 | #include <list> |
| 8 | #include <map> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 9 | #include <memory> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 10 | #include <string> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 11 | #include <vector> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 12 | |
| 13 | #include <base/callback_forward.h> |
| 14 | #include <base/files/file_descriptor_watcher_posix.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 15 | #include <base/files/scoped_file.h> |
| 16 | #include <base/memory/weak_ptr.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 17 | #include <brillo/asynchronous_signal_handler.h> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 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 Socket; |
| 22 | class SocketForwarder; |
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 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 25 | namespace system_proxy { |
| 26 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 27 | using OnProxyResolvedCallback = |
| 28 | base::OnceCallback<void(const std::list<std::string>&)>; |
| 29 | |
| 30 | class ProxyConnectJob; |
| 31 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 32 | // ServerProxy listens for connections from the host (system services, ARC++ |
| 33 | // apps) and sets-up connections to the remote server. |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 34 | // Note: System-proxy only supports proxying over IPv4 networks. |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 35 | class ServerProxy { |
| 36 | public: |
| 37 | explicit ServerProxy(base::OnceClosure quit_closure); |
| 38 | ServerProxy(const ServerProxy&) = delete; |
| 39 | ServerProxy& operator=(const ServerProxy&) = delete; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 40 | virtual ~ServerProxy(); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 41 | |
| 42 | void Init(); |
| 43 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 44 | // Creates a proxy resolution request that is forwarded to the parent process |
| 45 | // trough the standard output. When the request is resolved, the parent |
| 46 | // process will send the result trough the standard input. |
| 47 | // |callback| will be called when the proxy is resolved, with the list of |
| 48 | // proxy servers as parameter ,or in case of failure, with a list containing |
| 49 | // only the direct proxy. |
| 50 | void ResolveProxy(const std::string& target_url, |
| 51 | OnProxyResolvedCallback callback); |
| 52 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 53 | protected: |
| 54 | virtual int GetStdinPipe(); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 55 | virtual int GetStdoutPipe(); |
Andreea Costinas | b715eef | 2020-05-26 09:14:32 +0200 | [diff] [blame] | 56 | virtual void HandleStdinReadable(); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 57 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 58 | private: |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 59 | friend class ServerProxyTest; |
| 60 | FRIEND_TEST(ServerProxyTest, FetchCredentials); |
| 61 | FRIEND_TEST(ServerProxyTest, FetchListeningAddress); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 62 | FRIEND_TEST(ServerProxyTest, HandleConnectRequest); |
| 63 | FRIEND_TEST(ServerProxyTest, HandlePendingJobs); |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 64 | FRIEND_TEST(ServerProxyTest, SetupConnection); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 65 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 66 | bool HandleSignal(const struct signalfd_siginfo& siginfo); |
| 67 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 68 | void CreateListeningSocket(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 69 | void OnConnectionAccept(); |
| 70 | |
| 71 | // Called by |ProxyConnectJob| after setting up the connection with the remote |
| 72 | // server via the remote proxy server. If the connection is successful, |fwd| |
| 73 | // corresponds to the tunnel between the client and the server that has |
| 74 | // started to forward data. In case of failure, |fwd| is empty. |
| 75 | void OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 76 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 77 | ProxyConnectJob* connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 78 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 79 | // Called when the proxy resolution result for |target_url| is received via |
| 80 | // the standard input (see |ResolveProxy| method). |proxy_servers| will always |
| 81 | // contain at least one entry, the direct proxy. |
| 82 | void OnProxyResolved(const std::string& target_url, |
| 83 | const std::list<std::string>& proxy_servers); |
| 84 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 85 | // The proxy listening address in network-byte order. |
| 86 | uint32_t listening_addr_ = 0; |
| 87 | int listening_port_; |
| 88 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 89 | // The user name and password to use for proxy authentication in the format |
| 90 | // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL |
| 91 | // encoded and separated by colon. |
| 92 | std::string credentials_; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 93 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 94 | std::unique_ptr<patchpanel::Socket> listening_fd_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 95 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 96 | // List of SocketForwarders that corresponds to the TCP tunnel between the |
| 97 | // local client and the remote proxy, forwarding data between the TCP |
| 98 | // connection initiated by the local client to the local proxy and the TCP |
| 99 | // connection initiated by the local proxy to the remote proxy. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 100 | std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 101 | |
| 102 | std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>> |
| 103 | pending_connect_jobs_; |
| 104 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 105 | // Collection of ongoing proxy resolution requests. The key represents the |
| 106 | // target url to be resolved and it's mapped to a list of callbaks to pending |
| 107 | // connect jobs that are connecting to the same target url. |
| 108 | std::map<std::string, std::list<OnProxyResolvedCallback>> |
| 109 | pending_proxy_resolution_requests_; |
| 110 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 111 | base::OnceClosure quit_closure_; |
| 112 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 113 | std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 114 | brillo::AsynchronousSignalHandler signal_handler_; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 115 | |
| 116 | base::WeakPtrFactory<ServerProxy> weak_ptr_factory_; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 117 | }; |
| 118 | } // namespace system_proxy |
| 119 | |
| 120 | #endif // SYSTEM_PROXY_SERVER_PROXY_H_ |