Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 1 | // 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 Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 10 | #include <base/strings/string_tokenizer.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame^] | 11 | #include <base/strings/string_util.h> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 12 | #include <dbus/dbus-glib-lowlevel.h> |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame^] | 13 | #include <dbus/dbus-glib.h> |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 14 | #include <google/protobuf/stubs/common.h> |
| 15 | |
| 16 | #include "update_engine/dbus_constants.h" |
| 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 21 | using base::StringTokenizer; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 22 | using google::protobuf::Closure; |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame^] | 23 | using google::protobuf::NewPermanentCallback; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 24 | using std::deque; |
| 25 | using std::make_pair; |
| 26 | using std::multimap; |
| 27 | using std::pair; |
| 28 | using std::string; |
| 29 | |
| 30 | #define LIB_CROS_PROXY_RESOLVE_NAME "ProxyResolved" |
| 31 | #define LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE \ |
| 32 | "org.chromium.UpdateEngineLibcrosProxyResolvedInterface" |
| 33 | const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; |
| 34 | const char kLibCrosServicePath[] = "/org/chromium/LibCrosService"; |
| 35 | const char kLibCrosServiceInterface[] = "org.chromium.LibCrosServiceInterface"; |
| 36 | const char kLibCrosServiceResolveNetworkProxyMethodName[] = |
| 37 | "ResolveNetworkProxy"; |
| 38 | const char kLibCrosProxyResolveName[] = LIB_CROS_PROXY_RESOLVE_NAME; |
| 39 | const char kLibCrosProxyResolveSignalInterface[] = |
| 40 | LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE; |
| 41 | const char kLibCrosProxyResolveSignalFilter[] = "type='signal', " |
| 42 | "interface='" LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE "', " |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 43 | "member='" LIB_CROS_PROXY_RESOLVE_NAME "'"; |
| 44 | #undef LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE |
| 45 | #undef LIB_CROS_PROXY_RESOLVE_NAME |
| 46 | |
| 47 | namespace { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 48 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 49 | const int kTimeout = 5; // seconds |
| 50 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 51 | } // namespace {} |
| 52 | |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 53 | ChromeBrowserProxyResolver::ChromeBrowserProxyResolver( |
| 54 | DBusWrapperInterface* dbus) |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 55 | : dbus_(dbus), proxy_(NULL), timeout_(kTimeout) {} |
| 56 | |
| 57 | bool ChromeBrowserProxyResolver::Init() { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 58 | 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 Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 66 | TEST_AND_RETURN_FALSE(connection); |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 67 | |
| 68 | DBusError dbus_error; |
| 69 | dbus_error_init(&dbus_error); |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 70 | dbus_->DBusBusAddMatch(connection, kLibCrosProxyResolveSignalFilter, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 71 | &dbus_error); |
| 72 | TEST_AND_RETURN_FALSE(!dbus_error_is_set(&dbus_error)); |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 73 | TEST_AND_RETURN_FALSE(dbus_->DBusConnectionAddFilter( |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 74 | connection, |
| 75 | &ChromeBrowserProxyResolver::StaticFilterMessage, |
| 76 | this, |
| 77 | NULL)); |
| 78 | |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 79 | proxy_ = dbus_->ProxyNewForName(bus, kLibCrosServiceName, kLibCrosServicePath, |
| 80 | kLibCrosServiceInterface); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 81 | if (!proxy_) { |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 82 | dbus_->DBusConnectionRemoveFilter( |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 83 | connection, |
| 84 | &ChromeBrowserProxyResolver::StaticFilterMessage, |
| 85 | this); |
| 86 | } |
| 87 | TEST_AND_RETURN_FALSE(proxy_); // For the error log |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 92 | // 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 Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 98 | dbus_->DBusConnectionRemoveFilter( |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 99 | connection, |
| 100 | &ChromeBrowserProxyResolver::StaticFilterMessage, |
| 101 | this); |
| 102 | } |
| 103 | dbus_->ProxyUnref(proxy_); |
| 104 | } |
| 105 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 106 | // 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 | |
| 114 | bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url, |
| 115 | ProxiesResolvedFn callback, |
| 116 | void* data) { |
| 117 | GError* error = NULL; |
Andrew de los Reyes | 518502a | 2011-03-14 14:19:39 -0700 | [diff] [blame] | 118 | guint timeout = timeout_; |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 119 | if (proxy_) { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 120 | if (!dbus_->ProxyCall_3_0(proxy_, |
| 121 | kLibCrosServiceResolveNetworkProxyMethodName, |
| 122 | &error, |
| 123 | url.c_str(), |
| 124 | kLibCrosProxyResolveSignalInterface, |
| 125 | kLibCrosProxyResolveName)) { |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 126 | |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 127 | if (error) { |
| 128 | LOG(WARNING) << "dbus_g_proxy_call failed, continuing with no proxy: " |
| 129 | << utils::GetAndFreeGError(&error); |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 130 | } else { |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 131 | LOG(WARNING) << "dbus_g_proxy_call failed with no error string, " |
| 132 | "continuing with no proxy."; |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 133 | } |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 134 | timeout = 0; |
| 135 | } |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 136 | } else { |
Brandon Philips | 962873e | 2013-03-26 13:58:51 -0700 | [diff] [blame] | 137 | LOG(WARNING) << "dbus proxy object missing, continuing with no proxy."; |
Andrew de los Reyes | 518502a | 2011-03-14 14:19:39 -0700 | [diff] [blame] | 138 | timeout = 0; |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 139 | } |
Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 140 | |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 141 | callbacks_.insert(make_pair(url, make_pair(callback, data))); |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame^] | 142 | Closure* closure = NewPermanentCallback( |
| 143 | this, |
| 144 | &ChromeBrowserProxyResolver::HandleTimeout, |
| 145 | url); |
Andrew de los Reyes | 518502a | 2011-03-14 14:19:39 -0700 | [diff] [blame] | 146 | GSource* timer = g_timeout_source_new_seconds(timeout); |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame^] | 147 | g_source_set_callback( |
| 148 | timer, utils::GlibRunClosure, closure, utils::GlibDestroyClosure); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 149 | g_source_attach(timer, NULL); |
| 150 | timers_.insert(make_pair(url, timer)); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | DBusHandlerResult ChromeBrowserProxyResolver::FilterMessage( |
| 155 | DBusConnection* connection, |
| 156 | DBusMessage* message) { |
| 157 | // Code lifted from libcros. |
Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 158 | if (!dbus_->DBusMessageIsSignal(message, |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 159 | 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 Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 169 | if (!dbus_->DBusMessageGetArgs_3(message, &arg_error, |
Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 170 | &source_url, |
| 171 | &proxy_list, |
| 172 | &error)) { |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 173 | 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 | |
| 184 | bool 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 | |
| 207 | void 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 | |
| 214 | void 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 | |
| 223 | deque<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 Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 232 | base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 233 | |
| 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 Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 256 | base::TrimWhitespaceASCII(host_and_port, base::TRIM_ALL, &host_and_port); |
Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 257 | 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 |