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) { |
| 66 | // TODO(acostinas, crbug.com/1042626) Ask Chrome to resolve proxy for |
| 67 | // |target_url|. |
| 68 | std::move(callback).Run({brillo::http::kDirectProxy}); |
| 69 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 70 | |
| 71 | void ServerProxy::HandleStdinReadable() { |
| 72 | WorkerConfigs config; |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 73 | if (!ReadProtobuf(GetStdinPipe(), &config)) { |
| 74 | LOG(ERROR) << "Error decoding protobuf configurations." << std::endl; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 75 | return; |
| 76 | } |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 77 | |
| 78 | if (config.has_credentials()) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame^] | 79 | const std::string username = UrlEncode(config.credentials().username()); |
| 80 | const std::string password = UrlEncode(config.credentials().password()); |
| 81 | credentials_ = base::JoinString({username.c_str(), password.c_str()}, ":"); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (config.has_listening_address()) { |
| 85 | if (listening_addr_ != 0) { |
| 86 | LOG(ERROR) |
| 87 | << "Failure to set configurations: listening port was already set." |
| 88 | << std::endl; |
| 89 | return; |
| 90 | } |
| 91 | listening_addr_ = config.listening_address().addr(); |
| 92 | listening_port_ = config.listening_address().port(); |
| 93 | CreateListeningSocket(); |
| 94 | } |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool ServerProxy::HandleSignal(const struct signalfd_siginfo& siginfo) { |
| 98 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 99 | std::move(quit_closure_)); |
| 100 | return true; |
| 101 | } |
| 102 | |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 103 | int ServerProxy::GetStdinPipe() { |
| 104 | return STDIN_FILENO; |
| 105 | } |
| 106 | |
| 107 | void ServerProxy::CreateListeningSocket() { |
| 108 | listening_fd_ = std::make_unique<arc_networkd::Socket>( |
| 109 | AF_INET, SOCK_STREAM | SOCK_NONBLOCK); |
| 110 | |
| 111 | struct sockaddr_in addr = {0}; |
| 112 | addr.sin_family = AF_INET; |
| 113 | addr.sin_port = htons(listening_port_); |
| 114 | addr.sin_addr.s_addr = listening_addr_; |
| 115 | if (!listening_fd_->Bind((const struct sockaddr*)&addr, sizeof(addr))) { |
| 116 | LOG(ERROR) << "Cannot bind source socket" << std::endl; |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (!listening_fd_->Listen(kMaxConn)) { |
| 121 | LOG(ERROR) << "Cannot listen on source socket." << std::endl; |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame^] | 126 | listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept, |
| 127 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame^] | 130 | void ServerProxy::OnConnectionAccept() { |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 131 | struct sockaddr_storage client_src = {}; |
| 132 | socklen_t sockaddr_len = sizeof(client_src); |
| 133 | if (auto client_conn = |
| 134 | listening_fd_->Accept((struct sockaddr*)&client_src, &sockaddr_len)) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame^] | 135 | auto connect_job = std::make_unique<ProxyConnectJob>( |
| 136 | std::move(client_conn), credentials_, |
| 137 | base::BindOnce(&ServerProxy::ResolveProxy, base::Unretained(this)), |
| 138 | base::BindOnce(&ServerProxy::OnConnectionSetupFinished, |
| 139 | base::Unretained(this))); |
| 140 | if (connect_job->Start()) |
| 141 | pending_connect_jobs_[connect_job.get()] = std::move(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 142 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame^] | 143 | |
| 144 | // Cleanup any defunct forwarders. |
| 145 | // TODO(acostinas, chromium:1064536) Monitor the client and server sockets |
| 146 | // and remove the corresponding SocketForwarder when a socket closes. |
| 147 | for (auto it = forwarders_.begin(); it != forwarders_.end(); ++it) { |
| 148 | if (!(*it)->IsRunning() && (*it)->HasBeenStarted()) |
| 149 | it = forwarders_.erase(it); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ServerProxy::OnConnectionSetupFinished( |
| 154 | std::unique_ptr<arc_networkd::SocketForwarder> fwd, |
| 155 | ProxyConnectJob* connect_job) { |
| 156 | if (fwd) { |
| 157 | // The connection was set up successfully. |
| 158 | forwarders_.emplace_back(std::move(fwd)); |
| 159 | } |
| 160 | pending_connect_jobs_.erase(connect_job); |
Andreea Costinas | 44cefa2 | 2020-03-09 09:07:39 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 163 | } // namespace system_proxy |