Skip to content
Snippets Groups Projects
voip.rs 1.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • Timo Kösters's avatar
    Timo Kösters committed
    use crate::{services, Result, Ruma};
    
    use hmac::{Hmac, Mac, NewMac};
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
    use ruma::{api::client::voip::get_turn_server_info, SecondsSinceUnixEpoch};
    
    use sha1::Sha1;
    use std::time::{Duration, SystemTime};
    
    type HmacSha1 = Hmac<Sha1>;
    
    /// # `GET /_matrix/client/r0/voip/turnServer`
    ///
    /// TODO: Returns information about the recommended turn server.
    
    pub async fn turn_server_route(
    
    Timo Kösters's avatar
    Timo Kösters committed
        body: Ruma<get_turn_server_info::v3::IncomingRequest>,
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
    ) -> Result<get_turn_server_info::v3::Response> {
    
        let sender_user = body.sender_user.as_ref().expect("user is authenticated");
    
    
    Timo Kösters's avatar
    Timo Kösters committed
        let turn_secret = services().globals.turn_secret().clone();
    
    Jonas Platte's avatar
    Jonas Platte committed
        let (username, password) = if !turn_secret.is_empty() {
    
            let expiry = SecondsSinceUnixEpoch::from_system_time(
    
                SystemTime::now() + Duration::from_secs(services().globals.turn_ttl()),
    
            )
            .expect("time is valid");
    
            let username: String = format!("{}:{}", expiry.get(), sender_user);
    
            let mut mac = HmacSha1::new_from_slice(turn_secret.as_bytes())
                .expect("HMAC can take key of any size");
            mac.update(username.as_bytes());
    
            let password: String = base64::encode_config(mac.finalize().into_bytes(), base64::STANDARD);
    
            (username, password)
        } else {
            (
    
                services().globals.turn_username().clone(),
                services().globals.turn_password().clone(),
    
    Jonathan de Jong's avatar
    Jonathan de Jong committed
        Ok(get_turn_server_info::v3::Response {
    
    Jonas Platte's avatar
    Jonas Platte committed
            username,
            password,
    
            uris: services().globals.turn_uris().to_vec(),
            ttl: Duration::from_secs(services().globals.turn_ttl()),