1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use serde::{Deserialize, Serialize};
use shared::error::{Error, Result};
/// ICEServer describes a single STUN and TURN server that can be used by
/// the ICEAgent to establish a connection with a peer.
///
/// ## Specifications
///
/// * [W3C]
///
/// [W3C]: https://www.w3.org/TR/webrtc/#dom-rtciceserver
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Hash)]
#[serde(default)]
pub struct RTCIceServer {
/// The URLs representing the STUN or TURN servers.
pub urls: Vec<String>,
/// The username to use when authenticating with a TURN server.
pub username: String,
/// The credential (password or token) to use when authenticating with a TURN server.
pub credential: String,
}
impl RTCIceServer {
/// The URL as W3C "validate an ICE server URL" takes it. A `stuns:` URL whose host is an
/// IP address is taken: the algorithm has no condition that refuses it, and RFC 8489
/// section 8.1's "A 'stuns' URI containing an IP address MUST be rejected" binds the STUN
/// client that would use it, so gathering rejects it (`EngineSrflx`).
pub(crate) fn parse_url(&self, url_str: &str) -> Result<ice::url::Url> {
ice::url::Url::parse_url_lenient(url_str)
}
/// W3C "set a configuration", for one server: `urls` is not empty, and each url passes
/// "validate an ICE server URL" (webrtc-pc section 4.4.1.5).
///
/// # Errors
///
/// The W3C `SyntaxError` cases: no url ([`Error::ErrNoIceServerUrls`]), a scheme other than
/// `stun`, `stuns`, `turn` and `turns` ([`Error::ErrSchemeType`]), no opaque path, or one
/// with `/` or `@`, or a fragment ([`Error::ErrInvalidUrl`]), a query on a STUN url
/// ([`Error::ErrStunQuery`]) or one other than `transport=udp` / `transport=tcp` on a TURN
/// url ([`Error::ErrInvalidQuery`]), and a host and port that do not parse
/// ([`Error::ErrInvalidUrl`]). The
/// `InvalidAccessError` cases: a TURN url without a username or credential
/// ([`Error::ErrNoTurnCredentials`]), or with a username of 509 bytes or more (RFC 8489
/// section 14.3), or a username or credential that is not an RFC 8265 OpaqueString
/// ([`Error::InvalidAccessError`]).
pub(crate) fn validate(&self) -> Result<()> {
if self.urls.is_empty() {
return Err(Error::ErrNoIceServerUrls);
}
for url in &self.urls {
self.validate_url(url)?;
}
// "hostAndPortURL is failure": a host or a port the URL parser refuses is a
// SyntaxError like the textual checks above.
self.urls().map_err(|err| match err {
Error::Url(_) => Error::ErrInvalidUrl,
err => err,
})?;
Ok(())
}
/// The textual checks of W3C "validate an ICE server URL" that the URL parser of the ICE
/// agent does not make.
fn validate_url(&self, url: &str) -> Result<()> {
let Some((scheme, opaque_path)) = url.split_once(':') else {
return Err(Error::ErrSchemeType);
};
let scheme = scheme.to_ascii_lowercase();
let turn = match scheme.as_str() {
"stun" | "stuns" => false,
"turn" | "turns" => true,
_ => return Err(Error::ErrSchemeType),
};
// An opaque path: not empty, not starting with `/` (that is a hierarchical URL), and
// with neither `/` nor `@` in it; and no fragment.
let (path, query) = match opaque_path.split_once('?') {
Some((path, query)) => (path, Some(query)),
None => (opaque_path, None),
};
if path.is_empty() || path.contains(['/', '@']) || url.contains('#') {
return Err(Error::ErrInvalidUrl);
}
match query {
Some(_) if !turn => return Err(Error::ErrStunQuery),
Some(query) if query != "transport=udp" && query != "transport=tcp" => {
return Err(Error::ErrInvalidQuery);
}
_ => {}
}
if turn {
if self.username.is_empty() || self.credential.is_empty() {
return Err(Error::ErrNoTurnCredentials);
}
// RFC 8489 section 14.3: the username "MUST contain a UTF-8-encoded sequence of
// fewer than 509 bytes and MUST have been processed using the OpaqueString
// profile"; RFC 8265 section 4.1: a password conforms to the OpaqueString profile
// (4.2: no control, default-ignorable or unassigned code point, not empty).
let username = ::stun::opaque::opaque_string(&self.username)
.map_err(|_| Error::InvalidAccessError)?;
if username.len() >= 509 || ::stun::opaque::opaque_string(&self.credential).is_err() {
return Err(Error::InvalidAccessError);
}
}
Ok(())
}
/// Parses and returns the parsed ICE URLs, validating TURN credentials if necessary.
///
/// # Errors
///
/// Returns an error if:
/// - Any entry in `urls` is not a well-formed STUN/TURN URL
/// - A `turn:` or `turns:` URL is present but `username` or `credential` is empty
/// (`ErrNoTurnCredentials`)
pub fn urls(&self) -> Result<Vec<ice::url::Url>> {
let mut urls = vec![];
for url_str in &self.urls {
let mut url = self.parse_url(url_str)?;
if url.scheme == ice::url::SchemeType::Turn || url.scheme == ice::url::SchemeType::Turns
{
// https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.2)
if self.username.is_empty() || self.credential.is_empty() {
return Err(Error::ErrNoTurnCredentials);
}
url.username.clone_from(&self.username);
url.password.clone_from(&self.credential);
}
urls.push(url);
}
Ok(urls)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_ice_server_validate_success() {
let tests = vec![
(
RTCIceServer {
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: "placeholder".to_owned(),
},
true,
),
(
RTCIceServer {
urls: vec!["turn:[2001:db8:1234:5678::1]?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: "placeholder".to_owned(),
},
true,
),
/*TODO:(ICEServer{
URLs: []string{"turn:192.158.29.39?transport=udp"},
Username: "unittest".to_owned(),
Credential: OAuthCredential{
MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=",
AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA==",
},
CredentialType: ICECredentialTypeOauth,
}, true),*/
];
for (ice_server, expected_validate) in tests {
let result = ice_server.urls();
assert_eq!(result.is_ok(), expected_validate);
}
}
#[test]
fn test_ice_server_validate_failure() {
let tests = vec![
(
RTCIceServer {
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
..Default::default()
},
Error::ErrNoTurnCredentials,
),
(
RTCIceServer {
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: String::new(),
},
Error::ErrNoTurnCredentials,
),
(
RTCIceServer {
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: String::new(),
},
Error::ErrNoTurnCredentials,
),
(
RTCIceServer {
urls: vec!["turn:192.158.29.39?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: String::new(),
},
Error::ErrNoTurnCredentials,
),
];
for (ice_server, expected_err) in tests {
if let Err(err) = ice_server.urls() {
assert_eq!(err, expected_err, "{ice_server:?} with err {err:?}");
} else {
panic!("expected error, but got ok");
}
}
}
#[test]
fn test_ice_server_validate_failure_err_stun_query() {
let tests = vec![(
RTCIceServer {
urls: vec!["stun:google.de?transport=udp".to_owned()],
username: "unittest".to_owned(),
credential: String::new(),
},
Error::ErrStunQuery,
)];
for (ice_server, expected_err) in tests {
if let Err(err) = ice_server.urls() {
assert_eq!(err, expected_err, "{ice_server:?} with err {err:?}");
} else {
panic!("expected error, but got ok");
}
}
}
}