blob: 4f7b8bd6d1340c4f38b2e652b6520d2373a0a292 [file] [log] [blame]
Andrew de los Reyes000d8952011-03-02 15:21:14 -08001// Copyright (c) 2011 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 "update_engine/chrome_browser_proxy_resolver.h"
6
7#include <map>
8#include <string>
9
Chris Sosafc661a12013-02-26 14:43:21 -080010#include <base/strings/string_tokenizer.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070011#include <base/strings/string_util.h>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080012#include <dbus/dbus-glib-lowlevel.h>
Alex Deymoc4acdf42014-05-28 21:07:10 -070013#include <dbus/dbus-glib.h>
Andrew de los Reyes000d8952011-03-02 15:21:14 -080014#include <google/protobuf/stubs/common.h>
15
16#include "update_engine/dbus_constants.h"
17#include "update_engine/utils.h"
18
19namespace chromeos_update_engine {
20
Chris Sosafc661a12013-02-26 14:43:21 -080021using base::StringTokenizer;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080022using google::protobuf::Closure;
Alex Deymoc4acdf42014-05-28 21:07:10 -070023using google::protobuf::NewPermanentCallback;
Andrew de los Reyes000d8952011-03-02 15:21:14 -080024using std::deque;
25using std::make_pair;
26using std::multimap;
27using std::pair;
28using std::string;
29
30#define LIB_CROS_PROXY_RESOLVE_NAME "ProxyResolved"
31#define LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE \
32 "org.chromium.UpdateEngineLibcrosProxyResolvedInterface"
33const char kLibCrosServiceName[] = "org.chromium.LibCrosService";
34const char kLibCrosServicePath[] = "/org/chromium/LibCrosService";
35const char kLibCrosServiceInterface[] = "org.chromium.LibCrosServiceInterface";
36const char kLibCrosServiceResolveNetworkProxyMethodName[] =
37 "ResolveNetworkProxy";
38const char kLibCrosProxyResolveName[] = LIB_CROS_PROXY_RESOLVE_NAME;
39const char kLibCrosProxyResolveSignalInterface[] =
40 LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE;
41const char kLibCrosProxyResolveSignalFilter[] = "type='signal', "
42 "interface='" LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE "', "
Andrew de los Reyes000d8952011-03-02 15:21:14 -080043 "member='" LIB_CROS_PROXY_RESOLVE_NAME "'";
44#undef LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE
45#undef LIB_CROS_PROXY_RESOLVE_NAME
46
47namespace {
Gilad Arnoldb752fb32014-03-03 12:23:39 -080048
Andrew de los Reyes000d8952011-03-02 15:21:14 -080049const int kTimeout = 5; // seconds
50
Andrew de los Reyes000d8952011-03-02 15:21:14 -080051} // namespace {}
52
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080053ChromeBrowserProxyResolver::ChromeBrowserProxyResolver(
54 DBusWrapperInterface* dbus)
Andrew de los Reyes000d8952011-03-02 15:21:14 -080055 : dbus_(dbus), proxy_(NULL), timeout_(kTimeout) {}
56
57bool ChromeBrowserProxyResolver::Init() {
Gilad Arnoldb752fb32014-03-03 12:23:39 -080058 if (proxy_)
59 return true; // Already initialized.
60
61 // Set up signal handler. Code lifted from libcros.
62 GError* g_error = NULL;
63 DBusGConnection* bus = dbus_->BusGet(DBUS_BUS_SYSTEM, &g_error);
64 TEST_AND_RETURN_FALSE(bus);
65 DBusConnection* connection = dbus_->ConnectionGetConnection(bus);
Andrew de los Reyes000d8952011-03-02 15:21:14 -080066 TEST_AND_RETURN_FALSE(connection);
Gilad Arnoldb752fb32014-03-03 12:23:39 -080067
68 DBusError dbus_error;
69 dbus_error_init(&dbus_error);
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080070 dbus_->DBusBusAddMatch(connection, kLibCrosProxyResolveSignalFilter,
Gilad Arnoldb752fb32014-03-03 12:23:39 -080071 &dbus_error);
72 TEST_AND_RETURN_FALSE(!dbus_error_is_set(&dbus_error));
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080073 TEST_AND_RETURN_FALSE(dbus_->DBusConnectionAddFilter(
Andrew de los Reyes000d8952011-03-02 15:21:14 -080074 connection,
75 &ChromeBrowserProxyResolver::StaticFilterMessage,
76 this,
77 NULL));
78
Gilad Arnoldb752fb32014-03-03 12:23:39 -080079 proxy_ = dbus_->ProxyNewForName(bus, kLibCrosServiceName, kLibCrosServicePath,
80 kLibCrosServiceInterface);
Andrew de los Reyes000d8952011-03-02 15:21:14 -080081 if (!proxy_) {
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080082 dbus_->DBusConnectionRemoveFilter(
Andrew de los Reyes000d8952011-03-02 15:21:14 -080083 connection,
84 &ChromeBrowserProxyResolver::StaticFilterMessage,
85 this);
86 }
87 TEST_AND_RETURN_FALSE(proxy_); // For the error log
88 return true;
89}
90
91ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() {
Gilad Arnoldb752fb32014-03-03 12:23:39 -080092 // Remove DBus connection filters and Kill proxy object.
93 if (proxy_) {
94 GError* gerror = NULL;
95 DBusGConnection* gbus = dbus_->BusGet(DBUS_BUS_SYSTEM, &gerror);
96 if (gbus) {
97 DBusConnection* connection = dbus_->ConnectionGetConnection(gbus);
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -080098 dbus_->DBusConnectionRemoveFilter(
Gilad Arnoldb752fb32014-03-03 12:23:39 -080099 connection,
100 &ChromeBrowserProxyResolver::StaticFilterMessage,
101 this);
102 }
103 dbus_->ProxyUnref(proxy_);
104 }
105
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800106 // Kill outstanding timers
107 for (TimeoutsMap::iterator it = timers_.begin(), e = timers_.end(); it != e;
108 ++it) {
109 g_source_destroy(it->second);
110 it->second = NULL;
111 }
112}
113
114bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url,
115 ProxiesResolvedFn callback,
116 void* data) {
117 GError* error = NULL;
Andrew de los Reyes518502a2011-03-14 14:19:39 -0700118 guint timeout = timeout_;
Gilad Arnold1877c392012-02-10 11:34:33 -0800119 if (proxy_) {
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800120 if (!dbus_->ProxyCall_3_0(proxy_,
121 kLibCrosServiceResolveNetworkProxyMethodName,
122 &error,
123 url.c_str(),
124 kLibCrosProxyResolveSignalInterface,
125 kLibCrosProxyResolveName)) {
Gilad Arnold1877c392012-02-10 11:34:33 -0800126
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800127 if (error) {
128 LOG(WARNING) << "dbus_g_proxy_call failed, continuing with no proxy: "
129 << utils::GetAndFreeGError(&error);
Gilad Arnold1877c392012-02-10 11:34:33 -0800130 } else {
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800131 LOG(WARNING) << "dbus_g_proxy_call failed with no error string, "
132 "continuing with no proxy.";
Gilad Arnold1877c392012-02-10 11:34:33 -0800133 }
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800134 timeout = 0;
135 }
Gilad Arnold1877c392012-02-10 11:34:33 -0800136 } else {
Brandon Philips962873e2013-03-26 13:58:51 -0700137 LOG(WARNING) << "dbus proxy object missing, continuing with no proxy.";
Andrew de los Reyes518502a2011-03-14 14:19:39 -0700138 timeout = 0;
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800139 }
Gilad Arnold1877c392012-02-10 11:34:33 -0800140
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800141 callbacks_.insert(make_pair(url, make_pair(callback, data)));
Alex Deymoc4acdf42014-05-28 21:07:10 -0700142 Closure* closure = NewPermanentCallback(
143 this,
144 &ChromeBrowserProxyResolver::HandleTimeout,
145 url);
Andrew de los Reyes518502a2011-03-14 14:19:39 -0700146 GSource* timer = g_timeout_source_new_seconds(timeout);
Alex Deymoc4acdf42014-05-28 21:07:10 -0700147 g_source_set_callback(
148 timer, utils::GlibRunClosure, closure, utils::GlibDestroyClosure);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800149 g_source_attach(timer, NULL);
150 timers_.insert(make_pair(url, timer));
151 return true;
152}
153
154DBusHandlerResult ChromeBrowserProxyResolver::FilterMessage(
155 DBusConnection* connection,
156 DBusMessage* message) {
157 // Code lifted from libcros.
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800158 if (!dbus_->DBusMessageIsSignal(message,
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800159 kLibCrosProxyResolveSignalInterface,
160 kLibCrosProxyResolveName)) {
161 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
162 }
163 // Get args
164 char* source_url = NULL;
165 char* proxy_list = NULL;
166 char* error = NULL;
167 DBusError arg_error;
168 dbus_error_init(&arg_error);
Gilad Arnold1b9d6ae2014-03-03 13:46:07 -0800169 if (!dbus_->DBusMessageGetArgs_3(message, &arg_error,
Gilad Arnoldb752fb32014-03-03 12:23:39 -0800170 &source_url,
171 &proxy_list,
172 &error)) {
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800173 LOG(ERROR) << "Error reading dbus signal.";
174 return DBUS_HANDLER_RESULT_HANDLED;
175 }
176 if (!source_url || !proxy_list) {
177 LOG(ERROR) << "Error getting url, proxy list from dbus signal.";
178 return DBUS_HANDLER_RESULT_HANDLED;
179 }
180 HandleReply(source_url, proxy_list);
181 return DBUS_HANDLER_RESULT_HANDLED;
182}
183
184bool ChromeBrowserProxyResolver::DeleteUrlState(
185 const string& source_url,
186 bool delete_timer,
187 pair<ProxiesResolvedFn, void*>* callback) {
188 {
189 CallbacksMap::iterator it = callbacks_.lower_bound(source_url);
190 TEST_AND_RETURN_FALSE(it != callbacks_.end());
191 TEST_AND_RETURN_FALSE(it->first == source_url);
192 if (callback)
193 *callback = it->second;
194 callbacks_.erase(it);
195 }
196 {
197 TimeoutsMap::iterator it = timers_.lower_bound(source_url);
198 TEST_AND_RETURN_FALSE(it != timers_.end());
199 TEST_AND_RETURN_FALSE(it->first == source_url);
200 if (delete_timer)
201 g_source_destroy(it->second);
202 timers_.erase(it);
203 }
204 return true;
205}
206
207void ChromeBrowserProxyResolver::HandleReply(const string& source_url,
208 const string& proxy_list) {
209 pair<ProxiesResolvedFn, void*> callback;
210 TEST_AND_RETURN(DeleteUrlState(source_url, true, &callback));
211 (*callback.first)(ParseProxyString(proxy_list), callback.second);
212}
213
214void ChromeBrowserProxyResolver::HandleTimeout(string source_url) {
215 LOG(INFO) << "Timeout handler called. Seems Chrome isn't responding.";
216 pair<ProxiesResolvedFn, void*> callback;
217 TEST_AND_RETURN(DeleteUrlState(source_url, false, &callback));
218 deque<string> proxies;
219 proxies.push_back(kNoProxy);
220 (*callback.first)(proxies, callback.second);
221}
222
223deque<string> ChromeBrowserProxyResolver::ParseProxyString(
224 const string& input) {
225 deque<string> ret;
226 // Some of this code taken from
227 // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and
228 // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc
229 StringTokenizer entry_tok(input, ";");
230 while (entry_tok.GetNext()) {
231 string token = entry_tok.token();
Ben Chan736fcb52014-05-21 18:28:22 -0700232 base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800233
234 // Start by finding the first space (if any).
235 std::string::iterator space;
236 for (space = token.begin(); space != token.end(); ++space) {
237 if (IsAsciiWhitespace(*space)) {
238 break;
239 }
240 }
241
242 string scheme = string(token.begin(), space);
243 StringToLowerASCII(&scheme);
244 // Chrome uses "socks" to mean socks4 and "proxy" to mean http.
245 if (scheme == "socks")
246 scheme += "4";
247 else if (scheme == "proxy")
248 scheme = "http";
249 else if (scheme != "https" &&
250 scheme != "socks4" &&
251 scheme != "socks5" &&
252 scheme != "direct")
253 continue; // Invalid proxy scheme
254
255 string host_and_port = string(space, token.end());
Ben Chan736fcb52014-05-21 18:28:22 -0700256 base::TrimWhitespaceASCII(host_and_port, base::TRIM_ALL, &host_and_port);
Andrew de los Reyes000d8952011-03-02 15:21:14 -0800257 if (scheme != "direct" && host_and_port.empty())
258 continue; // Must supply host/port when non-direct proxy used.
259 ret.push_back(scheme + "://" + host_and_port);
260 }
261 if (ret.empty() || *ret.rbegin() != kNoProxy)
262 ret.push_back(kNoProxy);
263 return ret;
264}
265
266} // namespace chromeos_update_engine