OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 <string.h> | |
6 #include <unistd.h> | |
7 | |
8 #include <deque> | |
9 #include <string> | |
10 | |
11 #include <dbus/dbus-glib-lowlevel.h> | |
12 #include <glib.h> | |
13 | |
14 #include "base/command_line.h" | |
15 #include "base/file_util.h" | |
16 #include "base/json/json_reader.h" | |
17 #include "base/string_tokenizer.h" | |
18 #include "base/string_util.h" | |
19 #include "base/values.h" | |
20 #include "chromeos/dbus/dbus.h" | |
21 #include "chromeos/syslog_logging.h" | |
22 #include "gflags/gflags.h" | |
23 | |
24 | |
25 #pragma GCC diagnostic ignored "-Wstrict-aliasing" | |
26 DEFINE_bool(force_browser_proxies, false, "Force printing the list of " | |
27 "browser proxies"); | |
28 DEFINE_bool(force_session_proxies, false, "Force printing the list of " | |
29 "session proxies"); | |
30 DEFINE_bool(quiet, false, "Only print the proxies"); | |
31 DEFINE_bool(verbose, false, "Print additional messages even " | |
32 "when not run from a TTY"); | |
33 #pragma GCC diagnostic error "-Wstrict-aliasing" | |
34 | |
35 | |
36 const char kNoProxy[] = "direct://"; | |
37 | |
38 // For ShowBrowserProxies() | |
39 const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; | |
40 const char kLibCrosServicePath[] = "/org/chromium/LibCrosService"; | |
41 const char kLibCrosServiceInterface[] = "org.chromium.LibCrosServiceInterface"; | |
kmixter1
2011/04/06 04:28:53
Sort these if there is no specific sequence?
Michael Krebs
2011/04/12 00:53:38
They're in top-down order. You contact the servic
kmixter1
2011/04/12 01:17:41
I'd go for abc order, to simplify adding new const
Michael Krebs
2011/04/13 02:40:24
Done.
| |
42 const char kLibCrosServiceResolveNetworkProxyMethodName[] = | |
43 "ResolveNetworkProxy"; | |
44 const char kLibCrosProxyResolveName[] = "ProxyResolved"; | |
45 const char kLibCrosProxyResolveSignalInterface[] = | |
46 "org.chromium.CrashReporterLibcrosProxyResolvedInterface"; | |
47 | |
48 // For ShowSessionProxies() | |
49 const char kSessionManagerService[] = "org.chromium.SessionManager"; | |
50 const char kSessionManagerPath[] = "/org/chromium/SessionManager"; | |
51 const char kSessionManagerInterface[] = "org.chromium.SessionManagerInterface"; | |
52 const char kSessionManagerRetrievePropertyMethod[] = "RetrieveProperty"; | |
53 const char kSessionManagerProxySettingsKey[] = "cros.proxy.everywhere"; | |
54 | |
55 | |
56 // Number of seconds to wait for browser to send us a signal | |
57 const int kBrowserTimeout = 5; | |
58 | |
59 | |
60 static const char* GetGErrorMessage(const GError* error) { | |
kmixter1
2011/04/06 04:28:53
consistently use space-star not star-space here an
Michael Krebs
2011/04/12 00:53:38
Done.
| |
61 if (!error) | |
62 return "Unknown error."; | |
63 return error->message; | |
64 } | |
65 | |
66 | |
67 // Copied from src/update_engine/chrome_browser_proxy_resolver.cc | |
kmixter1
2011/04/12 01:17:41
1 line between functions
Michael Krebs
2011/04/13 02:40:24
Done.
| |
68 std::deque<std::string> ParseProxyString(const std::string &input) { | |
69 std::deque<std::string> ret; | |
70 // Some of this code taken from | |
71 // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and | |
72 // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc | |
73 StringTokenizer entry_tok(input, ";"); | |
74 while (entry_tok.GetNext()) { | |
75 std::string token = entry_tok.token(); | |
76 TrimWhitespaceASCII(token, TRIM_ALL, &token); | |
77 | |
78 // Start by finding the first space (if any). | |
79 std::string::iterator space; | |
80 for (space = token.begin(); space != token.end(); ++space) { | |
81 if (IsAsciiWhitespace(*space)) { | |
82 break; | |
83 } | |
84 } | |
85 | |
86 std::string scheme = std::string(token.begin(), space); | |
87 StringToLowerASCII(&scheme); | |
88 // Chrome uses "socks" to mean socks4 and "proxy" to mean http. | |
89 if (scheme == "socks") | |
90 scheme += "4"; | |
91 else if (scheme == "proxy") | |
92 scheme = "http"; | |
93 else if (scheme != "https" && | |
94 scheme != "socks4" && | |
95 scheme != "socks5" && | |
96 scheme != "direct") | |
97 continue; // Invalid proxy scheme | |
98 | |
99 std::string host_and_port = std::string(space, token.end()); | |
100 TrimWhitespaceASCII(host_and_port, TRIM_ALL, &host_and_port); | |
101 if (scheme != "direct" && host_and_port.empty()) | |
102 continue; // Must supply host/port when non-direct proxy used. | |
103 ret.push_back(scheme + "://" + host_and_port); | |
104 } | |
105 if (ret.empty() || *ret.rbegin() != kNoProxy) | |
106 ret.push_back(kNoProxy); | |
107 return ret; | |
108 } | |
109 | |
110 class BrowserProxyResolvedSignalWatcher : public chromeos::dbus::SignalWatcher { | |
kmixter1
2011/04/12 01:17:41
Add comment describing purpose of class.
Michael Krebs
2011/04/13 02:40:24
Done.
| |
111 public: | |
112 explicit BrowserProxyResolvedSignalWatcher(GMainLoop *main_loop, | |
113 std::deque<std::string> *proxies) | |
114 : main_loop_(main_loop), proxies_(proxies) { } | |
115 | |
116 virtual void OnSignal(DBusMessage *message) { | |
117 // Get args | |
118 char *source_url = NULL; | |
119 char *proxy_list = NULL; | |
120 char *error = NULL; | |
121 DBusError arg_error; | |
122 dbus_error_init(&arg_error); | |
123 if (!dbus_message_get_args(message, &arg_error, | |
124 DBUS_TYPE_STRING, &source_url, | |
125 DBUS_TYPE_STRING, &proxy_list, | |
126 DBUS_TYPE_STRING, &error, | |
127 DBUS_TYPE_INVALID)) { | |
128 LOG(ERROR) << "Error reading D-Bus signal"; | |
129 return; | |
130 } | |
131 if (!source_url || !proxy_list) { | |
132 LOG(ERROR) << "Error getting url, proxy list from D-Bus signal"; | |
133 return; | |
134 } | |
135 | |
136 const std::deque<std::string> &proxies = ParseProxyString(proxy_list); | |
137 for (std::deque<std::string>::const_iterator it = proxies.begin(); | |
138 it != proxies.end(); ++it) { | |
139 LOG(INFO) << "Found proxy via browser signal: " << (*it).c_str(); | |
140 proxies_->push_back(*it); | |
141 } | |
142 | |
143 g_main_loop_quit(main_loop_); | |
144 } | |
145 | |
146 private: | |
147 GMainLoop *main_loop_; | |
148 std::deque<std::string> *proxies_; | |
149 }; | |
150 | |
151 static gboolean HandleBrowserTimeout(void *data) { | |
152 GMainLoop *main_loop = reinterpret_cast<GMainLoop *>(data); | |
153 LOG(ERROR) << "Timeout while waiting for browser to resolve proxy"; | |
154 g_main_loop_quit(main_loop); | |
155 return false; // only call once | |
156 } | |
157 | |
158 static bool ShowBrowserProxies(const char *url) { | |
159 GMainLoop *main_loop = g_main_loop_new(NULL, false); | |
160 | |
161 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection(); | |
162 if (!dbus.HasConnection()) { | |
163 LOG(ERROR) << "Error connecting to system D-Bus"; | |
164 return false; | |
165 } | |
166 chromeos::dbus::Proxy browser_proxy(dbus, | |
167 kLibCrosServiceName, | |
168 kLibCrosServicePath, | |
169 kLibCrosServiceInterface); | |
170 if (!browser_proxy) { | |
171 LOG(ERROR) << "Error creating D-Bus proxy to interface " | |
172 << "'" << kLibCrosServiceName << "'"; | |
173 return false; | |
174 } | |
175 | |
176 // Watch for a proxy-resolved signal sent to us | |
177 std::deque<std::string> proxies; | |
178 BrowserProxyResolvedSignalWatcher proxy_resolver(main_loop, &proxies); | |
179 proxy_resolver.StartMonitoring(kLibCrosProxyResolveSignalInterface, | |
180 kLibCrosProxyResolveName); | |
181 | |
182 // Request the proxies for our URL. The answer is sent to us via a | |
183 // proxy-resolved signal. | |
184 GError *gerror = NULL; | |
185 if (!dbus_g_proxy_call(browser_proxy.gproxy(), | |
186 kLibCrosServiceResolveNetworkProxyMethodName, | |
187 &gerror, | |
188 G_TYPE_STRING, url, | |
189 G_TYPE_STRING, kLibCrosProxyResolveSignalInterface, | |
190 G_TYPE_STRING, kLibCrosProxyResolveName, | |
191 G_TYPE_INVALID, G_TYPE_INVALID)) { | |
192 LOG(ERROR) << "Error performing D-Bus proxy call " | |
193 << "'" << kLibCrosServiceResolveNetworkProxyMethodName << "'" | |
194 << ": " << GetGErrorMessage(gerror); | |
195 return false; | |
196 } | |
197 | |
198 // Setup a timeout in case the browser doesn't respond with our signal | |
199 g_timeout_add_seconds(kBrowserTimeout, &HandleBrowserTimeout, main_loop); | |
200 | |
201 // Loop until we either get the proxy-resolved signal, or until the | |
202 // timeout is reached. | |
203 g_main_loop_run(main_loop); | |
204 | |
205 for (std::deque<std::string>::const_iterator it = proxies.begin(); | |
206 it != proxies.end(); ++it) { | |
207 g_print("%s\n", (*it).c_str()); | |
208 } | |
209 | |
210 return true; | |
211 } | |
212 | |
213 | |
214 namespace { | |
215 enum ProxyMode { | |
216 kProxyModeDirect = 0, | |
217 kProxyModeAutoDetect = 1, | |
218 kProxyModePACScript = 2, | |
219 kProxyModeSingle = 3, | |
220 kProxyModeProxyPerScheme = 4 | |
221 }; | |
222 } // namespace {} | |
223 | |
224 // Returns the scheme portion of a URL. | |
225 const char *GetUrlScheme(const char *url) { | |
226 const char *ptr; | |
227 // An undefined URL or scheme defaults to "http://". | |
228 if (!url || !(ptr = strstr(url, "://"))) | |
229 return strdup("http"); | |
230 return strndup(url, ptr - url); | |
231 } | |
232 | |
233 const char *GetProtocolPathFromScheme(const char *scheme) { | |
234 if (!strcasecmp(scheme, "http")) | |
235 return strdup("http.server"); | |
236 if (!strcasecmp(scheme, "https")) | |
237 return strdup("https.server"); | |
238 return NULL; | |
239 } | |
240 | |
241 // Given a JSON string from the Session Manager describing the session's | |
242 // proxies, set "out_proxies" to the appropriate proxies for a given "url". | |
243 // Copied from src/update_engine/chrome_proxy_resolver.cc. | |
244 static bool GetProxiesFromJsonSettings(const char *url, | |
245 const char *settings, | |
246 std::deque<std::string> *out_proxies) { | |
247 base::JSONReader parser; | |
248 | |
249 out_proxies->clear(); | |
250 | |
251 scoped_ptr<Value> root( | |
252 parser.JsonToValue(settings, | |
253 true, // check root is obj/arr | |
254 false)); // false = disallow trailing comma | |
255 if (!root.get()) { | |
256 LOG(ERROR) << "Unable to parse JSON: " << parser.GetErrorMessage().c_str(); | |
257 return false; | |
258 } | |
259 | |
260 if (!root->IsType(Value::TYPE_DICTIONARY)) { | |
261 LOG(ERROR) << "Invalid root JSON type"; | |
262 return false; | |
263 } | |
264 | |
265 DictionaryValue* root_dict = dynamic_cast<DictionaryValue*>(root.get()); | |
266 if (!root_dict) { | |
267 LOG(ERROR) << "Expected a DictionaryValue"; | |
268 return false; | |
269 } | |
270 int mode = -1; | |
271 if (!root_dict->GetInteger("mode", &mode)) { | |
272 LOG(ERROR) << "Error parsing \"mode\" attribute"; | |
273 return false; | |
274 } | |
275 | |
276 // TODO(mkrebs): This ignores any "bypass_rules" setting. | |
kmixter1
2011/04/06 04:28:53
Not sure what is to be done based on this TODO.
Michael Krebs
2011/04/12 00:53:38
Moot now, because I removed the Session Manager pr
| |
277 switch (mode) { | |
278 case kProxyModeDirect: { | |
279 // Do nothing. | |
280 break; | |
281 } | |
282 | |
283 case kProxyModeSingle: { | |
284 LOG(INFO) << "single proxy mode"; | |
285 std::string proxy_string; | |
286 if (!root_dict->GetString("single.server", &proxy_string)) { | |
287 LOG(ERROR) << "Unable to find \"single.server\" key in " | |
288 << "session manager proxy message"; | |
289 return false; | |
290 } | |
291 if (proxy_string.find("://") == std::string::npos) { | |
292 // missing protocol, assume http. | |
293 proxy_string = std::string("http://") + proxy_string; | |
294 } | |
295 LOG(INFO) << "Found proxy via session manager: " << (*out_proxies)[0]; | |
296 out_proxies->push_back(proxy_string); | |
297 break; | |
298 } | |
299 | |
300 case kProxyModeProxyPerScheme: { | |
301 // Proxy per scheme mode. | |
302 LOG(INFO) << "proxy per scheme mode"; | |
303 | |
304 // Using "proto_*" variables to refer to http or https. | |
305 const char *scheme = GetUrlScheme(url); | |
306 const char *proto_path = GetProtocolPathFromScheme(scheme); | |
307 free(const_cast<char *>(scheme)); | |
308 if (!proto_path) { | |
309 LOG(ERROR) << "Unknown scheme '" << scheme | |
310 << "' for URL '" << url << "'"; | |
311 return false; | |
312 } | |
313 const char *socks_path = "socks.server"; | |
314 | |
315 std::string proto_server, socks_server; | |
316 if (root_dict->GetString(proto_path, &proto_server)) { | |
317 if (proto_server.find("://") == std::string::npos) { | |
318 // missing protocol, assume http. | |
319 proto_server = std::string("http://") + proto_server; | |
320 } | |
321 out_proxies->push_back(proto_server); | |
322 LOG(INFO) << "got http/https server: " << proto_server; | |
323 } | |
324 if (root_dict->GetString(socks_path, &socks_server)) { | |
325 out_proxies->push_back(socks_server); | |
326 LOG(INFO) << "got socks server: " << proto_server; | |
327 } | |
328 | |
329 free(const_cast<char *>(proto_path)); | |
330 break; | |
331 } | |
332 | |
333 default: { | |
334 LOG(INFO) << "unsupported proxy mode: " << mode; | |
335 break; | |
336 } | |
337 } | |
338 | |
339 // Always add direct proxy. | |
340 out_proxies->push_back(kNoProxy); | |
341 return true; | |
342 } | |
343 | |
344 static bool ShowSessionProxies(const char *url) { | |
345 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection(); | |
346 if (!dbus.HasConnection()) { | |
347 LOG(ERROR) << "Error connecting to system D-Bus"; | |
348 return false; | |
349 } | |
350 chromeos::dbus::Proxy session_proxy(dbus, | |
351 kSessionManagerService, | |
352 kSessionManagerPath, | |
353 kSessionManagerInterface); | |
354 if (!session_proxy) { | |
355 LOG(ERROR) << "Error creating D-Bus proxy to interface " | |
356 << "'" << kSessionManagerInterface << "'"; | |
357 return false; | |
358 } | |
359 | |
360 // First, query dbus for the currently stored settings. | |
361 gchar *json_settings = NULL; | |
362 GError *gerror = NULL; | |
363 GArray *sig = NULL; | |
364 if (!dbus_g_proxy_call(session_proxy.gproxy(), | |
365 kSessionManagerRetrievePropertyMethod, | |
366 &gerror, | |
367 G_TYPE_STRING, kSessionManagerProxySettingsKey, | |
368 G_TYPE_INVALID, | |
369 G_TYPE_STRING, &json_settings, | |
370 DBUS_TYPE_G_UCHAR_ARRAY, &sig, | |
371 G_TYPE_INVALID)) { | |
372 LOG(ERROR) << "Error retrieving proxy settings via D-Bus proxy call: " | |
373 << GetGErrorMessage(gerror); | |
374 return false; | |
375 } | |
376 g_array_free(sig, false); | |
377 LOG(INFO) << "Received session manager proxy settings: " << json_settings; | |
378 | |
379 std::deque<std::string> proxies; | |
380 if (!GetProxiesFromJsonSettings(url, json_settings, &proxies)) | |
381 LOG(ERROR) << "Error parsing JSON '" << json_settings << "'"; | |
382 g_free(json_settings); | |
383 | |
384 for (std::deque<std::string>::const_iterator it = proxies.begin(); | |
385 it != proxies.end(); ++it) { | |
386 g_print("%s\n", (*it).c_str()); | |
387 } | |
388 return true; | |
389 } | |
390 | |
391 | |
392 int main(int argc, char *argv[]) { | |
393 google::ParseCommandLineFlags(&argc, &argv, true); | |
394 CommandLine::Init(argc, argv); | |
395 | |
396 // Default to logging to syslog. | |
397 int init_flags = chromeos::kLogToSyslog; | |
398 // Log to stderr if a TTY (and "-quiet" wasn't passed), or if "-verbose" | |
399 // was passed. | |
400 if ((!FLAGS_quiet && isatty(STDERR_FILENO)) || FLAGS_verbose) | |
401 init_flags |= chromeos::kLogToStderr; | |
402 chromeos::InitLog(init_flags); | |
403 | |
404 ::g_type_init(); | |
405 | |
406 const char *url = (argc >= 2) ? argv[1] : NULL; | |
407 | |
408 LOG(INFO) << "Resolving proxies for URL: " << (url ? url : "<n/a>"); | |
409 | |
410 if (FLAGS_force_browser_proxies || FLAGS_force_session_proxies) { | |
411 if (FLAGS_force_browser_proxies) { | |
412 if (!ShowBrowserProxies(url)) | |
413 LOG(ERROR) << "Error resolving proxies via the browser"; | |
414 } | |
415 if (FLAGS_force_session_proxies) { | |
416 if (!ShowSessionProxies(url)) | |
417 LOG(ERROR) << "Error resolving proxies via the session manager"; | |
418 } | |
419 } else { | |
420 if (!ShowBrowserProxies(url)) { | |
421 LOG(ERROR) << "Error resolving proxies via the browser"; | |
422 if (!ShowSessionProxies(url)) { | |
kmixter1
2011/04/06 04:28:53
I've heard we're moving away from global proxy set
Michael Krebs
2011/04/12 00:53:38
Removed.
| |
423 LOG(ERROR) << "Error resolving proxies via the session manager"; | |
424 LOG(INFO) << "Assuming direct proxy"; | |
425 g_print("%s\n", kNoProxy); | |
426 } | |
427 } | |
428 } | |
429 | |
430 return 0; | |
431 } | |
OLD | NEW |