| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2011 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | #include "update_engine/chrome_browser_proxy_resolver.h" | 
|  | 18 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 19 | #include <deque> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 20 | #include <string> | 
|  | 21 |  | 
| Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 22 | #include <base/bind.h> | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 23 | #include <base/strings/string_tokenizer.h> | 
| Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 24 | #include <base/strings/string_util.h> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 27 |  | 
|  | 28 | namespace chromeos_update_engine { | 
|  | 29 |  | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 30 | using base::StringTokenizer; | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 31 | using base::TimeDelta; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 32 | using brillo::MessageLoop; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 33 | using std::deque; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 34 | using std::string; | 
|  | 35 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 36 | const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 37 | const char kLibCrosProxyResolveName[] = "ProxyResolved"; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 38 | const char kLibCrosProxyResolveSignalInterface[] = | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 39 | "org.chromium.UpdateEngineLibcrosProxyResolvedInterface"; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 40 |  | 
|  | 41 | namespace { | 
| Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 42 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 43 | const int kTimeout = 5;  // seconds | 
|  | 44 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 45 | }  // namespace | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 46 |  | 
| Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 47 | ChromeBrowserProxyResolver::ChromeBrowserProxyResolver( | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 48 | LibCrosProxy* libcros_proxy) | 
|  | 49 | : libcros_proxy_(libcros_proxy), timeout_(kTimeout) {} | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | bool ChromeBrowserProxyResolver::Init() { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 52 | libcros_proxy_->ue_proxy_resolved_interface() | 
|  | 53 | ->RegisterProxyResolvedSignalHandler( | 
|  | 54 | base::Bind(&ChromeBrowserProxyResolver::OnProxyResolvedSignal, | 
|  | 55 | base::Unretained(this)), | 
|  | 56 | base::Bind(&ChromeBrowserProxyResolver::OnSignalConnected, | 
|  | 57 | base::Unretained(this))); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 58 | return true; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 62 | // Kill outstanding timers. | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 63 | for (const auto& it : callbacks_) { | 
|  | 64 | MessageLoop::current()->CancelTask(it.second->timeout_id); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 65 | } | 
|  | 66 | } | 
|  | 67 |  | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 68 | ProxyRequestId ChromeBrowserProxyResolver::GetProxiesForUrl( | 
|  | 69 | const string& url, const ProxiesResolvedFn& callback) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 70 | int timeout = timeout_; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 71 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 72 | if (!libcros_proxy_->service_interface_proxy()->ResolveNetworkProxy( | 
|  | 73 | url.c_str(), | 
|  | 74 | kLibCrosProxyResolveSignalInterface, | 
|  | 75 | kLibCrosProxyResolveName, | 
|  | 76 | &error)) { | 
|  | 77 | LOG(WARNING) << "Can't resolve the proxy. Continuing with no proxy."; | 
| Andrew de los Reyes | 518502a | 2011-03-14 14:19:39 -0700 | [diff] [blame] | 78 | timeout = 0; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 79 | } | 
| Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 80 |  | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 81 | std::unique_ptr<ProxyRequestData> request(new ProxyRequestData()); | 
|  | 82 | request->callback = callback; | 
|  | 83 | ProxyRequestId timeout_id = MessageLoop::current()->PostDelayedTask( | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 84 | FROM_HERE, | 
|  | 85 | base::Bind(&ChromeBrowserProxyResolver::HandleTimeout, | 
|  | 86 | base::Unretained(this), | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 87 | url, | 
|  | 88 | request.get()), | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 89 | TimeDelta::FromSeconds(timeout)); | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 90 | request->timeout_id = timeout_id; | 
|  | 91 | callbacks_.emplace(url, std::move(request)); | 
|  | 92 |  | 
|  | 93 | // We re-use the timeout_id from the MessageLoop as the request id. | 
|  | 94 | return timeout_id; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 97 | bool ChromeBrowserProxyResolver::CancelProxyRequest(ProxyRequestId request) { | 
|  | 98 | // Finding the timeout_id in the callbacks_ structure requires a linear search | 
|  | 99 | // but we expect this operation to not be so frequent and to have just a few | 
|  | 100 | // proxy requests, so this should be fast enough. | 
|  | 101 | for (auto it = callbacks_.begin(); it != callbacks_.end(); ++it) { | 
|  | 102 | if (it->second->timeout_id == request) { | 
|  | 103 | MessageLoop::current()->CancelTask(request); | 
|  | 104 | callbacks_.erase(it); | 
|  | 105 | return true; | 
|  | 106 | } | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 107 | } | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 108 | return false; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | void ChromeBrowserProxyResolver::ProcessUrlResponse( | 
|  | 112 | const string& source_url, const deque<string>& proxies) { | 
|  | 113 | // Call all the occurrences of the |source_url| and erase them. | 
|  | 114 | auto lower_end = callbacks_.lower_bound(source_url); | 
|  | 115 | auto upper_end = callbacks_.upper_bound(source_url); | 
|  | 116 | for (auto it = lower_end; it != upper_end; ++it) { | 
|  | 117 | ProxyRequestData* request = it->second.get(); | 
|  | 118 | MessageLoop::current()->CancelTask(request->timeout_id); | 
|  | 119 | request->callback.Run(proxies); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 120 | } | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 121 | callbacks_.erase(lower_end, upper_end); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 124 | void ChromeBrowserProxyResolver::OnSignalConnected(const string& interface_name, | 
|  | 125 | const string& signal_name, | 
|  | 126 | bool successful) { | 
|  | 127 | if (!successful) { | 
|  | 128 | LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "." | 
|  | 129 | << signal_name; | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | void ChromeBrowserProxyResolver::OnProxyResolvedSignal( | 
|  | 134 | const string& source_url, | 
|  | 135 | const string& proxy_info, | 
|  | 136 | const string& error_message) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 137 | if (!error_message.empty()) { | 
|  | 138 | LOG(WARNING) << "ProxyResolved error: " << error_message; | 
|  | 139 | } | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 140 | ProcessUrlResponse(source_url, ParseProxyString(proxy_info)); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 141 | } | 
|  | 142 |  | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 143 | void ChromeBrowserProxyResolver::HandleTimeout(string source_url, | 
|  | 144 | ProxyRequestData* request) { | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 145 | LOG(INFO) << "Timeout handler called. Seems Chrome isn't responding."; | 
| Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 146 | // Mark the timer_id that produced this callback as invalid to prevent | 
|  | 147 | // canceling the timeout callback that already fired. | 
|  | 148 | request->timeout_id = MessageLoop::kTaskIdNull; | 
|  | 149 |  | 
|  | 150 | deque<string> proxies = {kNoProxy}; | 
|  | 151 | ProcessUrlResponse(source_url, proxies); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | deque<string> ChromeBrowserProxyResolver::ParseProxyString( | 
|  | 155 | const string& input) { | 
|  | 156 | deque<string> ret; | 
|  | 157 | // Some of this code taken from | 
|  | 158 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and | 
|  | 159 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc | 
|  | 160 | StringTokenizer entry_tok(input, ";"); | 
|  | 161 | while (entry_tok.GetNext()) { | 
|  | 162 | string token = entry_tok.token(); | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 163 | base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 164 |  | 
|  | 165 | // Start by finding the first space (if any). | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 166 | string::iterator space; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 167 | for (space = token.begin(); space != token.end(); ++space) { | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 168 | if (base::IsAsciiWhitespace(*space)) { | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 169 | break; | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 173 | string scheme = base::ToLowerASCII(string(token.begin(), space)); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 174 | // Chrome uses "socks" to mean socks4 and "proxy" to mean http. | 
|  | 175 | if (scheme == "socks") | 
|  | 176 | scheme += "4"; | 
|  | 177 | else if (scheme == "proxy") | 
|  | 178 | scheme = "http"; | 
|  | 179 | else if (scheme != "https" && | 
|  | 180 | scheme != "socks4" && | 
|  | 181 | scheme != "socks5" && | 
|  | 182 | scheme != "direct") | 
|  | 183 | continue;  // Invalid proxy scheme | 
|  | 184 |  | 
|  | 185 | string host_and_port = string(space, token.end()); | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 186 | 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] | 187 | if (scheme != "direct" && host_and_port.empty()) | 
|  | 188 | continue;  // Must supply host/port when non-direct proxy used. | 
|  | 189 | ret.push_back(scheme + "://" + host_and_port); | 
|  | 190 | } | 
|  | 191 | if (ret.empty() || *ret.rbegin() != kNoProxy) | 
|  | 192 | ret.push_back(kNoProxy); | 
|  | 193 | return ret; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | }  // namespace chromeos_update_engine |