blob: e4fc6deffeb99df60a653ab182b1529ce21ff0a8 [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
5#include "system-proxy/server_proxy.h"
6
7#include <iostream>
8#include <string>
9#include <utility>
10#include <vector>
11
Andreea Costinase45d54b2020-03-10 09:21:14 +010012#include <arc/network/socket.h>
13#include <arc/network/socket_forwarder.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010014#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 Costinase45d54b2020-03-10 09:21:14 +010019#include <base/strings/string_util.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010020#include <base/threading/thread.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010021#include <base/threading/thread_task_runner_handle.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010022#include <brillo/data_encoding.h>
23#include <brillo/http/http_transport.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010024
25#include "bindings/worker_common.pb.h"
26#include "system-proxy/protobuf_util.h"
Andreea Costinase45d54b2020-03-10 09:21:14 +010027#include "system-proxy/proxy_connect_job.h"
Andreea Costinas41e06442020-03-09 09:41:51 +010028
29namespace system_proxy {
30
Andreea Costinas44cefa22020-03-09 09:07:39 +010031namespace {
Andreea Costinase45d54b2020-03-10 09:21:14 +010032
33constexpr 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.
37std::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 Costinas44cefa22020-03-09 09:07:39 +010043} // namespace
44
Andreea Costinas41e06442020-03-09 09:41:51 +010045ServerProxy::ServerProxy(base::OnceClosure quit_closure)
Andreea Costinase45d54b2020-03-10 09:21:14 +010046 : quit_closure_(std::move(quit_closure)), weak_ptr_factory_(this) {}
47ServerProxy::~ServerProxy() = default;
Andreea Costinas41e06442020-03-09 09:41:51 +010048
49void ServerProxy::Init() {
50 // Start listening for input.
51 stdin_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinase45d54b2020-03-10 09:21:14 +010052 GetStdinPipe(), base::Bind(&ServerProxy::HandleStdinReadable,
53 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinas41e06442020-03-09 09:41:51 +010054
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 Costinase45d54b2020-03-10 09:21:14 +010064void 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 Costinas41e06442020-03-09 09:41:51 +010070
71void ServerProxy::HandleStdinReadable() {
72 WorkerConfigs config;
Andreea Costinas44cefa22020-03-09 09:07:39 +010073 if (!ReadProtobuf(GetStdinPipe(), &config)) {
74 LOG(ERROR) << "Error decoding protobuf configurations." << std::endl;
Andreea Costinas41e06442020-03-09 09:41:51 +010075 return;
76 }
Andreea Costinas44cefa22020-03-09 09:07:39 +010077
78 if (config.has_credentials()) {
Andreea Costinase45d54b2020-03-10 09:21:14 +010079 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 Costinas44cefa22020-03-09 09:07:39 +010082 }
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 Costinas41e06442020-03-09 09:41:51 +010095}
96
97bool 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 Costinas44cefa22020-03-09 09:07:39 +0100103int ServerProxy::GetStdinPipe() {
104 return STDIN_FILENO;
105}
106
107void 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 Costinase45d54b2020-03-10 09:21:14 +0100126 listening_fd_->fd(), base::BindRepeating(&ServerProxy::OnConnectionAccept,
127 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinas44cefa22020-03-09 09:07:39 +0100128}
129
Andreea Costinase45d54b2020-03-10 09:21:14 +0100130void ServerProxy::OnConnectionAccept() {
Andreea Costinas44cefa22020-03-09 09:07:39 +0100131 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 Costinase45d54b2020-03-10 09:21:14 +0100135 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 Costinas44cefa22020-03-09 09:07:39 +0100142 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100143
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
153void 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 Costinas44cefa22020-03-09 09:07:39 +0100161}
162
Andreea Costinas41e06442020-03-09 09:41:51 +0100163} // namespace system_proxy