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, |
178 // This is a no-op unless the authentication parameters are compiled in. | 172 const std::string& key) { |
179 // (even though values for them may be specified on the command line). | 173 // This is a no-op unless the key is set. (even though values for them may be |
180 // Authentication will still work if the command line parameters are used, | 174 // specified on the command line). Authentication will still work if the |
181 // however there will be a round-trip overhead for each challenge/response | 175 // command line parameters are used, however there will be a round-trip |
182 // (typically once per session). | 176 // overhead for each challenge/response (typically once per session). |
183 // TODO(bengr):Pass a configuration struct into DataReductionProxyConfigurator's | 177 // TODO(bengr):Pass a configuration struct into |
184 // constructor. The struct would carry everything in the preprocessor flags. | 178 // DataReductionProxyConfigurator's constructor. |
185 if (key_.empty()) | 179 if (key.empty()) |
186 return; | 180 return; |
187 DCHECK(session); | 181 DCHECK(session); |
188 net::HttpAuthCache* auth_cache = session->http_auth_cache(); | 182 net::HttpAuthCache* auth_cache = session->http_auth_cache(); |
189 DCHECK(auth_cache); | 183 DCHECK(auth_cache); |
190 InitDataReductionAuthentication(auth_cache); | 184 InitDataReductionAuthentication(auth_cache, key); |
191 } | 185 } |
192 | 186 |
193 // static | 187 // static |
194 void DataReductionProxySettings::InitDataReductionAuthentication( | 188 void DataReductionProxySettings::InitDataReductionAuthentication( |
195 net::HttpAuthCache* auth_cache) { | 189 net::HttpAuthCache* auth_cache, |
| 190 const std::string& key) { |
196 DCHECK(auth_cache); | 191 DCHECK(auth_cache); |
197 int64 timestamp = | 192 int64 timestamp = |
198 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | 193 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; |
199 | 194 |
200 DataReductionProxyList proxies = GetDataReductionProxies(); | 195 DataReductionProxyList proxies = GetDataReductionProxies(); |
201 for (DataReductionProxyList::iterator it = proxies.begin(); | 196 for (DataReductionProxyList::iterator it = proxies.begin(); |
202 it != proxies.end(); ++it) { | 197 it != proxies.end(); ++it) { |
203 GURL auth_origin = (*it).GetOrigin(); | 198 GURL auth_origin = (*it).GetOrigin(); |
204 int32 rand[3]; | 199 int32 rand[3]; |
205 crypto::RandBytes(rand, 3 * sizeof(rand[0])); | 200 crypto::RandBytes(rand, 3 * sizeof(rand[0])); |
206 | 201 |
207 std::string realm = | 202 std::string realm = |
208 base::StringPrintf("%s%lld", kAuthenticationRealmName, | 203 base::StringPrintf("%s%lld", kAuthenticationRealmName, |
209 static_cast<long long>(timestamp)); | 204 static_cast<long long>(timestamp)); |
210 std::string challenge = base::StringPrintf( | 205 std::string challenge = base::StringPrintf( |
211 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", | 206 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", |
212 kAuthenticationRealmName, | 207 kAuthenticationRealmName, |
213 realm.data(), | 208 realm.data(), |
214 static_cast<long long>(timestamp), | 209 static_cast<long long>(timestamp), |
215 rand[0], | 210 rand[0], |
216 rand[1], | 211 rand[1], |
217 rand[2]); | 212 rand[2]); |
218 base::string16 password = AuthHashForSalt(timestamp); | 213 base::string16 password = AuthHashForSalt(timestamp, key); |
219 | 214 |
220 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm | 215 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm |
221 << "] challenge: [" << challenge << "] password: [" << password << "]"; | 216 << "] challenge: [" << challenge << "] password: [" << password << "]"; |
222 | 217 |
223 net::AuthCredentials credentials(base::string16(), password); | 218 net::AuthCredentials credentials(base::string16(), password); |
224 // |HttpAuthController| searches this cache by origin and path, the latter | 219 // |HttpAuthController| searches this cache by origin and path, the latter |
225 // being '/' in the case of the data reduction proxy. | 220 // being '/' in the case of the data reduction proxy. |
226 auth_cache->Add(auth_origin, | 221 auth_cache->Add(auth_origin, |
227 realm, | 222 realm, |
228 net::HttpAuth::AUTH_SCHEME_SPDYPROXY, | 223 net::HttpAuth::AUTH_SCHEME_SPDYPROXY, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 DataReductionProxyList proxies = GetDataReductionProxies(); | 298 DataReductionProxyList proxies = GetDataReductionProxies(); |
304 for (DataReductionProxyList::iterator it = proxies.begin(); | 299 for (DataReductionProxyList::iterator it = proxies.begin(); |
305 it != proxies.end(); ++it) { | 300 it != proxies.end(); ++it) { |
306 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it); | 301 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it); |
307 if (origin_host.Equals(auth_info->challenger)) | 302 if (origin_host.Equals(auth_info->challenger)) |
308 return true; | 303 return true; |
309 } | 304 } |
310 return false; | 305 return false; |
311 } | 306 } |
312 | 307 |
313 // static | |
314 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge( | 308 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge( |
315 net::AuthChallengeInfo* auth_info) { | 309 net::AuthChallengeInfo* auth_info) { |
316 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) { | 310 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) { |
317 int64 salt; | 311 int64 salt; |
318 std::string realm_suffix = | 312 std::string realm_suffix = |
319 auth_info->realm.substr(strlen(kAuthenticationRealmName)); | 313 auth_info->realm.substr(strlen(kAuthenticationRealmName)); |
320 if (base::StringToInt64(realm_suffix, &salt)) { | 314 if (base::StringToInt64(realm_suffix, &salt)) { |
321 return AuthHashForSalt(salt); | 315 return AuthHashForSalt(salt, key_); |
322 } else { | 316 } else { |
323 DVLOG(1) << "Unable to parse realm name " << auth_info->realm | 317 DVLOG(1) << "Unable to parse realm name " << auth_info->realm |
324 << "into an int for salting."; | 318 << "into an int for salting."; |
325 return base::string16(); | 319 return base::string16(); |
326 } | 320 } |
327 } else { | 321 } else { |
328 return base::string16(); | 322 return base::string16(); |
329 } | 323 } |
330 } | 324 } |
331 | 325 |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 switches::kDataReductionProxyProbeURL); | 639 switches::kDataReductionProxyProbeURL); |
646 } | 640 } |
647 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) | 641 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) |
648 return DATA_REDUCTION_PROXY_PROBE_URL; | 642 return DATA_REDUCTION_PROXY_PROBE_URL; |
649 #else | 643 #else |
650 return std::string(); | 644 return std::string(); |
651 #endif | 645 #endif |
652 } | 646 } |
653 | 647 |
654 // static | 648 // static |
655 base::string16 DataReductionProxySettings::AuthHashForSalt(int64 salt) { | 649 base::string16 DataReductionProxySettings::AuthHashForSalt( |
656 if (!IsDataReductionProxyAllowed()) | 650 int64 salt, |
657 return base::string16(); | 651 const std::string& key) { |
658 | 652 std::string active_key; |
659 std::string key; | |
660 | 653 |
661 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 654 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
662 if (command_line.HasSwitch(switches::kDataReductionProxy)) { | 655 if (command_line.HasSwitch(switches::kDataReductionProxy)) { |
663 // If an origin is provided via a switch, then only consider the value | 656 // 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. | 657 // 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. | 658 // Don't expose |key_| to a proxy passed in via the command line. |
666 if (!command_line.HasSwitch(switches::kDataReductionProxyKey)) | 659 if (!command_line.HasSwitch(switches::kDataReductionProxyKey)) |
667 return base::string16(); | 660 return base::string16(); |
668 key = command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey); | 661 active_key = command_line.GetSwitchValueASCII( |
| 662 switches::kDataReductionProxyKey); |
669 } else { | 663 } else { |
670 key = key_; | 664 active_key = key; |
671 } | 665 } |
672 | 666 DCHECK(!active_key.empty()); |
673 DCHECK(!key.empty()); | |
674 | 667 |
675 std::string salted_key = | 668 std::string salted_key = |
676 base::StringPrintf("%lld%s%lld", | 669 base::StringPrintf("%lld%s%lld", |
677 static_cast<long long>(salt), | 670 static_cast<long long>(salt), |
678 key.c_str(), | 671 active_key.c_str(), |
679 static_cast<long long>(salt)); | 672 static_cast<long long>(salt)); |
680 return base::UTF8ToUTF16(base::MD5String(salted_key)); | 673 return base::UTF8ToUTF16(base::MD5String(salted_key)); |
681 } | 674 } |
682 | 675 |
683 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() { | 676 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() { |
684 DCHECK(url_request_context_getter_); | 677 DCHECK(url_request_context_getter_); |
685 std::string url = GetProxyCheckURL(); | 678 std::string url = GetProxyCheckURL(); |
686 if (url.empty()) | 679 if (url.empty()) |
687 return NULL; | 680 return NULL; |
688 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), | 681 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), |
689 net::URLFetcher::GET, | 682 net::URLFetcher::GET, |
690 this); | 683 this); |
691 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); | 684 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY); |
692 fetcher->SetRequestContext(url_request_context_getter_); | 685 fetcher->SetRequestContext(url_request_context_getter_); |
693 // Configure max retries to be at most kMaxRetries times for 5xx errors. | 686 // Configure max retries to be at most kMaxRetries times for 5xx errors. |
694 static const int kMaxRetries = 5; | 687 static const int kMaxRetries = 5; |
695 fetcher->SetMaxRetriesOn5xx(kMaxRetries); | 688 fetcher->SetMaxRetriesOn5xx(kMaxRetries); |
696 return fetcher; | 689 return fetcher; |
697 } | 690 } |
698 | 691 |
699 void DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() { | 692 void DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() { |
700 net::URLFetcher* fetcher = GetURLFetcher(); | 693 net::URLFetcher* fetcher = GetURLFetcher(); |
701 if (!fetcher) | 694 if (!fetcher) |
702 return; | 695 return; |
703 fetcher_.reset(fetcher); | 696 fetcher_.reset(fetcher); |
704 fetcher_->Start(); | 697 fetcher_->Start(); |
705 } | 698 } |
706 | 699 |
707 } // namespace data_reduction_proxy | 700 } // namespace data_reduction_proxy |
OLD | NEW |