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