1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Start to build RTC relay route

This commit is contained in:
Pierre HUBERT 2021-02-08 17:20:03 +01:00
parent 63f56d3dae
commit 2ce94f19f1
4 changed files with 48 additions and 2 deletions

View File

@ -36,7 +36,7 @@ database:
# Video calls configuration
rtc-relay:
ip: ::ffff:127.0.0.1
ip: 127.0.0.1
token: SecretToken
ice-servers:
- stun:stun.l.google.com:19302

View File

@ -3,6 +3,7 @@ pub mod server;
pub mod server_controller;
pub mod user_ws_controller;
pub mod rtc_relay_controller;
pub mod account_controller;
pub mod user_controller;
pub mod settings_controller;

View File

@ -0,0 +1,42 @@
//! # 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!();
}

View File

@ -14,9 +14,9 @@ use percent_encoding::percent_decode_str;
use crate::api_data::http_error::HttpError;
use crate::constants::MAX_REQUEST_SIZE;
use crate::controllers::{rtc_relay_controller, user_ws_controller};
use crate::controllers::routes::{get_routes, RequestResult, Route};
use crate::controllers::routes::Method::{GET, POST};
use crate::controllers::user_ws_controller;
use crate::data::base_request_handler::{BaseRequestHandler, PostFile, RequestValue};
use crate::data::config::Config;
use crate::data::http_request_handler::HttpRequestHandler;
@ -318,6 +318,9 @@ pub async fn start_server(conf: &Config) -> std::io::Result<()> {
// User WebSocket route
.service(actix_web::web::resource("/ws").to(user_ws_controller::ws_route))
// RTC Relay WebSocket route
.service(actix_web::web::resource("/rtc_proxy/ws").to(rtc_relay_controller::open_ws))
// API routes
.route("**", web::get().to(process_request))
.route("**", web::post().to(process_request))