1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-08 11:42:35 +00:00
comunicapiv3/src/controllers/rtc_relay_controller.rs

42 lines
1.5 KiB
Rust
Raw Normal View History

2021-02-08 16:20:03 +00:00
//! # RTC Relay controller
//!
//! @author Pierre Hubert
use crate::data::config::conf;
/// Establish a new connection with the RTC relay
///
/// Debug with
/// ```js
/// ws = new WebSocket("ws://0.0.0.0:3000/rtc_proxy/ws");
/// ws.onmessage = (msg) => console.log("WS msg", msg);
/// ws.onopen = () => console.log("Socket is open !");
/// ws.onerror = (e) => console.log("WS ERROR !", e);
/// ws.onclose = (e) => console.log("WS CLOSED!");
/// ```
pub async fn open_ws(req: actix_web::HttpRequest,
stream: actix_web::web::Payload) -> Result<actix_web::HttpResponse, actix_web::Error> {
let ip = req.peer_addr().unwrap();
// Check if video calls are enabled
if conf().rtc_relay.is_none() {
eprintln!("A relay from {} tried to connect to the server but the relay is disabled!", ip);
return Ok(actix_web::HttpResponse::BadRequest().body("RTC Relay not configured!"));
}
let conf = conf().rtc_relay.as_ref().unwrap();
// Check remote IP address
if !ip.ip().to_string().eq(&conf.ip) {
eprintln!("A relay from {} tried to connect to the server but the IP address is not authorized!", ip);
return Ok(actix_web::HttpResponse::Unauthorized().body("Access denied!"));
}
// Check the token
if !req.query_string().eq(&format!("token={}", &conf.token)) {
eprintln!("A relay from {} tried to connect with an invalid access token!", ip);
return Ok(actix_web::HttpResponse::Unauthorized().body("Invalid token!"));
}
unreachable!();
}