blob: 0704b14e5699d4eaabfc5867fadea3f0cf1f0967 [file] [log] [blame]
Andreea Costinas41e06442020-03-09 09:41:51 +01001// 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 Costinas44cefa22020-03-09 09:07:39 +01008#include <string>
Andreea Costinas41e06442020-03-09 09:41:51 +01009
Andreea Costinas44cefa22020-03-09 09:07:39 +010010#include <arc/network/socket.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010011#include <base/callback_forward.h>
12#include <base/files/file_descriptor_watcher_posix.h>
13#include <brillo/asynchronous_signal_handler.h>
Andreea Costinas44cefa22020-03-09 09:07:39 +010014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Andreea Costinas41e06442020-03-09 09:41:51 +010015
16namespace 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 Costinas44cefa22020-03-09 09:07:39 +010020// Note: System-proxy only supports proxying over IPv4 networks.
Andreea Costinas41e06442020-03-09 09:41:51 +010021class ServerProxy {
22 public:
23 explicit ServerProxy(base::OnceClosure quit_closure);
24 ServerProxy(const ServerProxy&) = delete;
25 ServerProxy& operator=(const ServerProxy&) = delete;
Andreea Costinas44cefa22020-03-09 09:07:39 +010026 virtual ~ServerProxy();
Andreea Costinas41e06442020-03-09 09:41:51 +010027
28 void Init();
29
Andreea Costinas44cefa22020-03-09 09:07:39 +010030 protected:
31 virtual int GetStdinPipe();
32
Andreea Costinas41e06442020-03-09 09:41:51 +010033 private:
Andreea Costinas44cefa22020-03-09 09:07:39 +010034 friend class ServerProxyTest;
35 FRIEND_TEST(ServerProxyTest, FetchCredentials);
36 FRIEND_TEST(ServerProxyTest, FetchListeningAddress);
37
Andreea Costinas41e06442020-03-09 09:41:51 +010038 void HandleStdinReadable();
39 bool HandleSignal(const struct signalfd_siginfo& siginfo);
40
Andreea Costinas44cefa22020-03-09 09:07:39 +010041 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 Costinas41e06442020-03-09 09:41:51 +010053 base::OnceClosure quit_closure_;
54 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_;
Andreea Costinas44cefa22020-03-09 09:07:39 +010055 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;
56
Andreea Costinas41e06442020-03-09 09:41:51 +010057 brillo::AsynchronousSignalHandler signal_handler_;
58};
59} // namespace system_proxy
60
61#endif // SYSTEM_PROXY_SERVER_PROXY_H_