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
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://w3c.github.io/webrtc-pc/#dom-rtciceserver
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Hash)]
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 {
pub(crate) fn parse_url(&self, url_str: &str) -> Result<ice::url::Url> {
ice::url::Url::parse_url(url_str)
}
pub(crate) fn validate(&self) -> Result<()> {
self.urls()?;
Ok(())
}
/// Parses and returns the parsed ICE URLs, validating TURN credentials if necessary.
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");
}
}
}
}