-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.cc
406 lines (360 loc) · 13.4 KB
/
detector.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2007-2010 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
// TODO(omaha): move EnclosePath and UnenclosePath functions from path.h to
// string.h
// TODO(omaha): Firefox detector does not handle proxy bypass.
#include "omaha/net/detector.h"
#include "base/scoped_ptr.h"
#include "omaha/base/atl_regexp.h"
#include "omaha/base/browser_utils.h"
#include "omaha/base/constants.h"
#include "omaha/base/debug.h"
#include "omaha/base/file_reader.h"
#include "omaha/base/path.h"
#include "omaha/base/reg_key.h"
#include "omaha/base/safe_format.h"
#include "omaha/base/string.h"
#include "omaha/base/user_info.h"
#include "omaha/base/utils.h"
#include "omaha/base/time.h"
#include "omaha/net/http_client.h"
#include "omaha/net/network_config.h"
namespace omaha {
HRESULT RegistryOverrideProxyDetector::Detect(ProxyConfig* config) {
ASSERT1(config);
RegKey reg_key;
HRESULT hr = reg_key.Open(reg_path_, KEY_READ);
if (FAILED(hr)) {
return hr;
}
CString proxy_host;
hr = reg_key.GetValue(kRegValueProxyHost, &proxy_host);
if (FAILED(hr)) {
return hr;
}
DWORD proxy_port(0);
hr = reg_key.GetValue(kRegValueProxyPort, &proxy_port);
if (FAILED(hr)) {
return hr;
}
*config = ProxyConfig();
SafeCStringFormat(&config->proxy, _T("%s:%d"), proxy_host, proxy_port);
config->source = source();
config->priority = ProxyConfig::PROXY_PRIORITY_OVERRIDE;
return S_OK;
}
UpdateDevProxyDetector::UpdateDevProxyDetector()
: registry_detector_(MACHINE_REG_UPDATE_DEV) {
}
FirefoxProxyDetector::FirefoxProxyDetector()
: cached_prefs_last_modified_(0),
cached_config_(new ProxyConfig) {
}
HRESULT FirefoxProxyDetector::Detect(ProxyConfig* config) {
ASSERT1(config);
// The Firefox profile is not available when running as a local system.
if (user_info::IsRunningAsSystem()) {
return E_FAIL;
}
const TCHAR* const kFirefoxPrefsJsFile = _T("\\prefs.js");
CString name, path;
HRESULT hr = GetFirefoxDefaultProfile(&name, &path);
if (FAILED(hr)) {
return hr;
}
path.Append(kFirefoxPrefsJsFile);
// Has the current profile been modified? Check the name, path, and
// last modified time of the profile are the same as their cached values.
FILETIME filetime_last_modified = {0};
int64 last_modified = 0;
if (SUCCEEDED(File::GetFileTime(path,
NULL,
NULL,
&filetime_last_modified))) {
last_modified = FileTimeToInt64(filetime_last_modified);
}
if (name.CompareNoCase(cached_prefs_name_) == 0 &&
path.CompareNoCase(cached_prefs_file_path_) == 0 &&
last_modified == cached_prefs_last_modified_ &&
last_modified) {
NET_LOG(L4, (_T("[using FF cached profile][%s]"), path));
*config = *cached_config_;
return S_OK;
}
hr = ParsePrefsFile(name, path, config);
if (SUCCEEDED(hr) && last_modified) {
// If FireFox is the default brower, promotes its proxy priority.
BrowserType browser_type(BROWSER_UNKNOWN);
if (SUCCEEDED(GetDefaultBrowserType(&browser_type)) &&
browser_type == BROWSER_FIREFOX) {
config->priority = ProxyConfig::PROXY_PRIORITY_DEFAULT_BROWSER;
}
NET_LOG(L4, (_T("[cache FF profile][%s]"), path));
cached_prefs_name_ = name;
cached_prefs_file_path_ = path;
cached_prefs_last_modified_ = last_modified;
*cached_config_ = *config;
}
return hr;
}
// This is what the proxy configuration in Firefox looks like:
// user_pref("network.proxy.autoconfig_url", "http://wpad/wpad.dat");
// user_pref("network.proxy.ftp", "127.0.0.1");
// user_pref("network.proxy.ftp_port", 8888);
// user_pref("network.proxy.gopher", "127.0.0.1");
// user_pref("network.proxy.gopher_port", 8888);
// user_pref("network.proxy.http", "127.0.0.1");
// user_pref("network.proxy.http_port", 8888);
// user_pref("network.proxy.share_proxy_settings", true);
// user_pref("network.proxy.socks", "127.0.0.1");
// user_pref("network.proxy.socks_port", 8888);
// user_pref("network.proxy.ssl", "127.0.0.1");
// user_pref("network.proxy.ssl_port", 8888);
// user_pref("network.proxy.type", 4);
HRESULT FirefoxProxyDetector::ParsePrefsFile(const TCHAR* name,
const TCHAR* file_path,
ProxyConfig* config) {
ASSERT1(name);
ASSERT1(file_path);
ASSERT1(config);
*config = ProxyConfig();
config->source = source();
// TODO(omaha): implement optimization not to parse the file again if it
// did not change.
UNREFERENCED_PARAMETER(name);
// There were issues in production where the code fails to allocate
// the 1MB memory buffer as it had been initially requested by a previous
// version of the code.
//
// The assert below is somehow flaky but useful to detect the unlikely cases
// when the prefs file can't be opened.
FileReader prefs_file;
const size_t kBufferSize = 0x10000; // 64KB buffer.
HRESULT hr = prefs_file.Init(file_path, kBufferSize);
ASSERT1(SUCCEEDED(hr));
if (FAILED(hr)) {
return hr;
}
CString proxy_type;
CString proxy_config_url;
CString proxy_http_host;
CString proxy_http_port;
CString proxy_ssl_host;
CString proxy_ssl_port;
// For each line in the prefs.js, try to parse the proxy information out.
char line[1024] = {0};
while (SUCCEEDED(prefs_file.ReadLineAnsi(arraysize(line), line))) {
ParsePrefsLine(line,
&proxy_type,
&proxy_config_url,
&proxy_http_host,
&proxy_http_port,
&proxy_ssl_host,
&proxy_ssl_port);
}
// The default in FireFox is direct connection so it may be that the
// network.proxy.type is missing.
int type = PROXY_TYPE_NO_PROXY;
if (!proxy_type.IsEmpty() &&
!String_StringToDecimalIntChecked(proxy_type, &type)) {
return E_UNEXPECTED;
}
// Direct connection.
if (type == PROXY_TYPE_NO_PROXY) {
return S_OK;
}
// We look for both proxy auto-detect and proxy config url, to emulate
// the IE behavior, where when the auto-detect fails it defaults to the
// auto config url. Firefox remembers the auto config url even if not used,
// so it might not hurt to try it out.
if (type & PROXY_TYPE_AUTO_DETECT) {
config->auto_detect = true;
}
if ((type & PROXY_TYPE_AUTO_CONFIG_URL) && !proxy_config_url.IsEmpty()) {
UnenclosePath(&proxy_config_url);
config->auto_config_url = proxy_config_url;
}
// Named proxy.
if (!(type & PROXY_TYPE_NAMED_PROXY)) {
return S_OK;
}
CString proxy;
hr = BuildProxyString(proxy_http_host,
proxy_http_port,
proxy_ssl_host,
proxy_ssl_port,
&proxy);
if (FAILED(hr)) {
return hr;
}
config->proxy = proxy;
return S_OK;
}
HRESULT FirefoxProxyDetector::BuildProxyString(const CString& proxy_http_host,
const CString& http_port,
const CString& proxy_ssl_host,
const CString& ssl_port,
CString* proxy) {
ASSERT1(proxy);
CString http_host = proxy_http_host;
CString ssl_host = proxy_ssl_host;
// The host names in the prefs file are strings literals.
UnenclosePath(&http_host);
UnenclosePath(&ssl_host);
// Validate the port values.
if (!http_port.IsEmpty()) {
int http_port_num = 0;
if (!String_StringToDecimalIntChecked(http_port, &http_port_num) ||
http_port_num <= 0 &&
http_port_num > INTERNET_MAX_PORT_NUMBER_VALUE) {
return E_INVALIDARG;
}
}
if (!ssl_port.IsEmpty()) {
int ssl_port_num = 0;
if (!String_StringToDecimalIntChecked(ssl_port, &ssl_port_num) ||
ssl_port_num <= 0 ||
ssl_port_num > INTERNET_MAX_PORT_NUMBER_VALUE) {
return E_INVALIDARG;
}
}
// Format the proxy string.
CString str;
if (!http_host.IsEmpty()) {
SafeCStringAppendFormat(&str, _T("http=%s"), http_host);
if (!http_port.IsEmpty()) {
SafeCStringAppendFormat(&str, _T(":%s"), http_port);
}
}
if (!ssl_host.IsEmpty()) {
// Append a separator if needed.
if (!str.IsEmpty()) {
str += _T(';');
}
SafeCStringAppendFormat(&str, _T("https=%s"), ssl_host);
if (!ssl_port.IsEmpty()) {
SafeCStringAppendFormat(&str, _T(":%s"), ssl_port);
}
}
*proxy = str;
return S_OK;
}
// Parses a line from the prefs.js. An example of line to parse is:
// user_pref("network.proxy.http", "foo");
void FirefoxProxyDetector::ParsePrefsLine(const char* ansi_line,
CString* proxy_type,
CString* proxy_config_url,
CString* proxy_http_host,
CString* proxy_http_port,
CString* proxy_ssl_host,
CString* proxy_ssl_port) {
// Skip the lines that do not contain "network.proxy" to speed up the
// parsing. This is important for large prefs files.
if (strstr(ansi_line, "network.proxy.") == NULL) {
return;
}
AtlRE proxy_type_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.type\\\"\\b*,\\b*{\\d+}\\)"), false); // NOLINT
AtlRE proxy_config_url_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.autoconfig_url\\\"\\b*,\\b*{\\q}\\)"), false); // NOLINT
AtlRE proxy_http_host_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.http\\\"\\b*,\\b*{\\q}\\)"), false); // NOLINT
AtlRE proxy_http_port_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.http_port\\\"\\b*,\\b*{\\d+}\\)"), false); // NOLINT
AtlRE proxy_ssl_host_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.ssl\\\"\\b*,\\b*{\\q}\\)"), false); // NOLINT
AtlRE proxy_ssl_port_regex(_T("^\\b*user_pref\\b*\\(\\b*\\\"network\\.proxy\\.ssl_port\\\"\\b*,\\b*{\\d+}\\)"), false); // NOLINT
CString line(ansi_line);
if (AtlRE::PartialMatch(line, proxy_type_regex, proxy_type)) {
return;
}
if (AtlRE::PartialMatch(line, proxy_config_url_regex, proxy_config_url)) {
return;
}
if (AtlRE::PartialMatch(line, proxy_http_host_regex, proxy_http_host)) {
return;
}
if (AtlRE::PartialMatch(line, proxy_http_port_regex, proxy_http_port)) {
return;
}
if (AtlRE::PartialMatch(line, proxy_ssl_host_regex, proxy_ssl_host)) {
return;
}
if (AtlRE::PartialMatch(line, proxy_ssl_port_regex, proxy_ssl_port)) {
return;
}
}
HRESULT DefaultProxyDetector::Detect(ProxyConfig* config) {
ASSERT1(config);
scoped_ptr<HttpClient> http_client(CreateHttpClient());
// We expect to be able to instantiate either of the http clients.
ASSERT1(http_client.get());
if (!http_client.get()) {
return E_UNEXPECTED;
}
HRESULT hr = http_client->Initialize();
if (FAILED(hr)) {
return hr;
}
HttpClient::ProxyInfo proxy_info = {0};
hr = http_client->GetDefaultProxyConfiguration(&proxy_info);
if (FAILED(hr)) {
return hr;
}
if (proxy_info.access_type == WINHTTP_ACCESS_TYPE_NAMED_PROXY) {
ProxyConfig proxy_config;
proxy_config.source = source();
proxy_config.proxy = proxy_info.proxy;
proxy_config.proxy_bypass = proxy_info.proxy_bypass;
*config = proxy_config;
return S_OK;
} else {
return E_FAIL;
}
}
HRESULT IEProxyDetector::Detect(ProxyConfig* config) {
ASSERT1(config);
// Internet Explorer proxy configuration is not available when running as
// local system.
if (user_info::IsRunningAsSystem()) {
return E_FAIL;
}
scoped_ptr<HttpClient> http_client(CreateHttpClient());
// We expect to be able to instantiate either of the http clients.
ASSERT1(http_client.get());
if (!http_client.get()) {
return E_UNEXPECTED;
}
HRESULT hr = http_client->Initialize();
if (FAILED(hr)) {
return hr;
}
HttpClient::CurrentUserIEProxyConfig ie_proxy_config = {0};
hr = http_client->GetIEProxyConfiguration(&ie_proxy_config);
if (FAILED(hr)) {
return hr;
}
config->source = source();
config->auto_detect = ie_proxy_config.auto_detect;
config->auto_config_url = ie_proxy_config.auto_config_url;
config->proxy = ie_proxy_config.proxy;
config->proxy_bypass = ie_proxy_config.proxy_bypass;
// If IE is the default brower, promotes its proxy priority.
BrowserType browser_type(BROWSER_UNKNOWN);
if (SUCCEEDED(GetDefaultBrowserType(&browser_type)) &&
browser_type == BROWSER_IE) {
config->priority = ProxyConfig::PROXY_PRIORITY_DEFAULT_BROWSER;
}
::GlobalFree(const_cast<TCHAR*>(ie_proxy_config.auto_config_url));
::GlobalFree(const_cast<TCHAR*>(ie_proxy_config.proxy));
::GlobalFree(const_cast<TCHAR*>(ie_proxy_config.proxy_bypass));
return S_OK;
};
} // namespace omaha