OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings. h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings. h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 rv = base::StringToInt64(pref_value, &val); | 60 rv = base::StringToInt64(pref_value, &val); |
61 DCHECK(rv); | 61 DCHECK(rv); |
62 } | 62 } |
63 return val; | 63 return val; |
64 } | 64 } |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 namespace data_reduction_proxy { | 68 namespace data_reduction_proxy { |
69 | 69 |
70 std::string DataReductionProxySettings::key_; | |
71 bool DataReductionProxySettings::allowed_; | 70 bool DataReductionProxySettings::allowed_; |
72 bool DataReductionProxySettings::promo_allowed_; | 71 bool DataReductionProxySettings::promo_allowed_; |
73 | 72 |
74 // static | 73 // static |
75 bool DataReductionProxySettings::IsProxyOriginSetOnCommandLine() { | 74 bool DataReductionProxySettings::IsProxyOriginSetOnCommandLine() { |
76 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 75 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
77 return command_line.HasSwitch( | 76 return command_line.HasSwitch( |
78 data_reduction_proxy::switches::kDataReductionProxy); | 77 data_reduction_proxy::switches::kDataReductionProxy); |
79 } | 78 } |
80 | 79 |
81 // static | 80 // static |
82 bool DataReductionProxySettings::IsProxyKeySetOnCommandLine() { | 81 bool DataReductionProxySettings::IsProxyKeySetOnCommandLine() { |
83 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 82 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
84 return command_line.HasSwitch( | 83 return command_line.HasSwitch( |
85 data_reduction_proxy::switches::kEnableDataReductionProxy); | 84 data_reduction_proxy::switches::kEnableDataReductionProxy); |
86 } | 85 } |
87 | 86 |
88 // static | 87 // static |
89 bool DataReductionProxySettings::IsIncludedInFieldTrialOrFlags() { | 88 bool DataReductionProxySettings::IsIncludedInFieldTrialOrFlags() { |
90 return (base::FieldTrialList::FindFullName( | 89 return (base::FieldTrialList::FindFullName( |
91 "DataCompressionProxyRollout") == kEnabled || | 90 "DataCompressionProxyRollout") == kEnabled || |
92 IsProxyOriginSetOnCommandLine()); | 91 IsProxyOriginSetOnCommandLine()); |
93 } | 92 } |
94 | 93 |
95 // static | 94 // static |
96 void DataReductionProxySettings::SetKey(const std::string& key) { | |
97 key_ = key; | |
98 } | |
99 | |
100 // static | |
101 void DataReductionProxySettings::SetAllowed(bool allowed) { | 95 void DataReductionProxySettings::SetAllowed(bool allowed) { |
102 allowed_ = allowed; | 96 allowed_ = allowed; |
103 } | 97 } |
104 | 98 |
105 // static | 99 // static |
106 void DataReductionProxySettings::SetPromoAllowed(bool promo_allowed) { | 100 void DataReductionProxySettings::SetPromoAllowed(bool promo_allowed) { |
107 promo_allowed_ = promo_allowed; | 101 promo_allowed_ = promo_allowed; |
108 } | 102 } |
109 | 103 |
110 DataReductionProxySettings::DataReductionProxySettings() | 104 DataReductionProxySettings::DataReductionProxySettings() |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 } | 161 } |
168 | 162 |
169 void DataReductionProxySettings::SetProxyConfigurator( | 163 void DataReductionProxySettings::SetProxyConfigurator( |
170 scoped_ptr<DataReductionProxyConfigurator> configurator) { | 164 scoped_ptr<DataReductionProxyConfigurator> configurator) { |
171 DCHECK(configurator); | 165 DCHECK(configurator); |
172 config_ = configurator.Pass(); | 166 config_ = configurator.Pass(); |
173 } | 167 } |
174 | 168 |
175 // static | 169 // static |
176 void DataReductionProxySettings::InitDataReductionProxySession( | 170 void DataReductionProxySettings::InitDataReductionProxySession( |
177 net::HttpNetworkSession* session) { | 171 net::HttpNetworkSession* session, |
172 const std::string& key) { | |
178 // This is a no-op unless the authentication parameters are compiled in. | 173 // This is a no-op unless the authentication parameters are compiled in. |
sgurun-gerrit only
2014/05/09 16:39:25
Is this comment stale? which parameters are compil
bengr
2014/05/09 17:12:20
Done.
| |
179 // (even though values for them may be specified on the command line). | 174 // (even though values for them may be specified on the command line). |
180 // Authentication will still work if the command line parameters are used, | 175 // Authentication will still work if the command line parameters are used, |
181 // however there will be a round-trip overhead for each challenge/response | 176 // however there will be a round-trip overhead for each challenge/response |
182 // (typically once per session). | 177 // (typically once per session). |
183 // TODO(bengr):Pass a configuration struct into DataReductionProxyConfigurator's | 178 // TODO(bengr):Pass a configuration struct into DataReductionProxyConfigurator's |
184 // constructor. The struct would carry everything in the preprocessor flags. | 179 // constructor. The struct would carry everything in the preprocessor flags. |
185 if (key_.empty()) | 180 if (key.empty()) |
186 return; | 181 return; |
187 DCHECK(session); | 182 DCHECK(session); |
188 net::HttpAuthCache* auth_cache = session->http_auth_cache(); | 183 net::HttpAuthCache* auth_cache = session->http_auth_cache(); |
189 DCHECK(auth_cache); | 184 DCHECK(auth_cache); |
190 InitDataReductionAuthentication(auth_cache); | 185 InitDataReductionAuthentication(auth_cache, key); |
191 } | 186 } |
192 | 187 |
193 // static | 188 // static |
194 void DataReductionProxySettings::InitDataReductionAuthentication( | 189 void DataReductionProxySettings::InitDataReductionAuthentication( |
195 net::HttpAuthCache* auth_cache) { | 190 net::HttpAuthCache* auth_cache, |
191 const std::string& key) { | |
196 DCHECK(auth_cache); | 192 DCHECK(auth_cache); |
197 int64 timestamp = | 193 int64 timestamp = |
198 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | 194 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; |
199 | 195 |
200 DataReductionProxyList proxies = GetDataReductionProxies(); | 196 DataReductionProxyList proxies = GetDataReductionProxies(); |
201 for (DataReductionProxyList::iterator it = proxies.begin(); | 197 for (DataReductionProxyList::iterator it = proxies.begin(); |
202 it != proxies.end(); ++it) { | 198 it != proxies.end(); ++it) { |
203 GURL auth_origin = (*it).GetOrigin(); | 199 GURL auth_origin = (*it).GetOrigin(); |
204 int32 rand[3]; | 200 int32 rand[3]; |
205 crypto::RandBytes(rand, 3 * sizeof(rand[0])); | 201 crypto::RandBytes(rand, 3 * sizeof(rand[0])); |
206 | 202 |
207 std::string realm = | 203 std::string realm = |
208 base::StringPrintf("%s%lld", kAuthenticationRealmName, | 204 base::StringPrintf("%s%lld", kAuthenticationRealmName, |
209 static_cast<long long>(timestamp)); | 205 static_cast<long long>(timestamp)); |
210 std::string challenge = base::StringPrintf( | 206 std::string challenge = base::StringPrintf( |
211 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", | 207 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", |
212 kAuthenticationRealmName, | 208 kAuthenticationRealmName, |
213 realm.data(), | 209 realm.data(), |
214 static_cast<long long>(timestamp), | 210 static_cast<long long>(timestamp), |
215 rand[0], | 211 rand[0], |
216 rand[1], | 212 rand[1], |
217 rand[2]); | 213 rand[2]); |
218 base::string16 password = AuthHashForSalt(timestamp); | 214 base::string16 password = AuthHashForSalt(timestamp, key); |
219 | 215 |
220 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm | 216 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm |
221 << "] challenge: [" << challenge << "] password: [" << password << "]"; | 217 << "] challenge: [" << challenge << "] password: [" << password << "]"; |
222 | 218 |
223 net::AuthCredentials credentials(base::string16(), password); | 219 net::AuthCredentials credentials(base::string16(), password); |
224 // |HttpAuthController| searches this cache by origin and path, the latter | 220 // |HttpAuthController| searches this cache by origin and path, the latter |
225 // being '/' in the case of the data reduction proxy. | 221 // being '/' in the case of the data reduction proxy. |
226 auth_cache->Add(auth_origin, | 222 auth_cache->Add(auth_origin, |
227 realm, | 223 realm, |
228 net::HttpAuth::AUTH_SCHEME_SPDYPROXY, | 224 net::HttpAuth::AUTH_SCHEME_SPDYPROXY, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 DataReductionProxyList proxies = GetDataReductionProxies(); | 299 DataReductionProxyList proxies = GetDataReductionProxies(); |
304 for (DataReductionProxyList::iterator it = proxies.begin(); | 300 for (DataReductionProxyList::iterator it = proxies.begin(); |
305 it != proxies.end(); ++it) { | 301 it != proxies.end(); ++it) { |
306 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it); | 302 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it); |
307 if (origin_host.Equals(auth_info->challenger)) | 303 if (origin_host.Equals(auth_info->challenger)) |
308 return true; | 304 return true; |
309 } | 305 } |
310 return false; | 306 return false; |
311 } | 307 } |
312 | 308 |
313 // static | |
314 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge( | 309 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge( |
315 net::AuthChallengeInfo* auth_info) { | 310 net::AuthChallengeInfo* auth_info) { |
316 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) { | 311 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) { |
317 int64 salt; | 312 int64 salt; |
318 std::string realm_suffix = | 313 std::string realm_suffix = |
319 auth_info->realm.substr(strlen(kAuthenticationRealmName)); | 314 auth_info->realm.substr(strlen(kAuthenticationRealmName)); |
320 if (base::StringToInt64(realm_suffix, &salt)) { | 315 if (base::StringToInt64(realm_suffix, &salt)) { |
321 return AuthHashForSalt(salt); | 316 return AuthHashForSalt(salt, key_); |
322 } else { | 317 } else { |
323 DVLOG(1) << "Unable to parse realm name " << auth_info->realm | 318 DVLOG(1) << "Unable to parse realm name " << auth_info->realm |
324 << "into an int for salting."; | 319 << "into an int for salting."; |
325 return base::string16(); | 320 return base::string16(); |
326 } | 321 } |
327 } else { | 322 } else { |
328 return base::string16(); | 323 return base::string16(); |
329 } | 324 } |
330 } | 325 } |
331 | 326 |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 switches::kDataReductionProxyProbeURL); | 640 switches::kDataReductionProxyProbeURL); |
646 } | 641 } |
647 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) | 642 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) |
648 return DATA_REDUCTION_PROXY_PROBE_URL; | 643 return DATA_REDUCTION_PROXY_PROBE_URL; |
649 #else | 644 #else |
650 return std::string(); | 645 return std::string(); |
651 #endif | 646 #endif |
652 } | 647 } |
653 | 648 |
654 // static | 649 // static |
655 base::string16 DataReductionProxySettings::AuthHashForSalt(int64 salt) { | 650 base::string16 DataReductionProxySettings::AuthHashForSalt( |
656 if (!IsDataReductionProxyAllowed()) | 651 int64 salt, |
657 return base::string16(); | 652 const std::string& key) { |
658 | 653 std::string active_key; |
659 std::string key; | |
660 | 654 |
661 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 655 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
662 if (command_line.HasSwitch(switches::kDataReductionProxy)) { | 656 if (command_line.HasSwitch(switches::kDataReductionProxy)) { |
663 // If an origin is provided via a switch, then only consider the value | 657 // If an origin is provided via a switch, then only consider the value |
664 // that is provided by a switch. Do not use the preprocessor constant. | 658 // that is provided by a switch. Do not use the preprocessor constant. |
665 // Don't expose |key_| to a proxy passed in via the command line. | 659 // Don't expose |key_| to a proxy passed in via the command line. |
666 if (!command_line.HasSwitch(switches::kDataReductionProxyKey)) | 660 if (!command_line.HasSwitch(switches::kDataReductionProxyKey)) |
667 return base::string16(); | 661 return base::string16(); |
668 key = command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); | 662 active_key = command_line.GetSwitchValueASCII( |
663 switches::kDataReductionProxyKey); | |
669 } else { | 664 } else { |
670 key = key_; | 665 active_key = key; |
671 } | 666 } |
672 | 667 DCHECK(!active_key.empty()); |
673 DCHECK(!key.empty()); | |
674 | 668 |
675 std::string salted_key = | 669 std::string salted_key = |
676 base::StringPrintf("%lld%s%lld", | 670 base::StringPrintf("%lld%s%lld", |
677 static_cast<long long>(salt), | 671 static_cast<long long>(salt), |
678 key.c_str(), | 672 active_key.c_str(), |
679 static_cast<long long>(salt)); | 673 static_cast<long long>(salt)); |
680 return base::UTF8ToUTF16(base::MD5String(salted_key)); | 674 return base::UTF8ToUTF16(base::MD5String(salted_key)); |
681 } | 675 } |
682 | 676 |
683 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() { | 677 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() { |
684 DCHECK(url_request_context_getter_); | 678 DCHECK(url_request_context_getter_); |
685 std::string url = GetProxyCheckURL(); | 679 std::string url = GetProxyCheckURL(); |
686 if (url.empty()) | 680 if (url.empty()) |
687 return NULL; | 681 return NULL; |
688 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), | 682 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), |
689 net::URLFetcher::GET, | 683 net::URLFetcher::GET, |
690 this); | 684 this); |
691 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); | 685 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); |
692 fetcher->SetRequestContext(url_request_context_getter_); | 686 fetcher->SetRequestContext(url_request_context_getter_); |
693 // Configure max retries to be at most kMaxRetries times for 5xx errors. | 687 // Configure max retries to be at most kMaxRetries times for 5xx errors. |
694 static const int kMaxRetries = 5; | 688 static const int kMaxRetries = 5; |
695 fetcher->SetMaxRetriesOn5xx(kMaxRetries); | 689 fetcher->SetMaxRetriesOn5xx(kMaxRetries); |
696 return fetcher; | 690 return fetcher; |
697 } | 691 } |
698 | 692 |
699 void DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() { | 693 void DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() { |
700 net::URLFetcher* fetcher = GetURLFetcher(); | 694 net::URLFetcher* fetcher = GetURLFetcher(); |
701 if (!fetcher) | 695 if (!fetcher) |
702 return; | 696 return; |
703 fetcher_.reset(fetcher); | 697 fetcher_.reset(fetcher); |
704 fetcher_->Start(); | 698 fetcher_->Start(); |
705 } | 699 } |
706 | 700 |
707 } // namespace data_reduction_proxy | 701 } // namespace data_reduction_proxy |
OLD | NEW |