blob: 66bc5dc4785ec35c99a670c047871f719a6c39c9 [file] [log] [blame]
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -08001// 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
13using std::string;
14using std::vector;
15
16namespace chromeos_update_engine {
17
18const char kSessionManagerService[] = "org.chromium.SessionManager";
19const char kSessionManagerPath[] = "/org/chromium/SessionManager";
20const char kSessionManagerInterface[] = "org.chromium.SessionManagerInterface";
21const char kSessionManagerRetrievePropertyMethod[] =
22 "RetrieveProperty";
23const char kSessionManagerProxySettingsKey[] = "cros.proxy.everywhere";
24
25bool ChromeProxyResolver::GetProxiesForUrl(
26 const std::string& url,
Andrew de los Reyes45168102010-11-22 11:13:50 -080027 std::deque<std::string>* out_proxies) {
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080028 // 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
39bool 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
59DBusGProxy* 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
75namespace {
76enum ProxyMode {
77 kProxyModeDirect = 0,
78 kProxyModeAutoDetect,
79 kProxyModePACScript,
80 kProxyModeSingle,
81 kProxyModeProxyPerScheme
82};
83} // namespace {}
84
85bool ChromeProxyResolver::GetProxiesForUrlWithSettings(
86 const string& url,
87 const string& json_settings,
Andrew de los Reyes45168102010-11-22 11:13:50 -080088 std::deque<std::string>* out_proxies) {
Andrew de los Reyes9cd120d2010-11-18 17:50:03 -080089 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
161bool 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