Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 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_proxy_resolver.h" |
| 6 | |
| 7 | #include <base/json/json_reader.h> |
| 8 | #include <base/scoped_ptr.h> |
| 9 | #include <base/values.h> |
| 10 | |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using std::vector; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | const char kSessionManagerService[] = "org.chromium.SessionManager"; |
| 19 | const char kSessionManagerPath[] = "/org/chromium/SessionManager"; |
| 20 | const char kSessionManagerInterface[] = "org.chromium.SessionManagerInterface"; |
| 21 | const char kSessionManagerRetrievePropertyMethod[] = |
| 22 | "RetrieveProperty"; |
| 23 | const char kSessionManagerProxySettingsKey[] = "cros.proxy.everywhere"; |
| 24 | |
| 25 | bool ChromeProxyResolver::GetProxiesForUrl( |
| 26 | const std::string& url, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 27 | std::deque<std::string>* out_proxies) { |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 28 | // First, query dbus for the currently stored settings |
| 29 | DBusGProxy* proxy = DbusProxy(); |
| 30 | TEST_AND_RETURN_FALSE(proxy); |
| 31 | string json_settings; |
| 32 | TEST_AND_RETURN_FALSE(GetJsonProxySettings(proxy, &json_settings)); |
| 33 | LOG(INFO) << "got settings:" << json_settings; |
| 34 | TEST_AND_RETURN_FALSE( |
| 35 | GetProxiesForUrlWithSettings(url, json_settings, out_proxies)); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool ChromeProxyResolver::GetJsonProxySettings(DBusGProxy* proxy, |
| 40 | std::string* out_json) { |
| 41 | gchar* value = NULL; |
| 42 | GArray* sig = NULL; |
| 43 | GError* error = NULL; |
| 44 | TEST_AND_RETURN_FALSE( |
| 45 | dbus_->ProxyCall(proxy, |
| 46 | kSessionManagerRetrievePropertyMethod, |
| 47 | &error, |
| 48 | G_TYPE_STRING, kSessionManagerProxySettingsKey, |
| 49 | G_TYPE_INVALID, |
| 50 | G_TYPE_STRING, &value, |
| 51 | DBUS_TYPE_G_UCHAR_ARRAY, &sig, |
| 52 | G_TYPE_INVALID)); |
| 53 | g_array_free(sig, false); |
| 54 | out_json->assign(value); |
| 55 | g_free(value); |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | DBusGProxy* ChromeProxyResolver::DbusProxy() { |
| 60 | GError* error = NULL; |
| 61 | DBusGConnection* bus = dbus_->BusGet(DBUS_BUS_SYSTEM, &error); |
| 62 | TEST_AND_RETURN_FALSE(bus); |
| 63 | DBusGProxy* proxy = dbus_->ProxyNewForNameOwner(bus, |
| 64 | kSessionManagerService, |
| 65 | kSessionManagerPath, |
| 66 | kSessionManagerInterface, |
| 67 | &error); |
| 68 | if (!proxy) { |
| 69 | LOG(ERROR) << "Error getting FlimFlam proxy: " |
| 70 | << utils::GetGErrorMessage(error); |
| 71 | } |
| 72 | return proxy; |
| 73 | } |
| 74 | |
| 75 | namespace { |
| 76 | enum ProxyMode { |
| 77 | kProxyModeDirect = 0, |
| 78 | kProxyModeAutoDetect, |
| 79 | kProxyModePACScript, |
| 80 | kProxyModeSingle, |
| 81 | kProxyModeProxyPerScheme |
| 82 | }; |
| 83 | } // namespace {} |
| 84 | |
| 85 | bool ChromeProxyResolver::GetProxiesForUrlWithSettings( |
| 86 | const string& url, |
| 87 | const string& json_settings, |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 88 | std::deque<std::string>* out_proxies) { |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 89 | base::JSONReader parser; |
| 90 | |
| 91 | scoped_ptr<Value> root( |
| 92 | parser.JsonToValue(json_settings, |
| 93 | true, // check root is obj/arr |
| 94 | false)); // false = disallow trailing comma |
| 95 | if (!root.get()) { |
| 96 | LOG(ERROR) << "Unable to parse \"" << json_settings << "\": " |
| 97 | << parser.GetErrorMessage(); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | TEST_AND_RETURN_FALSE(root->IsType(Value::TYPE_DICTIONARY)); |
| 102 | |
| 103 | DictionaryValue* root_dict = dynamic_cast<DictionaryValue*>(root.get()); |
| 104 | TEST_AND_RETURN_FALSE(root_dict); |
| 105 | int mode = -1; |
| 106 | TEST_AND_RETURN_FALSE(root_dict->GetInteger("mode", &mode)); |
| 107 | |
| 108 | LOG(INFO) << "proxy mode: " << mode; |
| 109 | if (mode != kProxyModeSingle && |
| 110 | mode != kProxyModeProxyPerScheme) { |
| 111 | LOG(INFO) << "unsupported proxy scheme"; |
| 112 | out_proxies->clear(); |
| 113 | out_proxies->push_back(kNoProxy); |
| 114 | return true; |
| 115 | } |
| 116 | if (mode == kProxyModeSingle) { |
| 117 | LOG(INFO) << "single proxy mode"; |
| 118 | string proxy_string; |
| 119 | TEST_AND_RETURN_FALSE(root_dict->GetString("single.server", &proxy_string)); |
| 120 | if (proxy_string.find("://") == string::npos) { |
| 121 | // missing protocol, assume http. |
| 122 | proxy_string = string("http://") + proxy_string; |
| 123 | } |
| 124 | out_proxies->clear(); |
| 125 | out_proxies->push_back(proxy_string); |
| 126 | LOG(INFO) << "single proxy: " << (*out_proxies)[0]; |
| 127 | out_proxies->push_back(kNoProxy); |
| 128 | return true; |
| 129 | } |
| 130 | // Proxy per scheme mode. |
| 131 | LOG(INFO) << "proxy per scheme mode"; |
| 132 | |
| 133 | // Find which scheme we are |
| 134 | bool url_is_http = utils::StringHasPrefix(url, "http://"); |
| 135 | if (!url_is_http) |
| 136 | TEST_AND_RETURN_FALSE(utils::StringHasPrefix(url, "https://")); |
| 137 | |
| 138 | // Using "proto_*" variables to refer to http or https |
| 139 | const string proto_path = url_is_http ? "http.server" : "https.server"; |
| 140 | const string socks_path = "socks.server"; |
| 141 | |
| 142 | out_proxies->clear(); |
| 143 | |
| 144 | string proto_server, socks_server; |
| 145 | if (root_dict->GetString(proto_path, &proto_server)) { |
| 146 | if (proto_server.find("://") == string::npos) { |
| 147 | // missing protocol, assume http. |
| 148 | proto_server = string("http://") + proto_server; |
| 149 | } |
| 150 | out_proxies->push_back(proto_server); |
| 151 | LOG(INFO) << "got http/https server: " << proto_server; |
| 152 | } |
| 153 | if (root_dict->GetString(socks_path, &socks_server)) { |
| 154 | out_proxies->push_back(socks_server); |
| 155 | LOG(INFO) << "got socks server: " << proto_server; |
| 156 | } |
| 157 | out_proxies->push_back(kNoProxy); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool ChromeProxyResolver::GetProxyType(const std::string& proxy, |
| 162 | curl_proxytype* out_type) { |
| 163 | if (utils::StringHasPrefix(proxy, "socks5://") || |
| 164 | utils::StringHasPrefix(proxy, "socks://")) { |
| 165 | *out_type = CURLPROXY_SOCKS5_HOSTNAME; |
| 166 | return true; |
| 167 | } |
| 168 | if (utils::StringHasPrefix(proxy, "socks4://")) { |
| 169 | *out_type = CURLPROXY_SOCKS4A; |
| 170 | return true; |
| 171 | } |
| 172 | if (utils::StringHasPrefix(proxy, "http://") || |
| 173 | utils::StringHasPrefix(proxy, "https://")) { |
| 174 | *out_type = CURLPROXY_HTTP; |
| 175 | return true; |
| 176 | } |
| 177 | if (utils::StringHasPrefix(proxy, kNoProxy)) { |
| 178 | // known failure case. don't log. |
| 179 | return false; |
| 180 | } |
| 181 | LOG(INFO) << "Unknown proxy type: " << proxy; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | } // namespace chromeos_update_engine |