| 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" |
| 11 #include "base/prefs/pref_member.h" | 11 #include "base/prefs/pref_member.h" |
| 12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "base/prefs/scoped_user_pref_update.h" | 13 #include "base/prefs/scoped_user_pref_update.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" |
| 18 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" | 19 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" |
| 19 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
| 20 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" | 21 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" |
| 21 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" | 22 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
| 22 #include "crypto/random.h" | |
| 23 #include "net/base/auth.h" | |
| 24 #include "net/base/host_port_pair.h" | 23 #include "net/base/host_port_pair.h" |
| 25 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 26 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
| 27 #include "net/http/http_auth.h" | |
| 28 #include "net/http/http_auth_cache.h" | |
| 29 #include "net/http/http_network_session.h" | 26 #include "net/http/http_network_session.h" |
| 30 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
| 31 #include "net/url_request/url_fetcher.h" | 28 #include "net/url_request/url_fetcher.h" |
| 32 #include "net/url_request/url_fetcher_delegate.h" | 29 #include "net/url_request/url_fetcher_delegate.h" |
| 33 #include "net/url_request/url_request_context_getter.h" | 30 #include "net/url_request/url_request_context_getter.h" |
| 34 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
| 35 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 36 | 33 |
| 37 | 34 |
| 38 using base::StringPrintf; | 35 using base::StringPrintf; |
| 39 | 36 |
| 40 namespace { | 37 namespace { |
| 41 | 38 |
| 42 // Key of the UMA DataReductionProxy.StartupState histogram. | 39 // Key of the UMA DataReductionProxy.StartupState histogram. |
| 43 const char kUMAProxyStartupStateHistogram[] = | 40 const char kUMAProxyStartupStateHistogram[] = |
| 44 "DataReductionProxy.StartupState"; | 41 "DataReductionProxy.StartupState"; |
| 45 | 42 |
| 46 // Key of the UMA DataReductionProxy.ProbeURL histogram. | 43 // Key of the UMA DataReductionProxy.ProbeURL histogram. |
| 47 const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL"; | 44 const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL"; |
| 48 | 45 |
| 49 // TODO(marq): Factor this string out into a constant here and in | |
| 50 // http_auth_handler_spdyproxy. | |
| 51 const char kAuthenticationRealmName[] = "SpdyProxy"; | |
| 52 | |
| 53 int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { | 46 int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { |
| 54 int64 val = 0; | 47 int64 val = 0; |
| 55 std::string pref_value; | 48 std::string pref_value; |
| 56 bool rv = list_value.GetString(index, &pref_value); | 49 bool rv = list_value.GetString(index, &pref_value); |
| 57 DCHECK(rv); | 50 DCHECK(rv); |
| 58 if (rv) { | 51 if (rv) { |
| 59 rv = base::StringToInt64(pref_value, &val); | 52 rv = base::StringToInt64(pref_value, &val); |
| 60 DCHECK(rv); | 53 DCHECK(rv); |
| 61 } | 54 } |
| 62 return val; | 55 return val; |
| 63 } | 56 } |
| 64 | 57 |
| 58 bool IsEnabledOnCommandLine() { |
| 59 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 60 return command_line.HasSwitch( |
| 61 data_reduction_proxy::switches::kEnableDataReductionProxy); |
| 62 } |
| 63 |
| 65 } // namespace | 64 } // namespace |
| 66 | 65 |
| 67 namespace data_reduction_proxy { | 66 namespace data_reduction_proxy { |
| 68 | 67 |
| 69 DataReductionProxySettings::DataReductionProxySettings( | 68 DataReductionProxySettings::DataReductionProxySettings( |
| 70 DataReductionProxyParams* params) | 69 DataReductionProxyParams* params) |
| 71 : restricted_by_carrier_(false), | 70 : restricted_by_carrier_(false), |
| 72 enabled_by_user_(false), | 71 enabled_by_user_(false), |
| 73 prefs_(NULL), | 72 prefs_(NULL), |
| 74 local_state_prefs_(NULL), | 73 local_state_prefs_(NULL), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 url_request_context_getter); | 131 url_request_context_getter); |
| 133 SetProxyConfigurator(configurator.Pass()); | 132 SetProxyConfigurator(configurator.Pass()); |
| 134 } | 133 } |
| 135 | 134 |
| 136 void DataReductionProxySettings::SetProxyConfigurator( | 135 void DataReductionProxySettings::SetProxyConfigurator( |
| 137 scoped_ptr<DataReductionProxyConfigurator> configurator) { | 136 scoped_ptr<DataReductionProxyConfigurator> configurator) { |
| 138 DCHECK(configurator); | 137 DCHECK(configurator); |
| 139 configurator_ = configurator.Pass(); | 138 configurator_ = configurator.Pass(); |
| 140 } | 139 } |
| 141 | 140 |
| 142 // static | |
| 143 void DataReductionProxySettings::InitDataReductionProxySession( | |
| 144 net::HttpNetworkSession* session, | |
| 145 const DataReductionProxyParams* params) { | |
| 146 // This is a no-op unless the authentication parameters are compiled in. | |
| 147 // (even though values for them may be specified on the command line). | |
| 148 // Authentication will still work if the command line parameters are used, | |
| 149 // however there will be a round-trip overhead for each challenge/response | |
| 150 // (typically once per session). | |
| 151 // TODO(bengr):Pass a configuration struct into DataReductionProxyConfigurator's | |
| 152 // constructor. The struct would carry everything in the preprocessor flags. | |
| 153 DCHECK(session); | |
| 154 net::HttpAuthCache* auth_cache = session->http_auth_cache(); | |
| 155 DCHECK(auth_cache); | |
| 156 InitDataReductionAuthentication(auth_cache, params); | |
| 157 } | |
| 158 | |
| 159 // static | |
| 160 void DataReductionProxySettings::InitDataReductionAuthentication( | |
| 161 net::HttpAuthCache* auth_cache, | |
| 162 const DataReductionProxyParams* params) { | |
| 163 DCHECK(auth_cache); | |
| 164 DCHECK(params); | |
| 165 int64 timestamp = | |
| 166 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000; | |
| 167 | |
| 168 DataReductionProxyParams::DataReductionProxyList proxies = | |
| 169 params->GetAllowedProxies(); | |
| 170 for (DataReductionProxyParams::DataReductionProxyList::iterator it = | |
| 171 proxies.begin(); | |
| 172 it != proxies.end(); ++it) { | |
| 173 GURL auth_origin = (*it).GetOrigin(); | |
| 174 | |
| 175 int32 rand[3]; | |
| 176 crypto::RandBytes(rand, 3 * sizeof(rand[0])); | |
| 177 | |
| 178 std::string realm = | |
| 179 base::StringPrintf("%s%lld", kAuthenticationRealmName, | |
| 180 static_cast<long long>(timestamp)); | |
| 181 std::string challenge = base::StringPrintf( | |
| 182 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", | |
| 183 kAuthenticationRealmName, | |
| 184 realm.data(), | |
| 185 static_cast<long long>(timestamp), | |
| 186 rand[0], | |
| 187 rand[1], | |
| 188 rand[2]); | |
| 189 base::string16 password = AuthHashForSalt(timestamp, params->key()); | |
| 190 | |
| 191 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm | |
| 192 << "] challenge: [" << challenge << "] password: [" << password << "]"; | |
| 193 | |
| 194 net::AuthCredentials credentials(base::string16(), password); | |
| 195 // |HttpAuthController| searches this cache by origin and path, the latter | |
| 196 // being '/' in the case of the data reduction proxy. | |
| 197 auth_cache->Add(auth_origin, | |
| 198 realm, | |
| 199 net::HttpAuth::AUTH_SCHEME_SPDYPROXY, | |
| 200 challenge, | |
| 201 credentials, | |
| 202 std::string("/")); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 bool DataReductionProxySettings::IsAcceptableAuthChallenge( | |
| 207 net::AuthChallengeInfo* auth_info) { | |
| 208 // Challenge realm must start with the authentication realm name. | |
| 209 std::string realm_prefix = | |
| 210 auth_info->realm.substr(0, strlen(kAuthenticationRealmName)); | |
| 211 if (realm_prefix != kAuthenticationRealmName) | |
| 212 return false; | |
| 213 | |
| 214 // The challenger must be one of the configured proxies. | |
| 215 DataReductionProxyParams::DataReductionProxyList proxies = | |
| 216 params_->GetAllowedProxies(); | |
| 217 for (DataReductionProxyParams::DataReductionProxyList::iterator it = | |
| 218 proxies.begin(); | |
| 219 it != proxies.end(); ++it) { | |
| 220 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it); | |
| 221 if (origin_host.Equals(auth_info->challenger)) | |
| 222 return true; | |
| 223 } | |
| 224 return false; | |
| 225 } | |
| 226 | |
| 227 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge( | |
| 228 net::AuthChallengeInfo* auth_info) { | |
| 229 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) { | |
| 230 int64 salt; | |
| 231 std::string realm_suffix = | |
| 232 auth_info->realm.substr(strlen(kAuthenticationRealmName)); | |
| 233 if (base::StringToInt64(realm_suffix, &salt)) { | |
| 234 return AuthHashForSalt(salt, params_->key()); | |
| 235 } else { | |
| 236 DVLOG(1) << "Unable to parse realm name " << auth_info->realm | |
| 237 << "into an int for salting."; | |
| 238 return base::string16(); | |
| 239 } | |
| 240 } else { | |
| 241 return base::string16(); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 bool DataReductionProxySettings::IsDataReductionProxyEnabled() { | 141 bool DataReductionProxySettings::IsDataReductionProxyEnabled() { |
| 246 return spdy_proxy_auth_enabled_.GetValue() || | 142 return spdy_proxy_auth_enabled_.GetValue() || IsEnabledOnCommandLine(); |
| 247 DataReductionProxyParams::IsKeySetOnCommandLine(); | |
| 248 } | 143 } |
| 249 | 144 |
| 250 bool | 145 bool |
| 251 DataReductionProxySettings::IsDataReductionProxyAlternativeEnabled() const { | 146 DataReductionProxySettings::IsDataReductionProxyAlternativeEnabled() const { |
| 252 return data_reduction_proxy_alternative_enabled_.GetValue(); | 147 return data_reduction_proxy_alternative_enabled_.GetValue(); |
| 253 } | 148 } |
| 254 | 149 |
| 255 bool DataReductionProxySettings::IsDataReductionProxyManaged() { | 150 bool DataReductionProxySettings::IsDataReductionProxyManaged() { |
| 256 return spdy_proxy_auth_enabled_.IsManaged(); | 151 return spdy_proxy_auth_enabled_.IsManaged(); |
| 257 } | 152 } |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 | 525 |
| 631 void DataReductionProxySettings::WarmProxyConnection() { | 526 void DataReductionProxySettings::WarmProxyConnection() { |
| 632 net::URLFetcher* fetcher = GetURLFetcherForWarmup(); | 527 net::URLFetcher* fetcher = GetURLFetcherForWarmup(); |
| 633 if (!fetcher) | 528 if (!fetcher) |
| 634 return; | 529 return; |
| 635 warmup_fetcher_.reset(fetcher); | 530 warmup_fetcher_.reset(fetcher); |
| 636 warmup_fetcher_->Start(); | 531 warmup_fetcher_->Start(); |
| 637 } | 532 } |
| 638 | 533 |
| 639 } // namespace data_reduction_proxy | 534 } // namespace data_reduction_proxy |
| OLD | NEW |