| 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 <map> | 
|  | 21 | #include <string> | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 22 | #include <utility> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 23 |  | 
| Alex Vakulenko | 4906c1c | 2014-08-21 13:17:44 -0700 | [diff] [blame] | 24 | #include <base/bind.h> | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 25 | #include <base/strings/string_tokenizer.h> | 
| Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 26 | #include <base/strings/string_util.h> | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 27 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/utils.h" | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | namespace chromeos_update_engine { | 
|  | 31 |  | 
| Chris Sosa | fc661a1 | 2013-02-26 14:43:21 -0800 | [diff] [blame] | 32 | using base::StringTokenizer; | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 33 | using base::TimeDelta; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 34 | using brillo::MessageLoop; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 35 | using std::deque; | 
|  | 36 | using std::make_pair; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 37 | using std::pair; | 
|  | 38 | using std::string; | 
|  | 39 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 40 | const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 41 | const char kLibCrosProxyResolveName[] = "ProxyResolved"; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 42 | const char kLibCrosProxyResolveSignalInterface[] = | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 43 | "org.chromium.UpdateEngineLibcrosProxyResolvedInterface"; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | namespace { | 
| Gilad Arnold | b752fb3 | 2014-03-03 12:23:39 -0800 | [diff] [blame] | 46 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 47 | const int kTimeout = 5;  // seconds | 
|  | 48 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 49 | }  // namespace | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 50 |  | 
| Gilad Arnold | 1b9d6ae | 2014-03-03 13:46:07 -0800 | [diff] [blame] | 51 | ChromeBrowserProxyResolver::ChromeBrowserProxyResolver( | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 52 | LibCrosProxy* libcros_proxy) | 
|  | 53 | : libcros_proxy_(libcros_proxy), timeout_(kTimeout) {} | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | bool ChromeBrowserProxyResolver::Init() { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 56 | libcros_proxy_->ue_proxy_resolved_interface() | 
|  | 57 | ->RegisterProxyResolvedSignalHandler( | 
|  | 58 | base::Bind(&ChromeBrowserProxyResolver::OnProxyResolvedSignal, | 
|  | 59 | base::Unretained(this)), | 
|  | 60 | base::Bind(&ChromeBrowserProxyResolver::OnSignalConnected, | 
|  | 61 | base::Unretained(this))); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 62 | return true; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 66 | // Kill outstanding timers. | 
| Alex Deymo | 020600d | 2014-11-05 21:05:55 -0800 | [diff] [blame] | 67 | for (auto& timer : timers_) { | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 68 | MessageLoop::current()->CancelTask(timer.second); | 
|  | 69 | timer.second = MessageLoop::kTaskIdNull; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 70 | } | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url, | 
|  | 74 | ProxiesResolvedFn callback, | 
|  | 75 | void* data) { | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 76 | int timeout = timeout_; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 77 | brillo::ErrorPtr error; | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 78 | if (!libcros_proxy_->service_interface_proxy()->ResolveNetworkProxy( | 
|  | 79 | url.c_str(), | 
|  | 80 | kLibCrosProxyResolveSignalInterface, | 
|  | 81 | kLibCrosProxyResolveName, | 
|  | 82 | &error)) { | 
|  | 83 | 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] | 84 | timeout = 0; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 85 | } | 
| Gilad Arnold | 1877c39 | 2012-02-10 11:34:33 -0800 | [diff] [blame] | 86 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 87 | callbacks_.insert(make_pair(url, make_pair(callback, data))); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 88 | MessageLoop::TaskId timer = MessageLoop::current()->PostDelayedTask( | 
|  | 89 | FROM_HERE, | 
|  | 90 | base::Bind(&ChromeBrowserProxyResolver::HandleTimeout, | 
|  | 91 | base::Unretained(this), | 
|  | 92 | url), | 
|  | 93 | TimeDelta::FromSeconds(timeout)); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 94 | timers_.insert(make_pair(url, timer)); | 
|  | 95 | return true; | 
|  | 96 | } | 
|  | 97 |  | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 98 | bool ChromeBrowserProxyResolver::DeleteUrlState( | 
|  | 99 | const string& source_url, | 
|  | 100 | bool delete_timer, | 
|  | 101 | pair<ProxiesResolvedFn, void*>* callback) { | 
|  | 102 | { | 
|  | 103 | CallbacksMap::iterator it = callbacks_.lower_bound(source_url); | 
|  | 104 | TEST_AND_RETURN_FALSE(it != callbacks_.end()); | 
|  | 105 | TEST_AND_RETURN_FALSE(it->first == source_url); | 
|  | 106 | if (callback) | 
|  | 107 | *callback = it->second; | 
|  | 108 | callbacks_.erase(it); | 
|  | 109 | } | 
|  | 110 | { | 
|  | 111 | TimeoutsMap::iterator it = timers_.lower_bound(source_url); | 
|  | 112 | TEST_AND_RETURN_FALSE(it != timers_.end()); | 
|  | 113 | TEST_AND_RETURN_FALSE(it->first == source_url); | 
|  | 114 | if (delete_timer) | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 115 | MessageLoop::current()->CancelTask(it->second); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 116 | timers_.erase(it); | 
|  | 117 | } | 
|  | 118 | return true; | 
|  | 119 | } | 
|  | 120 |  | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 121 | void ChromeBrowserProxyResolver::OnSignalConnected(const string& interface_name, | 
|  | 122 | const string& signal_name, | 
|  | 123 | bool successful) { | 
|  | 124 | if (!successful) { | 
|  | 125 | LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "." | 
|  | 126 | << signal_name; | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | void ChromeBrowserProxyResolver::OnProxyResolvedSignal( | 
|  | 131 | const string& source_url, | 
|  | 132 | const string& proxy_info, | 
|  | 133 | const string& error_message) { | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 134 | pair<ProxiesResolvedFn, void*> callback; | 
|  | 135 | TEST_AND_RETURN(DeleteUrlState(source_url, true, &callback)); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 136 | if (!error_message.empty()) { | 
|  | 137 | LOG(WARNING) << "ProxyResolved error: " << error_message; | 
|  | 138 | } | 
|  | 139 | (*callback.first)(ParseProxyString(proxy_info), callback.second); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
|  | 142 | void ChromeBrowserProxyResolver::HandleTimeout(string source_url) { | 
|  | 143 | LOG(INFO) << "Timeout handler called. Seems Chrome isn't responding."; | 
|  | 144 | pair<ProxiesResolvedFn, void*> callback; | 
|  | 145 | TEST_AND_RETURN(DeleteUrlState(source_url, false, &callback)); | 
|  | 146 | deque<string> proxies; | 
|  | 147 | proxies.push_back(kNoProxy); | 
|  | 148 | (*callback.first)(proxies, callback.second); | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | deque<string> ChromeBrowserProxyResolver::ParseProxyString( | 
|  | 152 | const string& input) { | 
|  | 153 | deque<string> ret; | 
|  | 154 | // Some of this code taken from | 
|  | 155 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and | 
|  | 156 | // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc | 
|  | 157 | StringTokenizer entry_tok(input, ";"); | 
|  | 158 | while (entry_tok.GetNext()) { | 
|  | 159 | string token = entry_tok.token(); | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 160 | base::TrimWhitespaceASCII(token, base::TRIM_ALL, &token); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 161 |  | 
|  | 162 | // Start by finding the first space (if any). | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 163 | string::iterator space; | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 164 | for (space = token.begin(); space != token.end(); ++space) { | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 165 | if (base::IsAsciiWhitespace(*space)) { | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 166 | break; | 
|  | 167 | } | 
|  | 168 | } | 
|  | 169 |  | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 170 | string scheme = base::ToLowerASCII(string(token.begin(), space)); | 
| Andrew de los Reyes | 000d895 | 2011-03-02 15:21:14 -0800 | [diff] [blame] | 171 | // Chrome uses "socks" to mean socks4 and "proxy" to mean http. | 
|  | 172 | if (scheme == "socks") | 
|  | 173 | scheme += "4"; | 
|  | 174 | else if (scheme == "proxy") | 
|  | 175 | scheme = "http"; | 
|  | 176 | else if (scheme != "https" && | 
|  | 177 | scheme != "socks4" && | 
|  | 178 | scheme != "socks5" && | 
|  | 179 | scheme != "direct") | 
|  | 180 | continue;  // Invalid proxy scheme | 
|  | 181 |  | 
|  | 182 | string host_and_port = string(space, token.end()); | 
| Ben Chan | 736fcb5 | 2014-05-21 18:28:22 -0700 | [diff] [blame] | 183 | 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] | 184 | if (scheme != "direct" && host_and_port.empty()) | 
|  | 185 | continue;  // Must supply host/port when non-direct proxy used. | 
|  | 186 | ret.push_back(scheme + "://" + host_and_port); | 
|  | 187 | } | 
|  | 188 | if (ret.empty() || *ret.rbegin() != kNoProxy) | 
|  | 189 | ret.push_back(kNoProxy); | 
|  | 190 | return ret; | 
|  | 191 | } | 
|  | 192 |  | 
|  | 193 | }  // namespace chromeos_update_engine |