blob: 578a19e5cc430767579aaf9307fe90b4c707eb25 [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
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Andreea Costinase45d54b2020-03-10 09:21:14 +010021class Socket;
22class SocketForwarder;
Garrick Evans3388a032020-03-24 11:25:55 +090023} // namespace patchpanel
Andreea Costinase45d54b2020-03-10 09:21:14 +010024
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>&)>;
Andreea Costinasbb2aa022020-06-13 00:03:23 +020029using OnAuthAcquiredCallback = base::OnceCallback<void(const std::string&)>;
Andreea Costinase45d54b2020-03-10 09:21:14 +010030
31class ProxyConnectJob;
32
Andreea Costinas41e06442020-03-09 09:41:51 +010033// ServerProxy listens for connections from the host (system services, ARC++
34// apps) and sets-up connections to the remote server.
Andreea Costinas44cefa22020-03-09 09:07:39 +010035// Note: System-proxy only supports proxying over IPv4 networks.
Andreea Costinas41e06442020-03-09 09:41:51 +010036class ServerProxy {
37 public:
38 explicit ServerProxy(base::OnceClosure quit_closure);
39 ServerProxy(const ServerProxy&) = delete;
40 ServerProxy& operator=(const ServerProxy&) = delete;
Andreea Costinas44cefa22020-03-09 09:07:39 +010041 virtual ~ServerProxy();
Andreea Costinas41e06442020-03-09 09:41:51 +010042
43 void Init();
44
Andreea Costinase45d54b2020-03-10 09:21:14 +010045 // Creates a proxy resolution request that is forwarded to the parent process
46 // trough the standard output. When the request is resolved, the parent
47 // process will send the result trough the standard input.
48 // |callback| will be called when the proxy is resolved, with the list of
49 // proxy servers as parameter ,or in case of failure, with a list containing
50 // only the direct proxy.
51 void ResolveProxy(const std::string& target_url,
52 OnProxyResolvedCallback callback);
Andreea Costinasbb2aa022020-06-13 00:03:23 +020053 // Creates an authentication required request that is forwarded to the parent
54 // process trough the standard output. When the request is resolved, the
55 // parent process will send the result trough the standard input. |callback|
56 // will be called when the credentials associated to the protection space
57 // given by the input parameters, or empty strings in case of failure or
58 // missing credentials.
59 void AuthenticationRequired(const std::string& proxy_url,
60 const std::string& scheme,
61 const std::string& realm,
62 OnAuthAcquiredCallback callback);
Andreea Costinase45d54b2020-03-10 09:21:14 +010063
Andreea Costinas44cefa22020-03-09 09:07:39 +010064 protected:
65 virtual int GetStdinPipe();
Andreea Costinas5862b102020-03-19 14:45:36 +010066 virtual int GetStdoutPipe();
Andreea Costinasb715eef2020-05-26 09:14:32 +020067 virtual void HandleStdinReadable();
Andreea Costinas44cefa22020-03-09 09:07:39 +010068
Andreea Costinas41e06442020-03-09 09:41:51 +010069 private:
Andreea Costinas44cefa22020-03-09 09:07:39 +010070 friend class ServerProxyTest;
71 FRIEND_TEST(ServerProxyTest, FetchCredentials);
72 FRIEND_TEST(ServerProxyTest, FetchListeningAddress);
Andreea Costinase45d54b2020-03-10 09:21:14 +010073 FRIEND_TEST(ServerProxyTest, HandleConnectRequest);
74 FRIEND_TEST(ServerProxyTest, HandlePendingJobs);
Andreea Costinas5862b102020-03-19 14:45:36 +010075 FRIEND_TEST(ServerProxyTest, SetupConnection);
Andreea Costinas833eb7c2020-06-12 11:09:15 +020076 FRIEND_TEST(ServerProxyTest, HandleCanceledJobWhilePendingProxyResolution);
Andreea Costinas44cefa22020-03-09 09:07:39 +010077
Andreea Costinas41e06442020-03-09 09:41:51 +010078 bool HandleSignal(const struct signalfd_siginfo& siginfo);
79
Andreea Costinas44cefa22020-03-09 09:07:39 +010080 void CreateListeningSocket();
Andreea Costinase45d54b2020-03-10 09:21:14 +010081 void OnConnectionAccept();
82
83 // Called by |ProxyConnectJob| after setting up the connection with the remote
84 // server via the remote proxy server. If the connection is successful, |fwd|
85 // corresponds to the tunnel between the client and the server that has
86 // started to forward data. In case of failure, |fwd| is empty.
87 void OnConnectionSetupFinished(
Garrick Evans3388a032020-03-24 11:25:55 +090088 std::unique_ptr<patchpanel::SocketForwarder> fwd,
Andreea Costinase45d54b2020-03-10 09:21:14 +010089 ProxyConnectJob* connect_job);
Andreea Costinas44cefa22020-03-09 09:07:39 +010090
Andreea Costinas5862b102020-03-19 14:45:36 +010091 // Called when the proxy resolution result for |target_url| is received via
92 // the standard input (see |ResolveProxy| method). |proxy_servers| will always
93 // contain at least one entry, the direct proxy.
94 void OnProxyResolved(const std::string& target_url,
95 const std::list<std::string>& proxy_servers);
96
Andreea Costinasbb2aa022020-06-13 00:03:23 +020097 void OnCredentialsReceived();
98
Andreea Costinas922fbaf2020-05-28 11:55:22 +020099 // Sets the environment variables for kerberos authentication.
100 void SetKerberosEnv(bool kerberos_enabled);
101
Andreea Costinas44cefa22020-03-09 09:07:39 +0100102 // The proxy listening address in network-byte order.
103 uint32_t listening_addr_ = 0;
104 int listening_port_;
105
Andreea Costinase45d54b2020-03-10 09:21:14 +0100106 // The user name and password to use for proxy authentication in the format
107 // compatible with libcurl's CURLOPT_USERPWD: both user name and password URL
108 // encoded and separated by colon.
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200109 std::string system_credentials_;
Andreea Costinas5862b102020-03-19 14:45:36 +0100110
Garrick Evans3388a032020-03-24 11:25:55 +0900111 std::unique_ptr<patchpanel::Socket> listening_fd_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100112
Andreea Costinase45d54b2020-03-10 09:21:14 +0100113 // List of SocketForwarders that corresponds to the TCP tunnel between the
114 // local client and the remote proxy, forwarding data between the TCP
115 // connection initiated by the local client to the local proxy and the TCP
116 // connection initiated by the local proxy to the remote proxy.
Garrick Evans3388a032020-03-24 11:25:55 +0900117 std::list<std::unique_ptr<patchpanel::SocketForwarder>> forwarders_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100118
119 std::map<ProxyConnectJob*, std::unique_ptr<ProxyConnectJob>>
120 pending_connect_jobs_;
121
Andreea Costinas5862b102020-03-19 14:45:36 +0100122 // Collection of ongoing proxy resolution requests. The key represents the
123 // target url to be resolved and it's mapped to a list of callbaks to pending
124 // connect jobs that are connecting to the same target url.
125 std::map<std::string, std::list<OnProxyResolvedCallback>>
126 pending_proxy_resolution_requests_;
127
Andreea Costinas41e06442020-03-09 09:41:51 +0100128 base::OnceClosure quit_closure_;
129 std::unique_ptr<base::FileDescriptorWatcher::Controller> stdin_watcher_;
Andreea Costinas44cefa22020-03-09 09:07:39 +0100130 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100131 brillo::AsynchronousSignalHandler signal_handler_;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100132
133 base::WeakPtrFactory<ServerProxy> weak_ptr_factory_;
Andreea Costinas41e06442020-03-09 09:41:51 +0100134};
135} // namespace system_proxy
136
137#endif // SYSTEM_PROXY_SERVER_PROXY_H_