OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/protocol/http_ice_config_request.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/values.h" | |
13 #include "net/base/url_util.h" | |
14 #include "remoting/protocol/ice_config.h" | |
15 | |
16 namespace remoting { | |
17 namespace protocol { | |
18 | |
19 namespace { | |
20 | |
21 // Ensure ICE config is correct at least one hour after session starts. | |
22 const int kMinimumConfigLifetimeSeconds = 3600; | |
23 | |
24 // See draft-petithuguenin-behave-turn-uris-01. | |
25 const int kDefaultStunTurnPort = 3478; | |
26 const int kDefaultTurnsPort = 5349; | |
27 | |
28 bool ParseLifetime(const std::string& string, base::TimeDelta* result) { | |
29 double seconds; | |
30 if (!base::EndsWith(string, "s", base::CompareCase::INSENSITIVE_ASCII) || | |
31 !base::StringToDouble(string.substr(0, string.size() - 1), &seconds)) { | |
32 return false; | |
33 } | |
34 *result = base::TimeDelta::FromSecondsD(seconds); | |
35 return true; | |
36 } | |
37 | |
38 // Parses url in form of <stun|turn|turns>:<host>[:<port>][?transport=<udp|tcp>] | |
39 // and adds an entry to the |config|. | |
40 bool AddServerToConfig(std::string url, | |
41 const std::string& username, | |
42 const std::string& password, | |
43 IceConfig* config) { | |
44 cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST; | |
45 | |
46 const char kTcpTransportSuffix[] = "?transport=tcp"; | |
47 const char kUdpTransportSuffix[] = "?transport=udp"; | |
48 if (base::EndsWith(url, kTcpTransportSuffix, | |
49 base::CompareCase::INSENSITIVE_ASCII)) { | |
50 turn_transport_type = cricket::PROTO_TCP; | |
51 url.resize(url.size() - strlen(kTcpTransportSuffix)); | |
52 } else if (base::EndsWith(url, kUdpTransportSuffix, | |
53 base::CompareCase::INSENSITIVE_ASCII)) { | |
54 turn_transport_type = cricket::PROTO_UDP; | |
55 url.resize(url.size() - strlen(kUdpTransportSuffix)); | |
56 } | |
57 | |
58 size_t colon_pos = url.find(':'); | |
59 if (colon_pos == std::string::npos) | |
60 return false; | |
61 | |
62 std::string protocol = url.substr(0, colon_pos); | |
63 | |
64 std::string host; | |
65 int port; | |
66 if (!net::ParseHostAndPort(url.substr(colon_pos + 1), &host, &port)) | |
67 return false; | |
68 | |
69 if (protocol == "stun") { | |
70 if (port == -1) | |
71 port = kDefaultStunTurnPort; | |
72 config->stun_servers.push_back(rtc::SocketAddress(host, port)); | |
73 } else if (protocol == "turn") { | |
74 if (port == -1) | |
75 port = kDefaultStunTurnPort; | |
76 if (turn_transport_type == cricket::PROTO_LAST) | |
77 turn_transport_type = cricket::PROTO_UDP; | |
78 config->turn_servers.push_back(cricket::RelayServerConfig( | |
79 host, port, username, password, turn_transport_type, false)); | |
80 } else if (protocol == "turns") { | |
81 if (port == -1) | |
82 port = kDefaultTurnsPort; | |
83 if (turn_transport_type == cricket::PROTO_LAST) | |
84 turn_transport_type = cricket::PROTO_TCP; | |
85 config->turn_servers.push_back(cricket::RelayServerConfig( | |
86 host, port, username, password, turn_transport_type, true)); | |
87 } else { | |
88 return false; | |
89 } | |
90 | |
91 return true; | |
92 } | |
93 | |
94 } // namespace | |
95 | |
96 HttpIceConfigRequest::HttpIceConfigRequest( | |
97 UrlRequestFactory* url_request_factory, | |
98 const std::string& url) | |
99 : url_(url) { | |
100 url_request_ = | |
101 url_request_factory->CreateUrlRequest(UrlRequest::Type::POST, url_); | |
102 url_request_->SetPostData("application/json", ""); | |
103 } | |
104 | |
105 HttpIceConfigRequest::~HttpIceConfigRequest() {} | |
106 | |
107 void HttpIceConfigRequest::Send(const OnIceConfigCallback& callback) { | |
108 DCHECK(on_ice_config_callback_.is_null()); | |
109 DCHECK(!callback.is_null()); | |
110 | |
111 on_ice_config_callback_ = callback; | |
112 url_request_->Start( | |
113 base::Bind(&HttpIceConfigRequest::OnResponse, base::Unretained(this))); | |
114 } | |
115 | |
116 void HttpIceConfigRequest::OnResponse(const UrlRequest::Result& result) { | |
Jamie
2016/02/19 22:25:28
DCHECK(!on_ice_config_callback.is_null())?
Sergey Ulanov
2016/02/19 22:44:26
Done.
| |
117 if (!result.success) { | |
118 LOG(ERROR) << "Failed to fetch " << url_; | |
119 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig()); | |
120 return; | |
121 } | |
122 | |
123 if (result.status != 200) { | |
124 LOG(ERROR) << "Received status code " << result.status << " from " << url_ | |
125 << ": " << result.response_body; | |
126 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig()); | |
127 return; | |
128 } | |
129 | |
130 scoped_ptr<base::Value> json = base::JSONReader::Read(result.response_body); | |
131 base::DictionaryValue* dictionary; | |
132 base::ListValue* ice_servers_list; | |
133 if (!json || !json->GetAsDictionary(&dictionary) || | |
134 !dictionary->GetList("iceServers", &ice_servers_list)) { | |
135 LOG(ERROR) << "Received invalid response from " << url_ << ": " | |
136 << result.response_body; | |
137 base::ResetAndReturn(&on_ice_config_callback_).Run(IceConfig()); | |
138 return; | |
139 } | |
140 | |
141 IceConfig ice_config; | |
142 | |
143 // Parse lifetimeDuration field. | |
144 std::string lifetime_str; | |
145 base::TimeDelta lifetime; | |
146 if (!dictionary->GetString("lifetimeDuration", &lifetime_str) || | |
147 !ParseLifetime(lifetime_str, &lifetime)) { | |
148 LOG(ERROR) << "Received invalid lifetimeDuration value: " << lifetime_str; | |
149 | |
150 // If the |lifetimeDuration| field is missing or cannot be parsed then mark | |
151 // the config as expired so it will refreshed for the next session. | |
152 ice_config.expiration_time = base::Time::Now(); | |
153 } else { | |
154 ice_config.expiration_time = | |
155 base::Time::Now() + lifetime - | |
156 base::TimeDelta::FromSeconds(kMinimumConfigLifetimeSeconds); | |
157 } | |
158 | |
159 // Parse iceServers list and store them in |ice_config|. | |
160 bool errors_found = false; | |
161 for (base::Value* server : *ice_servers_list) { | |
162 base::DictionaryValue* server_dict; | |
163 if (!server->GetAsDictionary(&server_dict)) { | |
164 errors_found = true; | |
165 continue; | |
166 } | |
167 | |
168 base::ListValue* urls_list; | |
169 if (!server_dict->GetList("urls", &urls_list)) { | |
170 errors_found = true; | |
171 continue; | |
172 } | |
173 | |
174 std::string username; | |
175 server_dict->GetString("username", &username); | |
176 | |
177 std::string password; | |
178 server_dict->GetString("credential", &password); | |
179 | |
180 for (base::Value* url : *urls_list) { | |
181 std::string url_str; | |
182 if (!url->GetAsString(&url_str)) { | |
183 errors_found = true; | |
184 continue; | |
185 } | |
186 if (!AddServerToConfig(url_str, username, password, &ice_config)) { | |
187 LOG(ERROR) << "Invalid ICE server URL: " << url_str; | |
188 } | |
189 } | |
190 } | |
191 | |
192 if (errors_found) { | |
193 LOG(ERROR) << "Received ICE config from the server that contained errors: " | |
194 << result.response_body; | |
195 } | |
196 | |
197 // If there are no STUN or no TURN servers then mark the config as expired so | |
198 // it will refreshed for the next session. | |
199 if (errors_found || ice_config.stun_servers.empty() || | |
200 ice_config.turn_servers.empty()) { | |
201 ice_config.expiration_time = base::Time::Now(); | |
202 } | |
203 | |
204 base::ResetAndReturn(&on_ice_config_callback_).Run(ice_config); | |
205 } | |
206 | |
207 } // namespace protocol | |
208 } // namespace remoting | |
OLD | NEW |