BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / relay.rs

relay.rs

relay.rs 3.1 KiB Download Raw
1use std::fmt;
2
3use libwebrtc::peer_connection_factory::IceServer;
4use serde::Deserialize;
5use session::{IC3, Request, Scope, Session};
6
7use crate::error::{Error, Result};
8
9pub const TOKENS_URL: &str = "https://teams.microsoft.com/trap/tokens";
10pub const DEFAULT_RELAY_HOST: &str = "gateway-eu.az.relay.teams.cloud.microsoft";
11
12pub fn ic3_scope() -> Scope {
13 Scope::new(IC3, "Teams.AccessAsUser.All")
14}
15
16#[derive(Clone, Deserialize)]
17pub struct RelayCredentials {
18 #[serde(default)]
19 pub realm: String,
20 pub username: String,
21 pub password: String,
22}
23
24impl fmt::Debug for RelayCredentials {
25 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26 formatter
27 .debug_struct("RelayCredentials")
28 .field("realm", &self.realm)
29 .finish_non_exhaustive()
30 }
31}
32
33#[derive(Deserialize)]
34struct TokensAnswer {
35 tokens: Vec<RelayCredentials>,
36 #[serde(default)]
37 expires: u64,
38}
39
40#[derive(Debug, Clone)]
41pub struct RelayGrant {
42 pub credentials: RelayCredentials,
43 pub expires_in_seconds: u64,
44}
45
46impl RelayGrant {
47 pub fn ice_server(&self, relay_host: &str) -> IceServer {
48 IceServer {
49 urls: vec![
50 format!("turn:{relay_host}:3478?transport=udp"),
51 format!("turn:{relay_host}:443?transport=tcp"),
52 format!("turns:{relay_host}:443"),
53 ],
54 username: self.credentials.username.clone(),
55 password: self.credentials.password.clone(),
56 }
57 }
58}
59
60pub async fn fetch_relay_grant(session: &Session) -> Result<RelayGrant> {
61 let mut request = Request::get(TOKENS_URL);
62 request.headers = vec![
63 ("api-version".into(), "2".into()),
64 ("X-MS-Migration".into(), "True".into()),
65 ("Accept".into(), "application/json, text/javascript".into()),
66 ];
67 let response = session.send(request, &ic3_scope()).await?;
68 let answer: TokensAnswer = serde_json::from_value(response.body)
69 .map_err(|error| Error::Signaling(format!("trap/tokens answer: {error}")))?;
70 let credentials = answer
71 .tokens
72 .into_iter()
73 .next()
74 .ok_or_else(|| Error::Signaling("trap/tokens returned no relay token".into()))?;
75 Ok(RelayGrant {
76 credentials,
77 expires_in_seconds: answer.expires,
78 })
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn debug_never_shows_secrets() {
87 let credentials = RelayCredentials {
88 realm: "rtcmedia".into(),
89 username: "user-secret".into(),
90 password: "pass-secret".into(),
91 };
92 let text = format!("{credentials:?}");
93 assert!(text.contains("rtcmedia") && !text.contains("secret"));
94 }
95
96 #[test]
97 fn ice_server_lists_udp_tcp_and_tls() {
98 let grant = RelayGrant {
99 credentials: RelayCredentials { realm: String::new(), username: "u".into(), password: "p".into() },
100 expires_in_seconds: 604800,
101 };
102 let server = grant.ice_server(DEFAULT_RELAY_HOST);
103 assert_eq!(server.urls.len(), 3);
104 assert!(server.urls[2].starts_with("turns:"));
105 }
106}