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 | |
| 7 | #include <memory> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 8 | #include <string> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 9 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 10 | #include <arc/network/socket.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 11 | #include <base/callback_forward.h> |
| 12 | #include <base/files/file_descriptor_watcher_posix.h> |
| 13 | #include <brillo/asynchronous_signal_handler.h> |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 15 | |
| 16 | namespace system_proxy { |
| 17 | |
| 18 | // ServerProxy listens for connections from the host (system services, ARC++ |
| 19 | // apps) and sets-up connections to the remote server. |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 20 | // Note: System-proxy only supports proxying over IPv4 networks. |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 21 | class ServerProxy { |
| 22 | public: |
| 23 | explicit ServerProxy(base::OnceClosure quit_closure); |
| 24 | ServerProxy(const ServerProxy&) = delete; |
| 25 | ServerProxy& operator=(const ServerProxy&) = delete; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 26 | virtual ~ServerProxy(); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 27 | |
| 28 | void Init(); |
| 29 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 30 | protected: |
| 31 | virtual int GetStdinPipe(); |
| 32 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 33 | private: |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 34 | friend class ServerProxyTest; |
| 35 | FRIEND_TEST(ServerProxyTest, FetchCredentials); |
| 36 | FRIEND_TEST(ServerProxyTest, FetchListeningAddress); |
| 37 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 38 | void HandleStdinReadable(); |
| 39 | bool HandleSignal(const struct signalfd_siginfo& siginfo); |
| 40 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 41 | void CreateListeningSocket(); |
| 42 | void OnConnectionRequest(); |
| 43 | |
| 44 | // The proxy listening address in network-byte order. |
| 45 | uint32_t listening_addr_ = 0; |
| 46 | int listening_port_; |
| 47 | |
| 48 | std::string username_; |
| 49 | std::string password_; |
| 50 | |
| 51 | std::unique_ptr<arc_networkd::Socket> listening_fd_; |
| 52 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 53 | base::OnceClosure quit_closure_; |
| 54 | std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 55 | std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; |
| 56 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 57 | brillo::AsynchronousSignalHandler signal_handler_; |
| 58 | }; |
| 59 | } // namespace system_proxy |
| 60 | |
| 61 | #endif // SYSTEM_PROXY_SERVER_PROXY_H_ |