1111#include " base/logging.h"
1212#include " base/notreached.h"
1313#include " base/strings/string_number_conversions.h"
14+ #include " base/strings/string_split.h"
1415#include " base/strings/string_util.h"
1516#include " base/values.h"
1617#include " net/base/url_util.h"
@@ -34,69 +35,6 @@ bool ParseLifetime(const std::string& string, base::TimeDelta* result) {
3435 return true ;
3536}
3637
37- // Parses url in form of <stun|turn|turns>:<host>[:<port>][?transport=<udp|tcp>]
38- // and adds an entry to the |config|.
39- bool AddServerToConfig (std::string url,
40- const std::string& username,
41- const std::string& password,
42- IceConfig* config) {
43- cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST;
44-
45- const char kTcpTransportSuffix [] = " ?transport=tcp" ;
46- const char kUdpTransportSuffix [] = " ?transport=udp" ;
47- if (base::EndsWith (url, kTcpTransportSuffix ,
48- base::CompareCase::INSENSITIVE_ASCII)) {
49- turn_transport_type = cricket::PROTO_TCP;
50- url.resize (url.size () - strlen (kTcpTransportSuffix ));
51- } else if (base::EndsWith (url, kUdpTransportSuffix ,
52- base::CompareCase::INSENSITIVE_ASCII)) {
53- turn_transport_type = cricket::PROTO_UDP;
54- url.resize (url.size () - strlen (kUdpTransportSuffix ));
55- }
56-
57- size_t colon_pos = url.find (' :' );
58- if (colon_pos == std::string::npos) {
59- return false ;
60- }
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-
70- if (protocol == " stun" ) {
71- if (port == -1 ) {
72- port = kDefaultStunTurnPort ;
73- }
74- config->stun_servers .emplace_back (host, port);
75- } else if (protocol == " turn" ) {
76- if (port == -1 ) {
77- port = kDefaultStunTurnPort ;
78- }
79- if (turn_transport_type == cricket::PROTO_LAST) {
80- turn_transport_type = cricket::PROTO_UDP;
81- }
82- config->turn_servers .emplace_back (host, port, username, password,
83- turn_transport_type, false );
84- } else if (protocol == " turns" ) {
85- if (port == -1 ) {
86- port = kDefaultTurnsPort ;
87- }
88- if (turn_transport_type == cricket::PROTO_LAST) {
89- turn_transport_type = cricket::PROTO_TCP;
90- }
91- config->turn_servers .emplace_back (host, port, username, password,
92- turn_transport_type, true );
93- } else {
94- return false ;
95- }
96-
97- return true ;
98- }
99-
10038// Returns the smallest specified value, or 0 if neither is specified.
10139// A value is "specified" if it is greater than 0.
10240int MinimumSpecified (int value1, int value2) {
@@ -120,6 +58,11 @@ IceConfig::~IceConfig() = default;
12058
12159// static
12260IceConfig IceConfig::Parse (const base::Value::Dict& dictionary) {
61+ const base::Value::Dict* data = dictionary.FindDict (" data" );
62+ if (data) {
63+ return Parse (*data);
64+ }
65+
12366 const base::Value::List* ice_servers_list = dictionary.FindList (" iceServers" );
12467 if (!ice_servers_list) {
12568 return IceConfig ();
@@ -185,7 +128,7 @@ IceConfig IceConfig::Parse(const base::Value::Dict& dictionary) {
185128 errors_found = true ;
186129 continue ;
187130 }
188- if (!AddServerToConfig (*url_str, username, password, &ice_config )) {
131+ if (!ice_config. AddServer (*url_str, username, password)) {
189132 LOG (ERROR) << " Invalid ICE server URL: " << *url_str;
190133 }
191134 }
@@ -210,30 +153,6 @@ IceConfig IceConfig::Parse(const base::Value::Dict& dictionary) {
210153 return ice_config;
211154}
212155
213- // static
214- IceConfig IceConfig::Parse (const std::string& config_json) {
215- std::optional<base::Value> json = base::JSONReader::Read (config_json);
216- if (!json) {
217- return IceConfig ();
218- }
219-
220- base::Value::Dict* dictionary = json->GetIfDict ();
221- if (!dictionary) {
222- return IceConfig ();
223- }
224-
225- // Handle the case when the config is wrapped in 'data', i.e. as {'data': {
226- // 'iceServers': {...} }}.
227- if (!dictionary->Find (" iceServers" )) {
228- base::Value::Dict* data_dictionary = dictionary->FindDict (" data" );
229- if (data_dictionary) {
230- return Parse (*data_dictionary);
231- }
232- }
233-
234- return Parse (*dictionary);
235- }
236-
237156// static
238157IceConfig IceConfig::Parse (const apis::v1::GetIceConfigResponse& config) {
239158 IceConfig ice_config;
@@ -255,8 +174,7 @@ IceConfig IceConfig::Parse(const apis::v1::GetIceConfigResponse& config) {
255174 MinimumSpecified (ice_config.max_bitrate_kbps , server.max_rate_kbps ());
256175
257176 for (const auto & url : server.urls ()) {
258- if (!AddServerToConfig (url, server.username (), server.credential (),
259- &ice_config)) {
177+ if (!ice_config.AddServer (url, server.username (), server.credential ())) {
260178 LOG (ERROR) << " Invalid ICE server URL: " << url;
261179 }
262180 }
@@ -271,4 +189,68 @@ IceConfig IceConfig::Parse(const apis::v1::GetIceConfigResponse& config) {
271189 return ice_config;
272190}
273191
192+ bool IceConfig::AddStunServer (std::string_view url) {
193+ CHECK (url.starts_with (" stun:" ));
194+ return AddServer (url, /* username=*/ " " , /* password=*/ " " );
195+ }
196+
197+ bool IceConfig::AddServer (std::string_view url,
198+ const std::string& username,
199+ const std::string& password) {
200+ cricket::ProtocolType turn_transport_type = cricket::PROTO_LAST;
201+
202+ const char kTcpTransportSuffix [] = " ?transport=tcp" ;
203+ const char kUdpTransportSuffix [] = " ?transport=udp" ;
204+ if (base::EndsWith (url, kTcpTransportSuffix ,
205+ base::CompareCase::INSENSITIVE_ASCII)) {
206+ turn_transport_type = cricket::PROTO_TCP;
207+ url.remove_suffix (strlen (kTcpTransportSuffix ));
208+ } else if (base::EndsWith (url, kUdpTransportSuffix ,
209+ base::CompareCase::INSENSITIVE_ASCII)) {
210+ turn_transport_type = cricket::PROTO_UDP;
211+ url.remove_suffix (strlen (kUdpTransportSuffix ));
212+ }
213+
214+ auto parts = base::SplitStringOnce (url, ' :' );
215+ if (!parts) {
216+ return false ;
217+ }
218+
219+ auto [protocol, host_and_port] = *parts;
220+ std::string host;
221+ int port;
222+ if (!net::ParseHostAndPort (host_and_port, &host, &port)) {
223+ return false ;
224+ }
225+
226+ if (protocol == " stun" ) {
227+ if (port == -1 ) {
228+ port = kDefaultStunTurnPort ;
229+ }
230+ stun_servers.emplace_back (host, port);
231+ } else if (protocol == " turn" ) {
232+ if (port == -1 ) {
233+ port = kDefaultStunTurnPort ;
234+ }
235+ if (turn_transport_type == cricket::PROTO_LAST) {
236+ turn_transport_type = cricket::PROTO_UDP;
237+ }
238+ turn_servers.emplace_back (host, port, username, password,
239+ turn_transport_type, false );
240+ } else if (protocol == " turns" ) {
241+ if (port == -1 ) {
242+ port = kDefaultTurnsPort ;
243+ }
244+ if (turn_transport_type == cricket::PROTO_LAST) {
245+ turn_transport_type = cricket::PROTO_TCP;
246+ }
247+ turn_servers.emplace_back (host, port, username, password,
248+ turn_transport_type, true );
249+ } else {
250+ return false ;
251+ }
252+
253+ return true ;
254+ }
255+
274256} // namespace remoting::protocol
0 commit comments