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