2024-12-05 19:54:34 +01:00
|
|
|
use crate::api::model::app_state::AppState;
|
|
|
|
|
use crate::api::model::request::UserApiRequest;
|
2024-12-07 15:45:00 +01:00
|
|
|
use crate::api::model::shared_stream::SharedStream;
|
2025-01-07 14:00:13 +01:00
|
|
|
use crate::debug_if_enabled;
|
|
|
|
|
use crate::model::api_proxy::ProxyUserCredentials;
|
2024-12-30 12:40:22 +01:00
|
|
|
use crate::model::config::{ConfigInput, ConfigTarget};
|
2025-01-07 14:00:13 +01:00
|
|
|
use crate::model::playlist::PlaylistItemType;
|
2024-04-01 18:02:02 +02:00
|
|
|
use crate::utils::request_utils;
|
2024-11-10 14:17:24 +01:00
|
|
|
use crate::utils::request_utils::mask_sensitive_info;
|
2025-01-07 14:00:13 +01:00
|
|
|
use actix_web::http::header::DATE;
|
2024-12-05 19:54:34 +01:00
|
|
|
use actix_web::http::header::{HeaderValue, CACHE_CONTROL};
|
|
|
|
|
use actix_web::{HttpRequest, HttpResponse};
|
2024-12-06 17:05:53 +01:00
|
|
|
use bytes::Bytes;
|
2024-12-07 15:45:00 +01:00
|
|
|
use chrono::Utc;
|
2025-01-07 14:00:13 +01:00
|
|
|
use log::{ error};
|
2024-12-05 19:54:34 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::path::Path;
|
2025-01-07 14:00:13 +01:00
|
|
|
use tokio_stream::wrappers::{BroadcastStream};
|
2024-12-05 19:54:34 +01:00
|
|
|
use url::Url;
|
2025-01-07 14:00:13 +01:00
|
|
|
use crate::api::model::buffered_stream;
|
|
|
|
|
use crate::api::model::buffered_stream::get_stream_response_with_headers;
|
2023-10-20 12:18:44 +02:00
|
|
|
|
2024-11-04 18:46:56 +01:00
|
|
|
pub async fn serve_file(file_path: &Path, req: &HttpRequest, mime_type: mime::Mime) -> HttpResponse {
|
2023-10-20 12:18:44 +02:00
|
|
|
if file_path.exists() {
|
2023-12-01 21:38:23 +01:00
|
|
|
if let Ok(file) = actix_files::NamedFile::open_async(file_path).await {
|
2024-04-01 16:49:12 +02:00
|
|
|
let mut result = file.set_content_type(mime_type)
|
2023-12-01 21:38:23 +01:00
|
|
|
.disable_content_disposition().into_response(req);
|
|
|
|
|
let headers = result.headers_mut();
|
2024-11-04 18:46:56 +01:00
|
|
|
headers.insert(CACHE_CONTROL, HeaderValue::from_bytes(b"no-cache").unwrap());
|
2023-12-01 21:38:23 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2023-10-20 12:18:44 +02:00
|
|
|
}
|
2023-12-01 21:38:23 +01:00
|
|
|
HttpResponse::NoContent().finish()
|
2023-10-23 09:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-04 18:46:56 +01:00
|
|
|
pub fn get_user_target_by_credentials<'a>(username: &str, password: &str, api_req: &'a UserApiRequest,
|
2024-12-05 19:54:34 +01:00
|
|
|
app_state: &'a AppState) -> Option<(ProxyUserCredentials, &'a ConfigTarget)> {
|
2023-10-23 09:58:36 +02:00
|
|
|
if !username.is_empty() && !password.is_empty() {
|
2023-12-01 21:38:23 +01:00
|
|
|
app_state.config.get_target_for_user(username, password)
|
2023-10-23 09:58:36 +02:00
|
|
|
} else {
|
2023-10-25 21:07:38 +02:00
|
|
|
let token = api_req.token.as_str().trim();
|
2024-05-10 12:01:48 +02:00
|
|
|
if token.is_empty() {
|
2023-10-23 09:58:36 +02:00
|
|
|
None
|
2024-05-10 12:01:48 +02:00
|
|
|
} else {
|
|
|
|
|
app_state.config.get_target_for_user_by_token(token)
|
2023-10-23 09:58:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-22 21:06:58 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-03 14:54:42 +01:00
|
|
|
pub fn get_user_target<'a>(api_req: &'a UserApiRequest, app_state: &'a AppState) -> Option<(ProxyUserCredentials, &'a ConfigTarget)> {
|
2023-11-22 21:06:58 +01:00
|
|
|
let username = api_req.username.as_str().trim();
|
|
|
|
|
let password = api_req.password.as_str().trim();
|
|
|
|
|
get_user_target_by_credentials(username, password, api_req, app_state)
|
2024-04-01 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 17:51:33 +01:00
|
|
|
/// Creates a notify stream for the given URL if a shared stream exists.
|
|
|
|
|
async fn create_notify_stream(
|
|
|
|
|
app_state: &AppState,
|
|
|
|
|
stream_url: &str,
|
2024-12-07 15:45:00 +01:00
|
|
|
) -> Option<BroadcastStream<Bytes>> {
|
2024-12-06 17:05:53 +01:00
|
|
|
let notify_stream_url = stream_url.to_string();
|
2024-12-06 17:51:33 +01:00
|
|
|
|
|
|
|
|
// Acquire lock and check for existing stream
|
|
|
|
|
let shared_streams = app_state.shared_streams.lock().await;
|
2024-12-06 17:05:53 +01:00
|
|
|
if let Some(shared_stream) = shared_streams.get(¬ify_stream_url) {
|
|
|
|
|
let rx = shared_stream.data_stream.subscribe();
|
2024-12-07 15:45:00 +01:00
|
|
|
Some(BroadcastStream::new(rx))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2024-12-06 17:05:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 14:34:33 +01:00
|
|
|
pub async fn stream_response(app_state: &AppState, stream_url: &str, req: &HttpRequest, input: Option<&ConfigInput>, item_type: PlaylistItemType, target: &ConfigTarget) -> HttpResponse {
|
2024-12-09 19:26:39 +01:00
|
|
|
debug_if_enabled!("Try to open stream {}", mask_sensitive_info(stream_url));
|
2025-01-08 21:17:57 +01:00
|
|
|
|
2025-01-07 14:34:33 +01:00
|
|
|
let share_stream = is_stream_share_enabled(item_type, target);
|
2024-12-05 19:57:13 +01:00
|
|
|
if share_stream {
|
2024-12-07 15:45:00 +01:00
|
|
|
if let Some(value) = shared_stream_response(app_state, stream_url).await {
|
|
|
|
|
return value;
|
2024-12-05 19:54:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 14:34:33 +01:00
|
|
|
let send_bytes = matches!(item_type, PlaylistItemType::Video | PlaylistItemType::Series);
|
|
|
|
|
|
2024-04-01 18:02:02 +02:00
|
|
|
if let Ok(url) = Url::parse(stream_url) {
|
2025-01-08 21:17:57 +01:00
|
|
|
let stream = buffered_stream::get_buffered_stream(&url, req, input, send_bytes);
|
2025-01-07 14:00:13 +01:00
|
|
|
return if share_stream {
|
|
|
|
|
SharedStream::register(app_state, stream_url, stream).await;
|
|
|
|
|
if let Some(broadcast_stream) = create_notify_stream(app_state, stream_url).await {
|
|
|
|
|
let body_stream = actix_web::body::BodyStream::new(broadcast_stream);
|
|
|
|
|
let mut response_builder = get_stream_response_with_headers();
|
|
|
|
|
response_builder.body(body_stream)
|
|
|
|
|
} else {
|
|
|
|
|
HttpResponse::BadRequest().finish()
|
2024-04-01 18:02:02 +02:00
|
|
|
}
|
2025-01-07 14:00:13 +01:00
|
|
|
} else {
|
|
|
|
|
let mut response_builder = get_stream_response_with_headers();
|
2025-01-08 21:17:57 +01:00
|
|
|
response_builder.streaming(stream)
|
2024-04-01 18:02:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-01-07 14:00:13 +01:00
|
|
|
error!("Url is malformed {}", mask_sensitive_info(stream_url));
|
2024-04-01 18:02:02 +02:00
|
|
|
HttpResponse::BadRequest().finish()
|
2024-04-02 17:00:28 +02:00
|
|
|
}
|
2024-12-07 15:45:00 +01:00
|
|
|
|
|
|
|
|
async fn shared_stream_response(app_state: &AppState, stream_url: &str) -> Option<HttpResponse> {
|
|
|
|
|
if let Some(stream) = create_notify_stream(app_state, stream_url).await {
|
2024-12-09 19:26:39 +01:00
|
|
|
debug_if_enabled!("Using shared channel {}", mask_sensitive_info(stream_url));
|
2025-01-07 14:00:13 +01:00
|
|
|
if app_state.shared_streams.lock().await.get(stream_url).is_some() {
|
|
|
|
|
let mut response_builder = get_stream_response_with_headers();
|
2024-12-07 15:45:00 +01:00
|
|
|
let current_date = Utc::now().format("%a, %d %b %Y %H:%M:%S GMT").to_string();
|
|
|
|
|
response_builder.insert_header((DATE, current_date.as_bytes()));
|
|
|
|
|
// response_builder.insert_header((ACCEPT_RANGES, "bytes".as_bytes()));
|
|
|
|
|
return Some(response_builder.body(actix_web::body::BodyStream::new(stream)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
2024-12-09 16:43:19 +01:00
|
|
|
|
|
|
|
|
pub fn is_stream_share_enabled(item_type: PlaylistItemType, target: &ConfigTarget) -> bool {
|
2024-12-13 00:13:14 +01:00
|
|
|
item_type == PlaylistItemType::Live && target.options.as_ref().is_some_and(|opt| opt.share_live_streams)
|
2025-01-03 13:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
2025-01-08 21:17:57 +01:00
|
|
|
|
|
|
|
|
pub fn get_headers_from_request(req: &HttpRequest) -> HashMap<String, Vec<u8>> {
|
|
|
|
|
req.headers()
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(k, v)| (k.as_str().to_string(), v.as_bytes().to_vec()))
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 13:08:46 +01:00
|
|
|
pub async fn resource_response(_app_state: &AppState, resource_url: &str, req: &HttpRequest, input: Option<&ConfigInput>) -> HttpResponse {
|
2025-01-03 16:49:24 +01:00
|
|
|
if resource_url.is_empty() {
|
|
|
|
|
return HttpResponse::NoContent().finish();
|
|
|
|
|
}
|
2025-01-08 21:17:57 +01:00
|
|
|
let req_headers = get_headers_from_request(req);
|
2025-01-03 13:08:46 +01:00
|
|
|
debug_if_enabled!("Try to open resource {}", mask_sensitive_info(resource_url));
|
|
|
|
|
|
|
|
|
|
if let Ok(url) = Url::parse(resource_url) {
|
2025-01-08 21:17:57 +01:00
|
|
|
let client = request_utils::get_client_request(input.map(|i| &i.headers), &url, Some(&req_headers));
|
2025-01-03 13:08:46 +01:00
|
|
|
match client.send().await {
|
|
|
|
|
Ok(response) => {
|
|
|
|
|
let status = response.status();
|
|
|
|
|
if status.is_success() {
|
|
|
|
|
let mut response_builder = HttpResponse::Ok();
|
|
|
|
|
response.headers().iter().for_each(|(k, v)| {
|
|
|
|
|
response_builder.insert_header((k.as_str(), v.as_ref()));
|
|
|
|
|
});
|
|
|
|
|
return response_builder.body(actix_web::body::BodyStream::new(response.bytes_stream()));
|
|
|
|
|
}
|
|
|
|
|
debug_if_enabled!("Failed to open resource got status {} for {}", status, mask_sensitive_info(resource_url));
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
|
|
|
|
error!("Received failure from server {}: {}", mask_sensitive_info(resource_url), err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
error!("Url is malformed {}", mask_sensitive_info(resource_url));
|
|
|
|
|
}
|
|
|
|
|
HttpResponse::BadRequest().finish()
|
|
|
|
|
}
|