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