Index: get_proxies.cc |
diff --git a/get_proxies.cc b/get_proxies.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2ca98237b3abf0382ab79b606b86005966c0bf3a |
--- /dev/null |
+++ b/get_proxies.cc |
@@ -0,0 +1,229 @@ |
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
kmixter1
2011/04/12 01:17:41
nit: I would probably choose print, list, or dump
Michael Krebs
2011/04/14 03:39:15
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <unistd.h> // for isatty() |
+ |
+#include <deque> |
+#include <string> |
+ |
+#include <dbus/dbus-glib-lowlevel.h> |
+#include <glib.h> |
+ |
+#include "base/command_line.h" |
+#include "base/file_util.h" |
+#include "base/string_tokenizer.h" |
+#include "base/string_util.h" |
+#include "base/values.h" |
+#include "chromeos/dbus/dbus.h" |
+#include "chromeos/syslog_logging.h" |
+#include "gflags/gflags.h" |
+ |
+ |
+#pragma GCC diagnostic ignored "-Wstrict-aliasing" |
+DEFINE_bool(quiet, false, "Only print the proxies"); |
+DEFINE_bool(verbose, false, "Print additional messages even " |
+ "when not run from a TTY"); |
+#pragma GCC diagnostic error "-Wstrict-aliasing" |
+ |
+ |
+const char kNoProxy[] = "direct://"; |
+ |
+// The browser's method to call to resolve proxies. |
+const char kLibCrosServiceName[] = "org.chromium.LibCrosService"; |
+const char kLibCrosServicePath[] = "/org/chromium/LibCrosService"; |
+const char kLibCrosServiceInterface[] = "org.chromium.LibCrosServiceInterface"; |
+const char kLibCrosServiceResolveNetworkProxyMethodName[] = |
+ "ResolveNetworkProxy"; |
+// The signal the browser will send to us with its answer. |
+const char kLibCrosProxyResolveSignalInterface[] = |
+ "org.chromium.CrashReporterLibcrosProxyResolvedInterface"; |
+const char kLibCrosProxyResolveName[] = "ProxyResolved"; |
+ |
+ |
kmixter1
2011/04/12 01:17:41
nit: I would avoid using extra vertical white spac
Michael Krebs
2011/04/14 03:39:15
Done.
|
+// Number of seconds to wait for browser to send us a signal |
+const int kBrowserTimeout = 5; |
kmixter1
2011/04/12 01:17:41
Should this be a DEFINE_int32 flag?
Michael Krebs
2011/04/14 03:39:15
Done.
|
+ |
+ |
+static const char *GetGErrorMessage(const GError *error) { |
+ if (!error) |
+ return "Unknown error."; |
+ return error->message; |
+} |
+ |
+ |
+// Copied from src/update_engine/chrome_browser_proxy_resolver.cc |
+// Parses the browser's answer for resolved proxies. It returns a |
+// list of strings, each of which is a resolved proxy. |
+std::deque<std::string> ParseProxyString(const std::string &input) { |
+ std::deque<std::string> ret; |
+ // Some of this code taken from |
+ // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_server.cc and |
+ // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list.cc |
+ StringTokenizer entry_tok(input, ";"); |
+ while (entry_tok.GetNext()) { |
+ std::string token = entry_tok.token(); |
+ TrimWhitespaceASCII(token, TRIM_ALL, &token); |
+ |
+ // Start by finding the first space (if any). |
+ std::string::iterator space; |
+ for (space = token.begin(); space != token.end(); ++space) { |
+ if (IsAsciiWhitespace(*space)) { |
+ break; |
+ } |
+ } |
+ |
+ std::string scheme = std::string(token.begin(), space); |
+ StringToLowerASCII(&scheme); |
+ // Chrome uses "socks" to mean socks4 and "proxy" to mean http. |
+ if (scheme == "socks") |
+ scheme += "4"; |
+ else if (scheme == "proxy") |
+ scheme = "http"; |
+ else if (scheme != "https" && |
+ scheme != "socks4" && |
+ scheme != "socks5" && |
+ scheme != "direct") |
+ continue; // Invalid proxy scheme |
+ |
+ std::string host_and_port = std::string(space, token.end()); |
+ TrimWhitespaceASCII(host_and_port, TRIM_ALL, &host_and_port); |
+ if (scheme != "direct" && host_and_port.empty()) |
+ continue; // Must supply host/port when non-direct proxy used. |
+ ret.push_back(scheme + "://" + host_and_port); |
+ } |
+ if (ret.empty() || *ret.rbegin() != kNoProxy) |
+ ret.push_back(kNoProxy); |
+ return ret; |
+} |
+ |
+class BrowserProxyResolvedSignalWatcher : public chromeos::dbus::SignalWatcher { |
kmixter1
2011/04/12 01:17:41
Add a basic class docstring.
Michael Krebs
2011/04/14 03:39:15
Is the comment that's there now okay? I couldn't
|
+ public: |
+ explicit BrowserProxyResolvedSignalWatcher(GMainLoop *main_loop, |
+ std::deque<std::string> *proxies) |
+ : main_loop_(main_loop), proxies_(proxies) { } |
+ |
+ virtual void OnSignal(DBusMessage *message) { |
+ // Get args |
+ char *source_url = NULL; |
+ char *proxy_list = NULL; |
+ char *error = NULL; |
+ DBusError arg_error; |
+ dbus_error_init(&arg_error); |
+ if (!dbus_message_get_args(message, &arg_error, |
+ DBUS_TYPE_STRING, &source_url, |
+ DBUS_TYPE_STRING, &proxy_list, |
+ DBUS_TYPE_STRING, &error, |
+ DBUS_TYPE_INVALID)) { |
+ LOG(ERROR) << "Error reading D-Bus signal"; |
+ return; |
+ } |
+ if (!source_url || !proxy_list) { |
+ LOG(ERROR) << "Error getting url, proxy list from D-Bus signal"; |
+ return; |
+ } |
+ |
+ const std::deque<std::string> &proxies = ParseProxyString(proxy_list); |
+ for (std::deque<std::string>::const_iterator it = proxies.begin(); |
+ it != proxies.end(); ++it) { |
+ LOG(INFO) << "Found proxy via browser signal: " << (*it).c_str(); |
+ proxies_->push_back(*it); |
+ } |
+ |
+ g_main_loop_quit(main_loop_); |
+ } |
+ |
+ private: |
+ GMainLoop *main_loop_; |
+ std::deque<std::string> *proxies_; |
+}; |
+ |
+static gboolean HandleBrowserTimeout(void *data) { |
+ GMainLoop *main_loop = reinterpret_cast<GMainLoop *>(data); |
+ LOG(ERROR) << "Timeout while waiting for browser to resolve proxy"; |
+ g_main_loop_quit(main_loop); |
+ return false; // only call once |
+} |
+ |
+static bool ShowBrowserProxies(const char *url) { |
+ GMainLoop *main_loop = g_main_loop_new(NULL, false); |
+ |
+ chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection(); |
+ if (!dbus.HasConnection()) { |
+ LOG(ERROR) << "Error connecting to system D-Bus"; |
+ return false; |
+ } |
+ chromeos::dbus::Proxy browser_proxy(dbus, |
+ kLibCrosServiceName, |
+ kLibCrosServicePath, |
+ kLibCrosServiceInterface); |
+ if (!browser_proxy) { |
+ LOG(ERROR) << "Error creating D-Bus proxy to interface " |
+ << "'" << kLibCrosServiceName << "'"; |
+ return false; |
+ } |
+ |
+ // Watch for a proxy-resolved signal sent to us |
+ std::deque<std::string> proxies; |
+ BrowserProxyResolvedSignalWatcher proxy_resolver(main_loop, &proxies); |
+ proxy_resolver.StartMonitoring(kLibCrosProxyResolveSignalInterface, |
+ kLibCrosProxyResolveName); |
+ |
+ // Request the proxies for our URL. The answer is sent to us via a |
+ // proxy-resolved signal. |
+ GError *gerror = NULL; |
+ if (!dbus_g_proxy_call(browser_proxy.gproxy(), |
+ kLibCrosServiceResolveNetworkProxyMethodName, |
+ &gerror, |
+ G_TYPE_STRING, url, |
+ G_TYPE_STRING, kLibCrosProxyResolveSignalInterface, |
+ G_TYPE_STRING, kLibCrosProxyResolveName, |
+ G_TYPE_INVALID, G_TYPE_INVALID)) { |
+ LOG(ERROR) << "Error performing D-Bus proxy call " |
+ << "'" << kLibCrosServiceResolveNetworkProxyMethodName << "'" |
+ << ": " << GetGErrorMessage(gerror); |
+ return false; |
+ } |
+ |
+ // Setup a timeout in case the browser doesn't respond with our signal |
+ g_timeout_add_seconds(kBrowserTimeout, &HandleBrowserTimeout, main_loop); |
+ |
+ // Loop until we either get the proxy-resolved signal, or until the |
+ // timeout is reached. |
+ g_main_loop_run(main_loop); |
+ |
+ for (std::deque<std::string>::const_iterator it = proxies.begin(); |
+ it != proxies.end(); ++it) { |
+ g_print("%s\n", (*it).c_str()); |
+ } |
+ |
+ return true; |
+} |
+ |
+ |
+int main(int argc, char *argv[]) { |
+ google::ParseCommandLineFlags(&argc, &argv, true); |
+ CommandLine::Init(argc, argv); |
+ |
+ // Default to logging to syslog. |
+ int init_flags = chromeos::kLogToSyslog; |
+ // Log to stderr if a TTY (and "-quiet" wasn't passed), or if "-verbose" |
+ // was passed. |
+ if ((!FLAGS_quiet && isatty(STDERR_FILENO)) || FLAGS_verbose) |
+ init_flags |= chromeos::kLogToStderr; |
+ chromeos::InitLog(init_flags); |
+ |
+ ::g_type_init(); |
+ |
+ const char *url = (argc >= 2) ? argv[1] : NULL; |
kmixter1
2011/04/12 01:17:41
Would generally say to
const char *url = NULL;
if
Michael Krebs
2011/04/14 03:39:15
Done.
|
+ |
+ LOG(INFO) << "Resolving proxies for URL: " << (url ? url : "<n/a>"); |
kmixter1
2011/04/12 01:17:41
add explicit comparison to NULL for local style.
Michael Krebs
2011/04/14 03:39:15
Moot after above change.
|
+ |
+ if (!ShowBrowserProxies(url)) { |
+ LOG(ERROR) << "Error resolving proxies via the browser"; |
+ LOG(INFO) << "Assuming direct proxy"; |
+ g_print("%s\n", kNoProxy); |
kmixter1
2011/04/12 01:17:41
Why g_print vs printf?
Michael Krebs
2011/04/14 03:39:15
Done.
|
+ } |
+ |
+ return 0; |
+} |