2022-10-05 20:34:31 +02:00
|
|
|
use std::time::{Duration, SystemTime};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2023-08-01 14:48:50 -10:00
|
|
|
use base64::{engine::general_purpose, Engine as _};
|
2024-06-05 18:42:56 -04:00
|
|
|
use conduit::utils;
|
2022-10-09 15:34:36 +02:00
|
|
|
use hmac::{Hmac, Mac};
|
2024-06-05 18:42:56 -04:00
|
|
|
use ruma::{api::client::voip::get_turn_server_info, SecondsSinceUnixEpoch, UserId};
|
2021-10-02 00:37:39 +02:00
|
|
|
use sha1::Sha1;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-10-02 00:37:39 +02:00
|
|
|
use crate::{services, Result, Ruma};
|
|
|
|
|
2024-06-05 18:42:56 -04:00
|
|
|
const RANDOM_USER_ID_LENGTH: usize = 10;
|
|
|
|
|
2021-10-02 00:37:39 +02:00
|
|
|
type HmacSha1 = Hmac<Sha1>;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/voip/turnServer`
|
|
|
|
///
|
|
|
|
/// TODO: Returns information about the recommended turn server.
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn turn_server_route(
|
2022-12-14 13:09:10 +01:00
|
|
|
body: Ruma<get_turn_server_info::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_turn_server_info::v3::Response> {
|
2022-10-05 09:34:25 +02:00
|
|
|
let turn_secret = services().globals.turn_secret().clone();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-11-26 20:36:40 +01:00
|
|
|
let (username, password) = if !turn_secret.is_empty() {
|
2021-10-02 00:37:39 +02:00
|
|
|
let expiry = SecondsSinceUnixEpoch::from_system_time(
|
2024-05-03 21:42:47 -04:00
|
|
|
SystemTime::now()
|
|
|
|
.checked_add(Duration::from_secs(services().globals.turn_ttl()))
|
|
|
|
.expect("TURN TTL should not get this high"),
|
2021-10-02 00:37:39 +02:00
|
|
|
)
|
|
|
|
.expect("time is valid");
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-06-05 18:42:56 -04:00
|
|
|
let user = body.sender_user.unwrap_or_else(|| {
|
|
|
|
UserId::parse_with_server_name(
|
|
|
|
utils::random_string(RANDOM_USER_ID_LENGTH).to_lowercase(),
|
|
|
|
&services().globals.config.server_name,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
});
|
|
|
|
|
|
|
|
let username: String = format!("{}:{}", expiry.get(), user);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-10-02 00:37:39 +02:00
|
|
|
let mut mac = HmacSha1::new_from_slice(turn_secret.as_bytes()).expect("HMAC can take key of any size");
|
|
|
|
mac.update(username.as_bytes());
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2023-08-01 14:48:50 -10:00
|
|
|
let password: String = general_purpose::STANDARD.encode(mac.finalize().into_bytes());
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-10-02 00:37:39 +02:00
|
|
|
(username, password)
|
|
|
|
} else {
|
|
|
|
(
|
2022-09-06 23:15:09 +02:00
|
|
|
services().globals.turn_username().clone(),
|
|
|
|
services().globals.turn_password().clone(),
|
2021-10-02 00:37:39 +02:00
|
|
|
)
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(get_turn_server_info::v3::Response {
|
2021-11-26 19:28:47 +01:00
|
|
|
username,
|
|
|
|
password,
|
2022-09-06 23:15:09 +02:00
|
|
|
uris: services().globals.turn_uris().to_vec(),
|
|
|
|
ttl: Duration::from_secs(services().globals.turn_ttl()),
|
2022-01-22 16:58:32 +01:00
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|