session-open-group-server/src/rpc.rs

447 lines
18 KiB
Rust
Raw Normal View History

2021-03-17 05:55:04 +01:00
use std::collections::HashMap;
2021-04-01 00:55:47 +02:00
use log::warn;
2021-03-23 01:13:32 +01:00
use serde::{Deserialize, Serialize};
2021-03-25 00:56:16 +01:00
use warp::{http::StatusCode, reply::Reply, reply::Response, Rejection};
2021-03-12 05:11:12 +01:00
2021-03-12 06:40:24 +01:00
use super::errors::Error;
2021-03-12 05:11:12 +01:00
use super::handlers;
2021-03-24 00:02:53 +01:00
use super::models;
2021-03-12 05:11:12 +01:00
use super::storage;
2021-03-31 02:00:02 +02:00
#[allow(dead_code)]
2021-04-27 01:12:07 +02:00
pub enum Mode {
2021-03-29 01:06:56 +02:00
FileServer,
OpenGroupServer,
}
2021-03-12 08:55:37 +01:00
#[derive(Deserialize, Serialize, Debug)]
pub struct RpcCall {
pub endpoint: String,
pub body: String,
2021-03-17 05:55:04 +01:00
pub method: String,
2021-03-25 01:38:06 +01:00
pub headers: HashMap<String, String>,
2021-03-12 08:55:37 +01:00
}
2021-04-27 01:12:07 +02:00
pub const MODE: Mode = Mode::OpenGroupServer;
2021-03-29 01:06:56 +02:00
2021-04-01 01:32:25 +02:00
pub async fn handle_rpc_call(rpc_call: RpcCall) -> Result<Response, Rejection> {
2021-03-23 23:27:21 +01:00
// Check that the endpoint is a valid URI and deconstruct it into a path
// and query parameters.
2021-03-24 03:54:00 +01:00
// Adding "http://placeholder.io" in front of the endpoint is a workaround
2021-03-23 23:27:21 +01:00
// for the fact that the URL crate doesn't accept relative URLs. There are
// other (cleaner) ways to fix this but they tend to be much more complex.
2021-05-14 06:22:33 +02:00
let raw_uri = format!("http://placeholder.io/{}", rpc_call.endpoint.trim_start_matches('/'));
2021-03-23 23:12:54 +01:00
let path: String = match raw_uri.parse::<http::Uri>() {
2021-05-14 06:22:33 +02:00
Ok(uri) => uri.path().trim_start_matches('/').to_string(),
2021-03-12 05:11:12 +01:00
Err(e) => {
2021-04-01 00:55:47 +02:00
warn!("Couldn't parse URI from: {} due to error: {}.", &raw_uri, e);
2021-03-23 23:12:54 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
let query_params: HashMap<String, String> = match url::Url::parse(&raw_uri) {
Ok(url) => url.query_pairs().into_owned().collect(),
Err(e) => {
2021-04-01 00:55:47 +02:00
warn!("Couldn't parse URL from: {} due to error: {}.", &raw_uri, e);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
};
2021-03-17 23:35:51 +01:00
// Get the auth token if possible
let auth_token = get_auth_token(&rpc_call);
2021-04-27 07:51:53 +02:00
// Get the room ID
let room_id_str = get_room_id(&rpc_call);
2021-03-12 05:11:12 +01:00
// Switch on the HTTP method
match rpc_call.method.as_ref() {
2021-04-27 07:51:53 +02:00
"GET" => {
return handle_get_request(room_id_str, rpc_call, &path, auth_token, query_params).await
2021-04-27 07:51:53 +02:00
}
"POST" => return handle_post_request(room_id_str, rpc_call, &path, auth_token).await,
2021-03-29 06:26:12 +02:00
"DELETE" => {
let pool = get_pool_for_room(&rpc_call)?;
2021-04-27 05:48:34 +02:00
return handle_delete_request(rpc_call, &path, auth_token, &pool).await;
2021-03-29 06:26:12 +02:00
}
2021-03-12 05:11:12 +01:00
_ => {
2021-04-01 00:55:47 +02:00
warn!("Ignoring RPC call with invalid or unused HTTP method: {}.", rpc_call.method);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
}
}
2021-04-01 01:32:25 +02:00
async fn handle_get_request(
2021-04-27 07:51:53 +02:00
room_id: Option<String>, rpc_call: RpcCall, path: &str, auth_token: Option<String>,
2021-03-29 06:26:12 +02:00
query_params: HashMap<String, String>,
2021-03-25 00:56:16 +01:00
) -> Result<Response, Rejection> {
2021-03-25 04:05:46 +01:00
// Handle routes that don't require authorization first
if path == "auth_token_challenge" {
2021-04-27 01:12:07 +02:00
reject_if_file_server_mode(path)?;
2021-03-29 06:26:12 +02:00
let pool = get_pool_for_room(&rpc_call)?;
2021-03-31 02:23:45 +02:00
let challenge = handlers::get_auth_token_challenge(query_params, &pool)?;
#[derive(Debug, Deserialize, Serialize)]
struct Response {
status_code: u16,
2021-03-25 01:38:06 +01:00
challenge: models::Challenge,
}
2021-03-25 00:56:16 +01:00
let response = Response { status_code: StatusCode::OK.as_u16(), challenge };
return Ok(warp::reply::json(&response).into_response());
2021-03-25 04:05:46 +01:00
} else if path.starts_with("rooms") {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-03-25 04:05:46 +01:00
if components.len() == 1 {
2021-03-31 02:23:45 +02:00
return handlers::get_all_rooms();
2021-03-25 04:05:46 +01:00
} else if components.len() == 2 {
let room_id = components[1];
2021-03-31 02:23:45 +02:00
return handlers::get_room(&room_id);
} else if components.len() == 3 && components[2] == "image" {
let room_id = components[1];
2021-04-01 01:32:25 +02:00
return handlers::get_group_image(&room_id).await;
2021-03-25 04:05:46 +01:00
} else {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
2021-05-28 05:55:51 +02:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
} else if path.starts_with("session_version") {
match MODE {
Mode::OpenGroupServer => {
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
Mode::FileServer => (),
}
let platform = query_params
.get("platform")
.ok_or_else(|| warp::reject::custom(Error::InvalidRpcCall))?;
2021-05-28 06:19:08 +02:00
let version = handlers::get_session_version(platform).await?;
let response = handlers::GenericStringResponse {
status_code: StatusCode::OK.as_u16(),
result: version,
};
return Ok(warp::reply::json(&response).into_response());
}
2021-04-27 01:12:07 +02:00
// This route requires auth in open group server mode, but not in file server mode
2021-03-29 06:26:12 +02:00
let pool = get_pool_for_room(&rpc_call)?;
2021-03-23 05:50:03 +01:00
if path.starts_with("files") {
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-03-19 00:09:13 +01:00
if components.len() != 2 {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
2021-03-19 00:09:13 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
2021-04-27 03:01:20 +02:00
let file_id: u64 = match components[1].parse() {
2021-03-26 05:24:02 +01:00
Ok(file_id) => file_id,
Err(_) => {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
2021-03-26 05:24:02 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
2021-04-27 07:51:53 +02:00
return handlers::get_file(room_id, file_id, auth_token, &pool)
2021-04-01 01:32:25 +02:00
.await
2021-03-25 00:56:16 +01:00
.map(|json| warp::reply::json(&json).into_response());
2021-03-19 00:09:13 +01:00
}
2021-04-28 02:30:39 +02:00
// Handle routes that require authorization
2021-05-14 06:22:33 +02:00
let auth_token = auth_token.ok_or_else(|| warp::reject::custom(Error::NoAuthToken))?;
2021-03-23 05:50:03 +01:00
match path {
2021-03-29 01:06:56 +02:00
"messages" => {
reject_if_file_server_mode(path)?;
2021-04-16 07:02:43 +02:00
let messages = handlers::get_messages(query_params, &auth_token, &pool)?;
#[derive(Debug, Deserialize, Serialize)]
struct Response {
status_code: u16,
messages: Vec<models::Message>,
}
let response = Response { status_code: StatusCode::OK.as_u16(), messages };
return Ok(warp::reply::json(&response).into_response());
2021-03-29 01:06:56 +02:00
}
2021-03-25 00:56:16 +01:00
"deleted_messages" => {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-04-16 07:02:43 +02:00
let deletions = handlers::get_deleted_messages(query_params, &auth_token, &pool)?;
#[derive(Debug, Deserialize, Serialize)]
struct Response {
status_code: u16,
ids: Vec<models::DeletedMessage>,
2021-04-16 07:02:43 +02:00
}
let response = Response { status_code: StatusCode::OK.as_u16(), ids: deletions };
return Ok(warp::reply::json(&response).into_response());
2021-03-29 01:06:56 +02:00
}
"moderators" => {
reject_if_file_server_mode(path)?;
2021-04-16 07:02:43 +02:00
let public_keys = handlers::get_moderators(&auth_token, &pool)?;
#[derive(Debug, Deserialize, Serialize)]
struct Response {
status_code: u16,
moderators: Vec<String>,
}
let response =
Response { status_code: StatusCode::OK.as_u16(), moderators: public_keys };
return Ok(warp::reply::json(&response).into_response());
2021-03-29 01:06:56 +02:00
}
"block_list" => {
reject_if_file_server_mode(path)?;
2021-03-31 02:23:45 +02:00
return handlers::get_banned_public_keys(&auth_token, &pool);
2021-03-29 01:06:56 +02:00
}
"member_count" => {
reject_if_file_server_mode(path)?;
2021-03-31 02:23:45 +02:00
return handlers::get_member_count(&auth_token, &pool);
2021-03-25 00:56:16 +01:00
}
2021-03-12 05:11:12 +01:00
_ => {
2021-04-01 00:55:47 +02:00
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", rpc_call.endpoint);
2021-03-25 00:56:16 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
}
}
2021-04-01 01:32:25 +02:00
async fn handle_post_request(
2021-04-27 07:51:53 +02:00
room_id: Option<String>, rpc_call: RpcCall, path: &str, auth_token: Option<String>,
2021-03-25 00:56:16 +01:00
) -> Result<Response, Rejection> {
2021-04-16 06:34:32 +02:00
// Handle routes that don't require authorization first
2021-04-28 02:30:39 +02:00
// The compact poll endpoint expects the auth token to be in the request body; not
// in the headers.
2021-04-16 06:34:32 +02:00
if path == "compact_poll" {
reject_if_file_server_mode(path)?;
2021-04-20 02:28:21 +02:00
#[derive(Debug, Deserialize, Serialize)]
struct CompactPollRequestBodyWrapper {
requests: Vec<models::CompactPollRequestBody>,
}
let wrapper: CompactPollRequestBodyWrapper = match serde_json::from_str(&rpc_call.body) {
2021-04-16 06:34:32 +02:00
Ok(bodies) => bodies,
Err(e) => {
warn!(
2021-04-20 02:28:21 +02:00
"Couldn't parse compact poll request body wrapper from: {} due to error: {}.",
2021-04-16 06:34:32 +02:00
rpc_call.body, e
);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
2021-04-20 02:28:21 +02:00
return handlers::compact_poll(wrapper.requests);
2021-04-16 06:34:32 +02:00
}
2021-04-27 01:12:07 +02:00
// This route requires auth in open group server mode, but not in file server mode
let pool = get_pool_for_room(&rpc_call)?;
if path == "files" {
#[derive(Debug, Deserialize)]
struct JSON {
file: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
2021-04-29 02:21:27 +02:00
Ok(json) => json,
2021-04-27 01:12:07 +02:00
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
2021-04-27 07:51:53 +02:00
return handlers::store_file(room_id, &json.file, auth_token, &pool).await;
2021-04-27 01:12:07 +02:00
}
2021-04-28 02:30:39 +02:00
// Handle routes that require authorization
2021-05-14 06:22:33 +02:00
let auth_token = auth_token.ok_or_else(|| warp::reject::custom(Error::NoAuthToken))?;
2021-04-23 03:16:31 +02:00
if path.starts_with("rooms") {
reject_if_file_server_mode(path)?;
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-04-23 03:16:31 +02:00
if components.len() == 3 && components[2] == "image" {
#[derive(Debug, Deserialize)]
struct JSON {
file: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
2021-04-29 02:21:27 +02:00
Ok(json) => json,
2021-04-23 03:16:31 +02:00
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
let room_id = components[1];
return handlers::set_group_image(&json.file, &room_id, &auth_token, &pool).await;
} else {
warn!("Invalid endpoint: {}.", rpc_call.endpoint);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
}
2021-03-23 05:50:03 +01:00
match path {
"messages" => {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-03-12 05:11:12 +01:00
let message = match serde_json::from_str(&rpc_call.body) {
2021-03-17 01:51:11 +01:00
Ok(message) => message,
2021-03-12 05:11:12 +01:00
Err(e) => {
2021-04-01 00:55:47 +02:00
warn!("Couldn't parse message from: {} due to error: {}.", rpc_call.body, e);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
};
2021-04-20 02:28:21 +02:00
return handlers::insert_message(message, &auth_token, &pool);
2021-03-25 00:56:16 +01:00
}
2021-03-23 05:50:03 +01:00
"block_list" => {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-03-19 06:44:07 +01:00
#[derive(Debug, Deserialize)]
2021-03-25 00:56:16 +01:00
struct JSON {
2021-03-25 01:38:06 +01:00
public_key: String,
2021-03-25 00:56:16 +01:00
}
2021-03-19 06:44:07 +01:00
let json: JSON = match serde_json::from_str(&rpc_call.body) {
2021-04-29 02:21:27 +02:00
Ok(json) => json,
2021-03-19 06:44:07 +01:00
Err(e) => {
2021-04-01 00:55:47 +02:00
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
2021-03-19 06:44:07 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
2021-04-20 02:28:21 +02:00
return handlers::ban(&json.public_key, &auth_token, &pool);
2021-03-25 00:56:16 +01:00
}
"ban_and_delete_all" => {
reject_if_file_server_mode(path)?;
#[derive(Debug, Deserialize)]
struct JSON {
public_key: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
return handlers::ban_and_delete_all_messages(&json.public_key, &auth_token, &pool);
}
2021-03-23 05:50:03 +01:00
"claim_auth_token" => {
2021-04-27 01:12:07 +02:00
reject_if_file_server_mode(path)?;
2021-03-19 06:44:07 +01:00
#[derive(Debug, Deserialize)]
2021-03-25 00:56:16 +01:00
struct JSON {
2021-03-25 01:38:06 +01:00
public_key: String,
2021-03-25 00:56:16 +01:00
}
2021-03-19 06:44:07 +01:00
let json: JSON = match serde_json::from_str(&rpc_call.body) {
2021-04-29 02:21:27 +02:00
Ok(json) => json,
2021-03-19 06:44:07 +01:00
Err(e) => {
2021-04-01 00:55:47 +02:00
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
2021-03-19 06:44:07 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
2021-04-20 02:28:21 +02:00
return handlers::claim_auth_token(&json.public_key, &auth_token, &pool);
2021-03-25 00:56:16 +01:00
}
2021-04-27 05:48:34 +02:00
"moderators" => {
reject_if_file_server_mode(path)?;
let body: models::ChangeModeratorRequestBody =
match serde_json::from_str(&rpc_call.body) {
2021-04-29 02:21:27 +02:00
Ok(body) => body,
2021-04-27 05:48:34 +02:00
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
return handlers::add_moderator_public(body, &auth_token).await;
}
2021-04-29 02:21:27 +02:00
"delete_messages" => {
reject_if_file_server_mode(path)?;
#[derive(Debug, Deserialize)]
struct JSON {
ids: Vec<i64>,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
return handlers::delete_messages(json.ids, &auth_token, &pool);
}
2021-03-12 05:11:12 +01:00
_ => {
2021-04-01 00:55:47 +02:00
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
2021-03-25 00:56:16 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
}
}
2021-04-27 05:48:34 +02:00
async fn handle_delete_request(
rpc_call: RpcCall, path: &str, auth_token: Option<String>,
pool: &storage::DatabaseConnectionPool,
2021-03-25 00:56:16 +01:00
) -> Result<Response, Rejection> {
// Check that the auth token is present
2021-05-14 06:22:33 +02:00
let auth_token = auth_token.ok_or_else(|| warp::reject::custom(Error::NoAuthToken))?;
2021-03-12 05:11:12 +01:00
// DELETE /messages/:server_id
2021-03-23 05:50:03 +01:00
if path.starts_with("messages") {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-03-12 05:11:12 +01:00
if components.len() != 2 {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", path);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
let server_id: i64 = match components[1].parse() {
Ok(server_id) => server_id,
2021-03-12 05:46:06 +01:00
Err(_) => {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", path);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
};
2021-03-31 02:23:45 +02:00
return handlers::delete_message(server_id, &auth_token, pool);
2021-03-12 05:11:12 +01:00
}
// DELETE /block_list/:public_key
2021-03-23 05:50:03 +01:00
if path.starts_with("block_list") {
2021-03-29 01:06:56 +02:00
reject_if_file_server_mode(path)?;
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-03-12 05:11:12 +01:00
if components.len() != 2 {
2021-04-01 00:55:47 +02:00
warn!("Invalid endpoint: {}.", path);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
let public_key = components[1].to_string();
2021-03-31 02:23:45 +02:00
return handlers::unban(&public_key, &auth_token, pool);
2021-03-12 05:11:12 +01:00
}
2021-03-18 00:29:59 +01:00
// DELETE /auth_token
2021-03-23 05:50:03 +01:00
if path == "auth_token" {
2021-04-27 01:12:07 +02:00
reject_if_file_server_mode(path)?;
2021-03-31 02:23:45 +02:00
return handlers::delete_auth_token(&auth_token, pool);
2021-03-18 00:29:59 +01:00
}
2021-04-27 05:48:34 +02:00
// DELETE /moderators/:public_key
2021-04-29 05:21:03 +02:00
if path.starts_with("moderators") {
2021-04-27 05:48:34 +02:00
reject_if_file_server_mode(path)?;
2021-05-14 06:22:33 +02:00
let components: Vec<&str> = path.split('/').collect(); // Split on subsequent slashes
2021-04-27 05:48:34 +02:00
if components.len() != 2 {
warn!("Invalid endpoint: {}.", path);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
let public_key = components[1].to_string();
let room_id = match get_room_id(&rpc_call) {
Some(room_id) => room_id,
None => {
warn!("Missing room ID.");
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
let body = models::ChangeModeratorRequestBody { public_key, room_id };
return handlers::delete_moderator_public(body, &auth_token).await;
}
2021-03-12 05:11:12 +01:00
// Unrecognized endpoint
2021-04-01 00:55:47 +02:00
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
2021-03-12 08:55:37 +01:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
2021-03-12 05:11:12 +01:00
}
2021-03-17 05:55:04 +01:00
// Utilities
2021-03-18 03:21:10 +01:00
2021-03-25 04:11:45 +01:00
fn get_pool_for_room(rpc_call: &RpcCall) -> Result<storage::DatabaseConnectionPool, Rejection> {
let room_id = get_room_id(&rpc_call).ok_or(Error::ValidationFailed)?;
return Ok(storage::pool_by_room_id(
&storage::RoomId::new(&room_id).ok_or(Error::ValidationFailed)?,
));
2021-03-25 04:11:45 +01:00
}
2021-03-17 23:35:51 +01:00
fn get_auth_token(rpc_call: &RpcCall) -> Option<String> {
2021-03-25 00:56:16 +01:00
if rpc_call.headers.is_empty() {
return None;
}
2021-03-23 01:13:32 +01:00
return rpc_call.headers.get("Authorization").map(|s| s.to_string());
2021-03-18 05:53:24 +01:00
}
2021-03-23 03:21:28 +01:00
fn get_room_id(rpc_call: &RpcCall) -> Option<String> {
2021-04-27 07:51:53 +02:00
match MODE {
// In file server mode we don't have a concept of rooms, but for convenience (i.e. so
// we can use the same database structure) we just always use the main room
Mode::FileServer => return Some("main".to_string()),
Mode::OpenGroupServer => {
if rpc_call.headers.is_empty() {
return None;
}
return rpc_call.headers.get("Room").map(|s| s.to_string());
}
2021-03-25 00:56:16 +01:00
}
}
2021-03-29 01:06:56 +02:00
fn reject_if_file_server_mode(path: &str) -> Result<(), Rejection> {
match MODE {
Mode::FileServer => {
2021-04-01 00:55:47 +02:00
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
2021-03-29 01:06:56 +02:00
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
Mode::OpenGroupServer => return Ok(()),
}
}