blob: 5096636505ccdba9e572cd13ee8301c2ec0e4af6 [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
Andreea Costinase45d54b2020-03-10 09:21:14 +01007#include <list>
8#include <map>
Andreea Costinas41e06442020-03-09 09:41:51 +01009#include <memory>
Andreea Costinas44cefa22020-03-09 09:07:39 +010010#include <string>
Andreea Costinase45d54b2020-03-10 09:21:14 +010011#include <vector>
Andreea Costinas41e06442020-03-09 09:41:51 +010012
13#include <base/callback_forward.h>
14#include <base/files/file_descriptor_watcher_posix.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010015#include <base/files/scoped_file.h>
16#include <base/memory/weak_ptr.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010017#include <brillo/asynchronous_signal_handler.h>
Andreea Costinas44cefa22020-03-09 09:07:39 +010018#include <gtest/gtest_prod.h> // for FRIEND_TEST
Andreea Costinas41e06442020-03-09 09:41:51 +010019
Andreea Costinase45d54b2020-03-10 09:21:14 +010020namespace arc_networkd {
21class Socket;
22class SocketForwarder;
23} // namespace arc_networkd
24
Andreea Costinas41e06442020-03-09 09:41:51 +010025namespace system_proxy {
26
Andreea Costinase45d54b2020-03-10 09:21:14 +010027using OnProxyResolvedCallback =
28 base::OnceCallback<void(const std::list<std::string>&)>;
29
30class ProxyConnectJob;
31
Andreea Costinas41e06442020-03-09 09:41:51 +010032// ServerProxy listens for connections from the host (system services, ARC++
33// apps) and sets-up connections to the remote server.
Andreea Costinas44cefa22020-03-09 09:07:39 +010034// Note: System-proxy only supports proxying over IPv4 networks.
Andreea Costinas41e06442020-03-09 09:41:51 +010035class ServerProxy {
36 public:
37 explicit ServerProxy(base::OnceClosure quit_closure);
38 ServerProxy(const ServerProxy&) = delete;
39 ServerProxy& operator=(const ServerProxy&) = delete;
Andreea Costinas44cefa22020-03-09 09:07:39 +010040 virtual ~ServerProxy();
Andreea Costinas41e06442020-03-09 09:41:51 +010041
42 void Init();
43
Andreea Costinase45d54b2020-03-10 09:21:14 +010044 // 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 Costinas44cefa22020-03-09 09:07:39 +010053 protected:
54 virtual int GetStdinPipe();
55
Andreea Costinas41e06442020-03-09 09:41:51 +010056 private:
Andreea Costinas44cefa22020-03-09 09:07:39 +010057 friend class ServerProxyTest;
58 FRIEND_TEST(ServerProxyTest, FetchCredentials);
59 FRIEND_TEST(ServerProxyTest, FetchListeningAddress);
Andreea Costinase45d54b2020-03-10 09:21:14 +010060 FRIEND_TEST(ServerProxyTest, HandleConnectRequest);
61 FRIEND_TEST(ServerProxyTest, HandlePendingJobs);
Andreea Costinas44cefa22020-03-09 09:07:39 +010062
Andreea Costinas41e06442020-03-09 09:41:51 +010063 void HandleStdinReadable();
64 bool HandleSignal(const struct signalfd_siginfo& siginfo);
65
Andreea Costinas44cefa22020-03-09 09:07:39 +010066 void CreateListeningSocket();
Andreea Costinase45d54b2020-03-10 09:21:14 +010067 void OnConnectionAccept();
68
69 // Called by |ProxyConnectJob| after setting up the connection with the remote
70 // server via the remote proxy server. If the connection is successful, |fwd|
71 // corresponds to the tunnel between the client and the server that has
72 // started to forward data. In case of failure, |fwd| is empty.
73 void OnConnectionSetupFinished(
74 std::unique_ptr<arc_networkd::SocketForwarder> fwd,
75 ProxyConnectJob* connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +010076
77 // The proxy listening address in network-byte order.
78 uint32_t listening_addr_ = 0;
79 int listening_port_;
80
Andreea Costinase45d54b2020-03-10 09:21:14 +010081 // The user name and password to use for proxy authentication in the format
82 // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL
83 // encoded and separated by colon.
84 std::string credentials_;
Andreea Costinas44cefa22020-03-09 09:07:39 +010085 std::unique_ptr<arc_networkd::Socket> listening_fd_;
86
Andreea Costinase45d54b2020-03-10 09:21:14 +010087 // List of SocketForwarders that corresponds to the TCP tunnel between the
88 // local client and the remote proxy, forwarding data between the TCP
89 // connection initiated by the local client to the local proxy and the TCP
90 // connection initiated by the local proxy to the remote proxy.
91 std::list<std::unique_ptr<arc_networkd::SocketForwarder>> forwarders_;
92
93 std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>>
94 pending_connect_jobs_;
95
Andreea Costinas41e06442020-03-09 09:41:51 +010096 base::OnceClosure quit_closure_;
97 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_;
Andreea Costinas44cefa22020-03-09 09:07:39 +010098 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;
Andreea Costinas41e06442020-03-09 09:41:51 +010099 brillo::AsynchronousSignalHandler signal_handler_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100100
101 base::WeakPtrFactory<ServerProxy> weak_ptr_factory_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100102};
103} // namespace system_proxy
104
105#endif // SYSTEM_PROXY_SERVER_PROXY_H_