-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome_browser_proxy_resolver.cc
313 lines (281 loc) · 10.3 KB
/
chrome_browser_proxy_resolver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "update_engine/chrome_browser_proxy_resolver.h"
#include <map>
#include <string>
#include <base/string_util.h>
#include <base/strings/string_tokenizer.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <google/protobuf/stubs/common.h>
#include "update_engine/dbus_constants.h"
#include "update_engine/utils.h"
namespace chromeos_update_engine {
using base::StringTokenizer;
using google::protobuf::Closure;
using google::protobuf::NewCallback;
using std::deque;
using std::make_pair;
using std::multimap;
using std::pair;
using std::string;
#define LIB_CROS_PROXY_RESOLVE_NAME "ProxyResolved"
#define LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE \
"org.chromium.UpdateEngineLibcrosProxyResolvedInterface"
const char kLibCrosServiceName[] = "org.chromium.LibCrosService";
const char kLibCrosServicePath[] = "/org/chromium/LibCrosService";
const char kLibCrosServiceInterface[] = "org.chromium.LibCrosServiceInterface";
const char kLibCrosServiceResolveNetworkProxyMethodName[] =
"ResolveNetworkProxy";
const char kLibCrosProxyResolveName[] = LIB_CROS_PROXY_RESOLVE_NAME;
const char kLibCrosProxyResolveSignalInterface[] =
LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE;
const char kLibCrosProxyResolveSignalFilter[] = "type='signal', "
"interface='" LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE "', "
"member='" LIB_CROS_PROXY_RESOLVE_NAME "'";
#undef LIB_CROS_PROXY_RESOLVE_SIGNAL_INTERFACE
#undef LIB_CROS_PROXY_RESOLVE_NAME
namespace {
const int kTimeout = 5; // seconds
DBusGProxy* GetProxy(DbusGlibInterface* dbus) {
GError* error = NULL;
DBusGConnection* bus = dbus->BusGet(DBUS_BUS_SYSTEM, &error);
if (!bus) {
LOG(ERROR) << "Failed to get bus";
return NULL;
}
DBusGProxy* proxy = dbus->ProxyNewForNameOwner(bus,
kLibCrosServiceName,
kLibCrosServicePath,
kLibCrosServiceInterface,
&error);
if (!proxy) {
LOG(ERROR) << "Error getting dbus proxy for " << kLibCrosServiceName << ": "
<< utils::GetAndFreeGError(&error);
return NULL;
}
return proxy;
}
} // namespace {}
ChromeBrowserProxyResolver::ChromeBrowserProxyResolver(DbusGlibInterface* dbus)
: dbus_(dbus), proxy_(NULL), timeout_(kTimeout) {}
bool ChromeBrowserProxyResolver::Init() {
// Set up signal handler. Code lifted from libcros
if (proxy_) {
// Already initialized
return true;
}
GError* gerror = NULL;
DBusGConnection* gbus = dbus_->BusGet(DBUS_BUS_SYSTEM, &gerror);
TEST_AND_RETURN_FALSE(gbus);
DBusConnection* connection = dbus_->ConnectionGetConnection(gbus);
TEST_AND_RETURN_FALSE(connection);
DBusError error;
dbus_error_init(&error);
dbus_->DbusBusAddMatch(connection, kLibCrosProxyResolveSignalFilter, &error);
TEST_AND_RETURN_FALSE(!dbus_error_is_set(&error));
TEST_AND_RETURN_FALSE(dbus_->DbusConnectionAddFilter(
connection,
&ChromeBrowserProxyResolver::StaticFilterMessage,
this,
NULL));
proxy_ = GetProxy(dbus_);
if (!proxy_) {
dbus_->DbusConnectionRemoveFilter(
connection,
&ChromeBrowserProxyResolver::StaticFilterMessage,
this);
}
TEST_AND_RETURN_FALSE(proxy_); // For the error log
return true;
}
void ChromeBrowserProxyResolver::Shutdown() {
if (!proxy_)
return;
GError* gerror = NULL;
DBusGConnection* gbus = dbus_->BusGet(DBUS_BUS_SYSTEM, &gerror);
TEST_AND_RETURN(gbus);
DBusConnection* connection = dbus_->ConnectionGetConnection(gbus);
dbus_->DbusConnectionRemoveFilter(
connection,
&ChromeBrowserProxyResolver::StaticFilterMessage,
this);
proxy_ = NULL;
}
ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() {
// Kill proxy object.
Shutdown();
// Kill outstanding timers
for (TimeoutsMap::iterator it = timers_.begin(), e = timers_.end(); it != e;
++it) {
g_source_destroy(it->second);
it->second = NULL;
}
}
bool ChromeBrowserProxyResolver::GetProxiesForUrl(const string& url,
ProxiesResolvedFn callback,
void* data) {
GError* error = NULL;
guint timeout = timeout_;
if (proxy_) {
bool dbus_success = true;
bool dbus_reinit = false;
bool dbus_got_error;
do {
if (!dbus_success) {
// We failed with a null error, time to re-init the dbus proxy.
LOG(WARNING) << "attempting to reinitialize dbus proxy and retrying";
Shutdown();
if (!Init()) {
LOG(WARNING) << "failed to reinitialize the dbus proxy";
break;
}
dbus_reinit = true;
}
dbus_success = dbus_->ProxyCall(
proxy_,
kLibCrosServiceResolveNetworkProxyMethodName,
&error,
G_TYPE_STRING, url.c_str(),
G_TYPE_STRING, kLibCrosProxyResolveSignalInterface,
G_TYPE_STRING, kLibCrosProxyResolveName,
G_TYPE_INVALID, G_TYPE_INVALID);
dbus_got_error = false;
if (dbus_success) {
LOG(INFO) << "dbus_g_proxy_call succeeded!";
} else {
if (error) {
// Register the fact that we did receive an error, as it is nullified
// on the next line.
dbus_got_error = true;
LOG(WARNING) << "dbus_g_proxy_call failed: "
<< utils::GetAndFreeGError(&error)
<< " Continuing with no proxy.";
} else {
LOG(WARNING) << "dbus_g_proxy_call failed with no error string, "
"continuing with no proxy.";
}
timeout = 0;
}
} while (!(dbus_success || dbus_got_error || dbus_reinit));
} else {
LOG(WARNING) << "dbus proxy object missing, continuing with no proxy.";
timeout = 0;
}
callbacks_.insert(make_pair(url, make_pair(callback, data)));
Closure* closure = NewCallback(this,
&ChromeBrowserProxyResolver::HandleTimeout,
url);
GSource* timer = g_timeout_source_new_seconds(timeout);
g_source_set_callback(timer, &utils::GlibRunClosure, closure, NULL);
g_source_attach(timer, NULL);
timers_.insert(make_pair(url, timer));
return true;
}
DBusHandlerResult ChromeBrowserProxyResolver::FilterMessage(
DBusConnection* connection,
DBusMessage* message) {
// Code lifted from libcros.
if (!dbus_->DbusMessageIsSignal(message,
kLibCrosProxyResolveSignalInterface,
kLibCrosProxyResolveName)) {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
// Get args
char* source_url = NULL;
char* proxy_list = NULL;
char* error = NULL;
DBusError arg_error;
dbus_error_init(&arg_error);
if (!dbus_->DbusMessageGetArgs(message, &arg_error,
DBUS_TYPE_STRING, &source_url,
DBUS_TYPE_STRING, &proxy_list,
DBUS_TYPE_STRING, &error,
DBUS_TYPE_INVALID)) {
LOG(ERROR) << "Error reading dbus signal.";
return DBUS_HANDLER_RESULT_HANDLED;
}
if (!source_url || !proxy_list) {
LOG(ERROR) << "Error getting url, proxy list from dbus signal.";
return DBUS_HANDLER_RESULT_HANDLED;
}
HandleReply(source_url, proxy_list);
return DBUS_HANDLER_RESULT_HANDLED;
}
bool ChromeBrowserProxyResolver::DeleteUrlState(
const string& source_url,
bool delete_timer,
pair<ProxiesResolvedFn, void*>* callback) {
{
CallbacksMap::iterator it = callbacks_.lower_bound(source_url);
TEST_AND_RETURN_FALSE(it != callbacks_.end());
TEST_AND_RETURN_FALSE(it->first == source_url);
if (callback)
*callback = it->second;
callbacks_.erase(it);
}
{
TimeoutsMap::iterator it = timers_.lower_bound(source_url);
TEST_AND_RETURN_FALSE(it != timers_.end());
TEST_AND_RETURN_FALSE(it->first == source_url);
if (delete_timer)
g_source_destroy(it->second);
timers_.erase(it);
}
return true;
}
void ChromeBrowserProxyResolver::HandleReply(const string& source_url,
const string& proxy_list) {
pair<ProxiesResolvedFn, void*> callback;
TEST_AND_RETURN(DeleteUrlState(source_url, true, &callback));
(*callback.first)(ParseProxyString(proxy_list), callback.second);
}
void ChromeBrowserProxyResolver::HandleTimeout(string source_url) {
LOG(INFO) << "Timeout handler called. Seems Chrome isn't responding.";
pair<ProxiesResolvedFn, void*> callback;
TEST_AND_RETURN(DeleteUrlState(source_url, false, &callback));
deque<string> proxies;
proxies.push_back(kNoProxy);
(*callback.first)(proxies, callback.second);
}
deque<string> ChromeBrowserProxyResolver::ParseProxyString(
const string& input) {
deque<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()) {
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;
}
}
string scheme = 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
string host_and_port = 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;
}
} // namespace chromeos_update_engine