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 | |
| 5 | #include "system-proxy/server_proxy.h" |
| 6 | |
| 7 | #include <iostream> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 12 | #include <arc/network/socket.h> |
| 13 | #include <arc/network/socket_forwarder.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 14 | #include <base/bind.h> |
| 15 | #include <base/bind_helpers.h> |
| 16 | #include <base/callback_helpers.h> |
| 17 | #include <base/posix/eintr_wrapper.h> |
| 18 | #include <base/files/file_util.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 19 | #include <base/strings/string_util.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 20 | #include <base/threading/thread.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 21 | #include <base/threading/thread_task_runner_handle.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 22 | #include <brillo/data_encoding.h> |
| 23 | #include <brillo/http/http_transport.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 24 | |
| 25 | #include "bindings/worker_common.pb.h" |
| 26 | #include "system-proxy/protobuf_util.h" |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 27 | #include "system-proxy/proxy_connect_job.h" |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 28 | |
| 29 | namespace system_proxy { |
| 30 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 31 | namespace { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 32 | |
| 33 | constexpr int kMaxConn = 100; |
| 34 | |
| 35 | // Returns the URL encoded value of |text|. It also verifies if the string was |
| 36 | // already encoded and, if true it returns it unmodified. |
| 37 | std::string UrlEncode(const std::string& text) { |
| 38 | if (text == brillo::data_encoding::UrlDecode(text.c_str())) |
| 39 | return brillo::data_encoding::UrlEncode(text.c_str(), false); |
| 40 | return text; |
| 41 | } |
| 42 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 43 | } // namespace |
| 44 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 45 | ServerProxy::ServerProxy(base::OnceClosure quit_closure) |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 46 | : quit_closure_(std::move(quit_closure)), weak_ptr_factory_(this) {} |
| 47 | ServerProxy::~ServerProxy() = default; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 48 | |
| 49 | void ServerProxy::Init() { |
| 50 | // Start listening for input. |
| 51 | stdin_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 52 | GetStdinPipe(), base::Bind(&ServerProxy::HandleStdinReadable, |
| 53 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 54 | |
| 55 | // Handle termination signals. |
| 56 | signal_handler_.Init(); |
| 57 | for (int signal : {SIGINT, SIGTERM, SIGHUP, SIGQUIT}) { |
| 58 | signal_handler_.RegisterHandler( |
| 59 | signal, base::BindRepeating(&ServerProxy::HandleSignal, |
| 60 | base::Unretained(this))); |
| 61 | } |
| 62 | } |
| 63 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 64 | void ServerProxy::ResolveProxy(const std::string& target_url, |
| 65 | OnProxyResolvedCallback callback) { |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 66 | auto it = pending_proxy_resolution_requests_.find(target_url); |
| 67 | if (it != pending_proxy_resolution_requests_.end()) { |
| 68 | it->second.push_back(std::move(callback)); |
| 69 | return; |
| 70 | } |
| 71 | ProxyResolutionRequest proxy_request; |
| 72 | proxy_request.set_target_url(target_url); |
| 73 | WorkerRequest request; |
| 74 | *request.mutable_proxy_resolution_request() = proxy_request; |
| 75 | if (!WriteProtobuf(GetStdoutPipe(), request)) { |
| 76 | LOG(ERROR) << "Failed to send proxy resolution request for url: " |
| 77 | << target_url; |
| 78 | std::move(callback).Run({brillo::http::kDirectProxy}); |
| 79 | return; |
| 80 | } |
| 81 | pending_proxy_resolution_requests_[target_url].push_back(std::move(callback)); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 82 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 83 | |
| 84 | void ServerProxy::HandleStdinReadable() { |
| 85 | WorkerConfigs config; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 86 | if (!ReadProtobuf(GetStdinPipe(), &config)) { |
| 87 | LOG(ERROR) << "Error decoding protobuf configurations." << std::endl; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 88 | return; |
| 89 | } |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 90 | |
| 91 | if (config.has_credentials()) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 92 | const std::string username = UrlEncode(config.credentials().username()); |
| 93 | const std::string password = UrlEncode(config.credentials().password()); |
| 94 | credentials_ = base::JoinString({username.c_str(), password.c_str()}, ":"); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (config.has_listening_address()) { |
| 98 | if (listening_addr_ != 0) { |
| 99 | LOG(ERROR) |
| 100 | << "Failure to set configurations: listening port was already set." |
| 101 | << std::endl; |
| 102 | return; |
| 103 | } |
| 104 | listening_addr_ = config.listening_address().addr(); |
| 105 | listening_port_ = config.listening_address().port(); |
| 106 | CreateListeningSocket(); |
| 107 | } |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 108 | |
| 109 | if (config.has_proxy_resolution_reply()) { |
| 110 | std::list<std::string> proxies; |
| 111 | const ProxyResolutionReply& reply = config.proxy_resolution_reply(); |
| 112 | for (auto const& proxy : reply.proxy_servers()) |
| 113 | proxies.push_back(proxy); |
| 114 | |
| 115 | OnProxyResolved(reply.target_url(), proxies); |
| 116 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | bool ServerProxy::HandleSignal(const struct signalfd_siginfo& siginfo) { |
| 120 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 121 | std::move(quit_closure_)); |
| 122 | return true; |
| 123 | } |
| 124 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 125 | int ServerProxy::GetStdinPipe() { |
| 126 | return STDIN_FILENO; |
| 127 | } |
| 128 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 129 | int ServerProxy::GetStdoutPipe() { |
| 130 | return STDOUT_FILENO; |
| 131 | } |
| 132 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 133 | void ServerProxy::CreateListeningSocket() { |
| 134 | listening_fd_ = std::make_unique<arc_networkd::Socket>( |
| 135 | AF_INET, SOCK_STREAM | SOCK_NONBLOCK); |
| 136 | |
| 137 | struct sockaddr_in addr = {0}; |
| 138 | addr.sin_family = AF_INET; |
| 139 | addr.sin_port = htons(listening_port_); |
| 140 | addr.sin_addr.s_addr = listening_addr_; |
| 141 | if (!listening_fd_->Bind((const struct sockaddr*)&addr, sizeof(addr))) { |
| 142 | LOG(ERROR) << "Cannot bind source socket" << std::endl; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if (!listening_fd_->Listen(kMaxConn)) { |
| 147 | LOG(ERROR) << "Cannot listen on source socket." << std::endl; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 152 | listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept, |
| 153 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 156 | void ServerProxy::OnConnectionAccept() { |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 157 | struct sockaddr_storage client_src = {}; |
| 158 | socklen_t sockaddr_len = sizeof(client_src); |
| 159 | if (auto client_conn = |
| 160 | listening_fd_->Accept((struct sockaddr*)&client_src, &sockaddr_len)) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 161 | auto connect_job = std::make_unique<ProxyConnectJob>( |
| 162 | std::move(client_conn), credentials_, |
| 163 | base::BindOnce(&ServerProxy::ResolveProxy, base::Unretained(this)), |
| 164 | base::BindOnce(&ServerProxy::OnConnectionSetupFinished, |
| 165 | base::Unretained(this))); |
| 166 | if (connect_job->Start()) |
| 167 | pending_connect_jobs_[connect_job.get()] = std::move(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 168 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 169 | // Cleanup any defunct forwarders. |
| 170 | // TODO(acostinas, chromium:1064536) Monitor the client and server sockets |
| 171 | // and remove the corresponding SocketForwarder when a socket closes. |
| 172 | for (auto it = forwarders_.begin(); it != forwarders_.end(); ++it) { |
| 173 | if (!(*it)->IsRunning() && (*it)->HasBeenStarted()) |
| 174 | it = forwarders_.erase(it); |
| 175 | } |
| 176 | } |
| 177 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 178 | void ServerProxy::OnProxyResolved(const std::string& target_url, |
| 179 | const std::list<std::string>& proxy_servers) { |
| 180 | auto callbacks = std::move(pending_proxy_resolution_requests_[target_url]); |
| 181 | pending_proxy_resolution_requests_.erase(target_url); |
| 182 | |
| 183 | for (auto& callback : callbacks) |
| 184 | std::move(callback).Run(proxy_servers); |
| 185 | } |
| 186 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 187 | void ServerProxy::OnConnectionSetupFinished( |
| 188 | std::unique_ptr<arc_networkd::SocketForwarder> fwd, |
| 189 | ProxyConnectJob* connect_job) { |
| 190 | if (fwd) { |
| 191 | // The connection was set up successfully. |
| 192 | forwarders_.emplace_back(std::move(fwd)); |
| 193 | } |
| 194 | pending_connect_jobs_.erase(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 197 | } // namespace system_proxy |