OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/client/session/assignment_source.h" | 5 #include "blimp/client/session/assignment_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" |
9 #include "base/location.h" | 12 #include "base/location.h" |
10 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
11 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/values.h" |
12 #include "blimp/client/app/blimp_client_switches.h" | 16 #include "blimp/client/app/blimp_client_switches.h" |
| 17 #include "blimp/common/protocol_version.h" |
13 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
14 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/load_flags.h" |
| 21 #include "net/base/net_errors.h" |
| 22 #include "net/base/url_util.h" |
| 23 #include "net/http/http_status_code.h" |
| 24 #include "net/proxy/proxy_config_service.h" |
| 25 #include "net/proxy/proxy_service.h" |
| 26 #include "net/url_request/url_fetcher.h" |
| 27 #include "net/url_request/url_request_context.h" |
| 28 #include "net/url_request/url_request_context_builder.h" |
| 29 #include "net/url_request/url_request_context_getter.h" |
15 | 30 |
16 namespace blimp { | 31 namespace blimp { |
| 32 namespace client { |
| 33 |
17 namespace { | 34 namespace { |
18 | 35 |
19 // TODO(kmarshall): Take values from configuration data. | 36 // Assignment request JSON keys. |
20 const char kDummyClientToken[] = "MyVoiceIsMyPassport"; | 37 const char kProtocolVersionKey[] = "protocol_version"; |
21 const std::string kDefaultBlimpletIPAddress = "127.0.0.1"; | 38 |
22 const uint16_t kDefaultBlimpletTCPPort = 25467; | 39 // Assignment response JSON keys. |
23 | 40 const char kClientTokenKey[] = "clientToken"; |
24 net::IPAddress GetBlimpletIPAddress() { | 41 const char kHostKey[] = "host"; |
| 42 const char kPortKey[] = "port"; |
| 43 const char kCertificateFingerprintKey[] = "certificateFingerprint"; |
| 44 const char kCertificateKey[] = "certificate"; |
| 45 |
| 46 // URL scheme constants for custom assignments. See the '--blimplet-endpoint' |
| 47 // documentation in blimp_client_switches.cc for details. |
| 48 const char kCustomSSLScheme[] = "ssl"; |
| 49 const char kCustomTCPScheme[] = "tcp"; |
| 50 const char kCustomQUICScheme[] = "quic"; |
| 51 |
| 52 Assignment GetCustomBlimpletAssignment() { |
| 53 GURL url(base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 54 switches::kBlimpletEndpoint)); |
| 55 |
25 std::string host; | 56 std::string host; |
26 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 57 int port; |
27 switches::kBlimpletHost)) { | 58 if (url.is_empty() || !url.is_valid() || !url.has_scheme() || |
28 host = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 59 !net::ParseHostAndPort(url.path(), &host, &port)) { |
29 switches::kBlimpletHost); | 60 return Assignment(); |
30 } else { | 61 } |
31 host = kDefaultBlimpletIPAddress; | 62 |
32 } | |
33 net::IPAddress ip_address; | 63 net::IPAddress ip_address; |
34 if (!ip_address.AssignFromIPLiteral(host)) | 64 if (!ip_address.AssignFromIPLiteral(host)) { |
35 CHECK(false) << "Invalid BlimpletAssignment host " << host; | 65 CHECK(false) << "Invalid BlimpletAssignment host " << host; |
36 return ip_address; | 66 } |
37 } | 67 |
38 | 68 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { |
39 uint16_t GetBlimpletTCPPort() { | 69 CHECK(false) << "Invalid BlimpletAssignment port " << port; |
40 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 70 } |
41 switches::kBlimpletTCPPort)) { | 71 |
42 std::string port_str = | 72 Assignment::TransportProtocol protocol = |
43 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 73 Assignment::TransportProtocol::UNKNOWN; |
44 switches::kBlimpletTCPPort); | 74 if (url.has_scheme()) { |
45 uint port_64t; | 75 if (url.SchemeIs(kCustomSSLScheme)) { |
46 if (!base::StringToUint(port_str, &port_64t) || | 76 protocol = Assignment::TransportProtocol::SSL; |
47 !base::IsValueInRangeForNumericType<uint16_t>(port_64t)) { | 77 } else if (url.SchemeIs(kCustomTCPScheme)) { |
48 CHECK(false) << "Invalid BlimpletAssignment port " << port_str; | 78 protocol = Assignment::TransportProtocol::TCP; |
| 79 } else if (url.SchemeIs(kCustomQUICScheme)) { |
| 80 protocol = Assignment::TransportProtocol::QUIC; |
| 81 } else { |
| 82 CHECK(false) << "Invalid BlimpletAssignment scheme " << url.scheme(); |
49 } | 83 } |
50 return base::checked_cast<uint16_t>(port_64t); | 84 } |
51 } else { | 85 |
52 return kDefaultBlimpletTCPPort; | 86 Assignment assignment; |
53 } | 87 assignment.transport_protocol = protocol; |
54 } | 88 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 89 assignment.client_token = kDummyClientToken; |
| 90 return assignment; |
| 91 } |
| 92 |
| 93 GURL GetBlimpAssignerURL() { |
| 94 // TODO(dtrainor): Add a way to specify another assigner. |
| 95 return GURL(kDefaultAssignerURL); |
| 96 } |
| 97 |
| 98 class SimpleURLRequestContextGetter : public net::URLRequestContextGetter { |
| 99 public: |
| 100 SimpleURLRequestContextGetter( |
| 101 const scoped_refptr<base::SingleThreadTaskRunner>& io_loop_task_runner) |
| 102 : io_loop_task_runner_(io_loop_task_runner), |
| 103 proxy_config_service_(net::ProxyService::CreateSystemProxyConfigService( |
| 104 io_loop_task_runner_, |
| 105 io_loop_task_runner_)) {} |
| 106 |
| 107 // net::URLRequestContextGetter implementation. |
| 108 net::URLRequestContext* GetURLRequestContext() override { |
| 109 if (!url_request_context_) { |
| 110 net::URLRequestContextBuilder builder; |
| 111 builder.set_proxy_config_service(std::move(proxy_config_service_)); |
| 112 builder.DisableHttpCache(); |
| 113 url_request_context_ = builder.Build(); |
| 114 } |
| 115 |
| 116 return url_request_context_.get(); |
| 117 } |
| 118 |
| 119 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
| 120 const override { |
| 121 return io_loop_task_runner_; |
| 122 } |
| 123 |
| 124 private: |
| 125 ~SimpleURLRequestContextGetter() override {} |
| 126 |
| 127 scoped_refptr<base::SingleThreadTaskRunner> io_loop_task_runner_; |
| 128 scoped_ptr<net::URLRequestContext> url_request_context_; |
| 129 |
| 130 // Temporary storage for the ProxyConfigService, which needs to be created on |
| 131 // the main thread but cleared on the IO thread. This will be built in the |
| 132 // constructor and cleared on the IO thread. Due to the usage of this class |
| 133 // this is safe. |
| 134 scoped_ptr<net::ProxyConfigService> proxy_config_service_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(SimpleURLRequestContextGetter); |
| 137 }; |
55 | 138 |
56 } // namespace | 139 } // namespace |
57 | 140 |
58 namespace client { | 141 Assignment::Assignment() : transport_protocol(TransportProtocol::UNKNOWN) {} |
| 142 |
| 143 Assignment::~Assignment() {} |
| 144 |
| 145 bool Assignment::is_null() const { |
| 146 return ip_endpoint.address().empty() || ip_endpoint.port() == 0 || |
| 147 transport_protocol == TransportProtocol::UNKNOWN; |
| 148 } |
59 | 149 |
60 AssignmentSource::AssignmentSource( | 150 AssignmentSource::AssignmentSource( |
61 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) | 151 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, |
62 : main_task_runner_(main_task_runner) {} | 152 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) |
| 153 : main_task_runner_(main_task_runner), |
| 154 url_request_context_(new SimpleURLRequestContextGetter(io_task_runner)) {} |
63 | 155 |
64 AssignmentSource::~AssignmentSource() {} | 156 AssignmentSource::~AssignmentSource() {} |
65 | 157 |
66 void AssignmentSource::GetAssignment(const AssignmentCallback& callback) { | 158 void AssignmentSource::GetAssignment(const std::string& client_auth_token, |
| 159 const AssignmentCallback& callback) { |
67 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 160 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 161 |
| 162 // Cancel any outstanding callback. |
| 163 if (!callback_.is_null()) { |
| 164 base::ResetAndReturn(&callback_) |
| 165 .Run(AssignmentSource::Result::RESULT_SERVER_INTERRUPTED, Assignment()); |
| 166 } |
| 167 callback_ = AssignmentCallback(callback); |
| 168 |
| 169 Assignment assignment = GetCustomBlimpletAssignment(); |
| 170 if (!assignment.is_null()) { |
| 171 // Post the result so that the behavior of this function is consistent. |
| 172 main_task_runner_->PostTask( |
| 173 FROM_HERE, base::Bind(base::ResetAndReturn(&callback_), |
| 174 AssignmentSource::Result::RESULT_OK, assignment)); |
| 175 return; |
| 176 } |
| 177 |
| 178 // Call out to the network for a real assignment. Build the network request |
| 179 // to hit the assigner. |
| 180 url_fetcher_ = net::URLFetcher::Create(GetBlimpAssignerURL(), |
| 181 net::URLFetcher::POST, this); |
| 182 url_fetcher_->SetRequestContext(url_request_context_.get()); |
| 183 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 184 net::LOAD_DO_NOT_SEND_COOKIES); |
| 185 url_fetcher_->AddExtraRequestHeader("Authorization: Bearer " + |
| 186 client_auth_token); |
| 187 |
| 188 // Write the JSON for the request data. |
| 189 base::DictionaryValue dictionary; |
| 190 dictionary.SetString(kProtocolVersionKey, blimp::kEngineVersion); |
| 191 std::string json; |
| 192 base::JSONWriter::Write(dictionary, &json); |
| 193 url_fetcher_->SetUploadData("application/json", json); |
| 194 |
| 195 url_fetcher_->Start(); |
| 196 } |
| 197 |
| 198 void AssignmentSource::OnURLFetchComplete(const net::URLFetcher* source) { |
| 199 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 200 DCHECK(!callback_.is_null()); |
| 201 DCHECK_EQ(url_fetcher_.get(), source); |
| 202 |
| 203 if (!source->GetStatus().is_success()) { |
| 204 DVLOG(1) << "Assignment request failed due to network error: " |
| 205 << net::ErrorToString(source->GetStatus().error()); |
| 206 base::ResetAndReturn(&callback_) |
| 207 .Run(AssignmentSource::Result::RESULT_NETWORK_FAILURE, Assignment()); |
| 208 return; |
| 209 } |
| 210 |
| 211 switch (source->GetResponseCode()) { |
| 212 case net::HTTP_OK: |
| 213 ParseAssignerResponse(); |
| 214 break; |
| 215 case net::HTTP_BAD_REQUEST: |
| 216 base::ResetAndReturn(&callback_) |
| 217 .Run(AssignmentSource::Result::RESULT_BAD_REQUEST, Assignment()); |
| 218 break; |
| 219 case net::HTTP_UNAUTHORIZED: |
| 220 base::ResetAndReturn(&callback_) |
| 221 .Run(AssignmentSource::Result::RESULT_EXPIRED_ACCESS_TOKEN, |
| 222 Assignment()); |
| 223 break; |
| 224 case net::HTTP_FORBIDDEN: |
| 225 base::ResetAndReturn(&callback_) |
| 226 .Run(AssignmentSource::Result::RESULT_USER_INVALID, Assignment()); |
| 227 break; |
| 228 case 429: // Too Many Requests |
| 229 base::ResetAndReturn(&callback_) |
| 230 .Run(AssignmentSource::Result::RESULT_OUT_OF_VMS, Assignment()); |
| 231 break; |
| 232 case net::HTTP_INTERNAL_SERVER_ERROR: |
| 233 base::ResetAndReturn(&callback_) |
| 234 .Run(AssignmentSource::Result::RESULT_SERVER_ERROR, Assignment()); |
| 235 break; |
| 236 default: |
| 237 base::ResetAndReturn(&callback_) |
| 238 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 239 break; |
| 240 } |
| 241 } |
| 242 |
| 243 void AssignmentSource::ParseAssignerResponse() { |
| 244 DCHECK(url_fetcher_.get()); |
| 245 DCHECK(url_fetcher_->GetStatus().is_success()); |
| 246 DCHECK_EQ(net::HTTP_OK, url_fetcher_->GetResponseCode()); |
| 247 |
| 248 // Grab the response from the assigner request. |
| 249 std::string response; |
| 250 if (!url_fetcher_->GetResponseAsString(&response)) { |
| 251 base::ResetAndReturn(&callback_) |
| 252 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 253 return; |
| 254 } |
| 255 |
| 256 // Attempt to interpret the response as JSON and treat it as a dictionary. |
| 257 scoped_ptr<base::Value> json = base::JSONReader::Read(response); |
| 258 if (!json) { |
| 259 base::ResetAndReturn(&callback_) |
| 260 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 261 return; |
| 262 } |
| 263 |
| 264 const base::DictionaryValue* dict; |
| 265 if (!json->GetAsDictionary(&dict)) { |
| 266 base::ResetAndReturn(&callback_) |
| 267 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 268 return; |
| 269 } |
| 270 |
| 271 // Validate that all the expected fields are present. |
| 272 std::string client_token; |
| 273 std::string host; |
| 274 int port; |
| 275 std::string cert_fingerprint; |
| 276 std::string cert; |
| 277 if (!(dict->GetString(kClientTokenKey, &client_token) && |
| 278 dict->GetString(kHostKey, &host) && dict->GetInteger(kPortKey, &port) && |
| 279 dict->GetString(kCertificateFingerprintKey, &cert_fingerprint) && |
| 280 dict->GetString(kCertificateKey, &cert))) { |
| 281 base::ResetAndReturn(&callback_) |
| 282 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 283 return; |
| 284 } |
| 285 |
| 286 net::IPAddress ip_address; |
| 287 if (!ip_address.AssignFromIPLiteral(host)) { |
| 288 base::ResetAndReturn(&callback_) |
| 289 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 290 return; |
| 291 } |
| 292 |
| 293 if (!base::IsValueInRangeForNumericType<uint16_t>(port)) { |
| 294 base::ResetAndReturn(&callback_) |
| 295 .Run(AssignmentSource::Result::RESULT_BAD_RESPONSE, Assignment()); |
| 296 return; |
| 297 } |
| 298 |
68 Assignment assignment; | 299 Assignment assignment; |
69 assignment.ip_endpoint = | 300 // The assigner assumes SSL-only and all engines it assigns only communicate |
70 net::IPEndPoint(GetBlimpletIPAddress(), GetBlimpletTCPPort()); | 301 // over SSL. |
71 assignment.client_token = kDummyClientToken; | 302 assignment.transport_protocol = Assignment::TransportProtocol::SSL; |
72 main_task_runner_->PostTask(FROM_HERE, base::Bind(callback, assignment)); | 303 assignment.ip_endpoint = net::IPEndPoint(ip_address, port); |
| 304 assignment.client_token = client_token; |
| 305 assignment.certificate = cert; |
| 306 assignment.certificate_fingerprint = cert_fingerprint; |
| 307 |
| 308 base::ResetAndReturn(&callback_) |
| 309 .Run(AssignmentSource::Result::RESULT_OK, assignment); |
73 } | 310 } |
74 | 311 |
75 } // namespace client | 312 } // namespace client |
76 } // namespace blimp | 313 } // namespace blimp |
OLD | NEW |